Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.

Slides:



Advertisements
Similar presentations
Lecture # 11 Grammar Problems.
Advertisements

Compiler Construction Dr. Naveed Ejaz Lecture 2. 2 Two-pass Compiler Front End Back End source code IR machine code errors.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
Chapter 2 A Simple Compiler
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
ParsingParsing. 2 Front-End: Parser  Checks the stream of words and their parts of speech for grammatical correctness scannerparser source code tokens.
Context-free Grammars
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Grammars This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax Directed Definitions Synthesized Attributes
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
PART I: overview material
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Recursive Descent Parsers Read and recognize the input (in order to translate it or evaluate it) Implicitly construct the derivation tree Design is driven.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Analyzer (Parser)
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Context Free Grammars and Regular Grammars Needs for CFG Grammars and Production Rules Context Free Grammars (CFG) Regular Grammars (RG)
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Introduction to Parsing
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
Programming Languages Translator
Context-free grammars, derivation trees, and ambiguity
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Construction
Parsing with Context Free Grammars
Compiler Design 4. Language Grammars
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Chapter 2: A Simple One Pass Compiler
CSC 4181Compiler Construction Context-Free Grammars
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
High-Level Programming Language
Faculty of Computer Science and Information System
Presentation transcript:

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object code) ? Parser

Here’s the Plan Design a context-free grammar to specify (syntactically) valid BL programs Use the grammar to implement a recursive descent parser (i.e., an algorithm to parse BL programs and construct the corresponding Program object)

Specifying Syntax with CFGs Context-free grammars (cfg) Some syntactically valid real constants: E E-93

Rewrite Rules for Real Constants . |. |.  E | E + | E -  |  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 can be rewritten as or

Four Components of a CFG Nonterminal symbols,,, Terminal symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, E, +, -,. Start symbol Rewrite rules on previous slide

A Derivation of 5.6E10 We begin with the start symbol… … and choose one of its rewrite rules: . |. |.

A Derivation of 5.6E10 . Now we choose one of the nonterminal symbols… … and choose one of its rewrite rules:  |

A Derivation of 5.6E10 . Again we choose one of the nonterminal symbols… … and choose one of its rewrite rules:  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

A Derivation of 5.6E10 .  5.  5. 6  5. 6 E  5. 6 E 1  5. 6 E 1 0 If it’s possible to find such a derivation, we write:  5.6E10 *

A Derivation Tree for 5.6E10. E

Find a Derivation Tree for 5.E3 The derivation tree: Can you find a derivation tree for.6E10?. E 35 Why not?

Language Generated by a CFG Definition: Let G = (Nonterminals, Terminals,, Rewrite Rules) be a context-free grammar. The language generated (or specified) by G is denoted L(G) and is defined as: * L(G) = {x: string of Terminals (  x)}

Another Example: A CFG for Boolean Expressions  T | F | NOT ( ) | ( AND ) | ( OR )

CFG for Boolean Expressions What are the nonterminal symbols? What are the terminal symbols? What is the start symbol? How many rewrite rules? T, F, AND, OR, NOT, (, ) 5

A Derivation Tree for NOT((T OR F)) NOT() OR() T F

Find a Derivation Tree for ((T OR NOT (F)) AND T) AND() T OR() TNOT() F

A Famous Context-free Grammar  |  |  ( ) |  + | -  * | DIV | MOD  |  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

What’s So Special About This CFG? Find a derivation tree for * * 2

What’s So Special Continued… Find the derivation tree for (4 + 6) * 2 4 * 2 () 6 +

How About These Rewrite Rules?  | ( ) |  + | - | * | DIV | MOD  |  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

New Rules Continued… Find a derivation tree for * * 2

New Rules Continued… How about this tree for * 2? 2 * 4 + 6