Syntax Analysis source program lexical analyzer tokens syntax analyzer

Slides:



Advertisements
Similar presentations
lec02-parserCFG March 27, 2017 Syntax Analyzer
Advertisements

Compiler Construction
Chapter 3 Syntax Analysis
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Top-Down Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
1 Compiler Construction Syntax Analysis Top-down parsing.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
1 Compiler Construction Syntax Analysis Top-down parsing.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Lec04-bottomupparser 4/13/2018 LR Parsing.
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
CSc 453 Syntax Analysis (Parsing)
Bottom-up Parsing.
Compiler Construction
Programming Languages Translator
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
Context free grammars Terminals Nonterminals Start symbol productions
Unit-3 Bottom-Up-Parsing.
Bottom-up Parsing.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Syntactic Analysis and Parsing
Parsing.
Compiler Construction
Top-down parsing cannot be performed on left recursive grammars.
CS 404 Introduction to Compiler Design
Compiler Construction
UNIT 2 - SYNTAX ANALYSIS Role of the parser Writing grammars
Bottom-Up Syntax Analysis
Top-Down Parsing.
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Top-Down Parsing CS 671 January 29, 2008.
CSC 4181 Compiler Construction Parsing
Lecture 7 Predictive Parsing
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Top-Down Parsing The parse tree is created top to bottom.
Bottom Up Parsing.
R.Rajkumar Asst.Professor CSE
Parsing IV Bottom-up Parsing
Grammar design: Associativity
Syntax Analysis - Parsing
Lecture 7 Predictive Parsing
Context Free Grammar – Quick Review
lec02-parserCFG May 27, 2019 Syntax Analyzer
Chap. 3 BOTTOM-UP PARSING
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Syntax Analysis source program lexical analyzer tokens syntax analyzer parse tree semantic analyzer parser tree by Neng-Fa Zhou

The Role of the Parser Construct a parse tree Report and recover from errors Collect information into symbol tables by Neng-Fa Zhou

Context-free Grammars G=(S ,N,P,S) S is a finite set of terminals N is a finite set of non-terminals P is a finite subset of production rules S is the start symbol by Neng-Fa Zhou

CFG: Examples Arithmetic expressions Statements E ::= T | E + T | E - T T ::= F | T * F |T / F F ::= id | (E) IfStatement ::= if E then Statement else Statement by Neng-Fa Zhou

CFG vs. Regular Expressions CFG is more expressive than RE Every language that can be described by regular expressions can also be described by a CFG Example languages that are CFG but not RE if-then-else statement, {anbn | n>=1} Non-CFG L1={wcw | w is in (a|b)*} L2={anbmcndm | n>=1 and m>=1} by Neng-Fa Zhou

Derivations aAb agb if A ::= g * a a * * a b and b g then a g a is a sentential form a is a sentence if it contains only terminal symbols * S a by Neng-Fa Zhou

Derivations leftmost derivation Rightmost derivation aAb agb if a is a string of terminals aAb agb if b is a string of terminals by Neng-Fa Zhou

Parse Trees A parse tree is any tree in which The root is labeled with S Each leaf is labeled with a token a or e Each interior node is labeled by a nonterminal If an interior node is labeled A and has children labeled X1,.. Xn, then A ::= X1...Xn is a production. by Neng-Fa Zhou

Parse Trees and Derivations E ::= E + E | E * E | E - E | - E | ( E ) | id by Neng-Fa Zhou

Ambiguity A grammar that produces more than one parse tree for some sentence is said to be ambiguous. by Neng-Fa Zhou

Eliminating Ambiguity Rewrite productions to take the precedence of operators into account stmt ::= matched_stmt | unmatched_stmt matched_stmt ::= if E then matched_stmt else matched_stmt | other unmatched_stmt ::= if E then stmt | if E then matched_stmt else unmatched_stmt by Neng-Fa Zhou

Eliminating Left-Recursion Direct left-recursion A ::= Aa | b A ::= Aa1 | ... |Aam|b1|...|bn A ::= b1A' | ... |bnA' A' ::= a1A' | ... | anA' | e A ::= bA' A' ::= aA' | e by Neng-Fa Zhou

Eliminating Indirect Left-Recursion Algorithm S ::= Aa | b A ::= Ac | Sd | e Arrange the nonterminals in some order A1,...,An. for (i in 1..n) { for (j in 1..i-1) { replace each production of the form Ai ::= Ajg by the productions Ai ::= d1g | d2g |... | dkg where Aj ::= d1 | d2 |... | dk } eliminate the immediate left recursion among Ai productions } by Neng-Fa Zhou

Left Factoring A ::= ab1 | ... | abn | g A ::= aA' | g A' ::= b1 | ... | bn by Neng-Fa Zhou

Top-Down Parsing Start from the start symbol and build the parse tree top-down Apply a production to a nonterminal. The right-hand of the production will be the children of the nonterminal Match terminal symbols with the input May require backtracking Some grammars are backtrack-free (predictive) by Neng-Fa Zhou

Construct Parse Trees Top-Down Start with the tree of one node labeled with the start symbol and repeat the following steps until the fringe of the parse tree matches the input string 1. At a node labeled A, select a production with A on its LHS and for each symbol on its RHS, construct the appropriate child 2. When a terminal is added to the fringe that doesn't match the input string, backtrack 3. Find the next node to be expanded ! Minimize the number of backtracks by Neng-Fa Zhou

Example Left-recursive Right-recursive E ::= T | E + T | E - T T ::= F F ::= id | number | (E) E ::= T E' E'::= + T E' | - T E' | e T::= F T' T' ::= * F T' | / F T' F ::= id | number | (E) x - 2 * y by Neng-Fa Zhou

Control Top-Down Parsing Heuristics Use input string to guide search Backtrack-free search Lookahead is necessary Predictive parsing by Neng-Fa Zhou

Predictive Parsing FIRST and FOLLOW FIRST(X) If X is a terminal FIRST(X)={X} If X::= e Add e to FIRST(X) If X::=Y1,Y2,…,Yk Add FIRST(Yi) to FIRST(X) if Y1…Yi-1 =>* e Add e to FIRST(X) if Y1…Yk =>* e by Neng-Fa Zhou

Predictive Parsing FIRST and FOLLOW FOLLOW(X) Add $ to FOLLOW(S) If A ::= aBb Add everything in FIRST(b) except for e to FOLLOW(B) If A ::= aBb (b=>* e) or A ::= aB Add everything in FOLLOW(A) to FOLLOW(B) by Neng-Fa Zhou

Recursive Descent Parsing match(expected_token){ if (input_token != expected_token) error(); else input_token = next_token(); }   main(){ exp(); match(EOS); exp(){ switch (input_token) { case ID, NUM, L_PAREN: term(); exp_prime(); return; default: exp_prime(){ switch (input_token){ case PLUS: match(PLUS); term(); exp_prime(); break; case MINUS: match(MINUS); case R_PAREN,EOS: default: error(); }   by Neng-Fa Zhou

Top-Down Parsing (Nonrecursive predictive parser) by Neng-Fa Zhou

Top-Down Parsing (Nonrecursive predictive parser) parsing table by Neng-Fa Zhou

Example by Neng-Fa Zhou

Parsing Table Construction for each production p = (A::=a) { for each terminal a in FIRST(a), add p to M[A,a]; if e is in FIRST(a) for each terminal b (including $) in FOLLOW(A) add p to M[A,b]; } by Neng-Fa Zhou

Example E ::= TE' E ::= E+T | T E' ::= +TE' | e T ::= T*F | F T ::= FT' T' ::= *FT' | e F ::= (E) | id E ::= E+T | T T ::= T*F | F F ::= (E) | id by Neng-Fa Zhou

LL(1) Grammar Example (non-LL(1) grammar) A grammar is said to be LL(1) if |M[A,a]|<=1 for each nonterminal A and terminal a. Example (non-LL(1) grammar) S ::= iEtSS' | a S' ::= eS | e E ::= b S ::=iEtS | iEtSeS | a E :: = b by Neng-Fa Zhou

Bottom-Up Parsing Start from the input sequence of tokens Apply a production to the sentential form and rewrite it to a new one Keep track of the current sentential form by Neng-Fa Zhou

Construct Parse Trees Bottom-Up 1.a = the given string of tokens; 2. Repeat (reduction) 2.1 Matches the RHS of a production with a substring of a 2.2 Replace RHS with the LHS of the production until no production rule applies (backtrack) or a becomes the start symbol (success); by Neng-Fa Zhou

Example abbcde aAbcde aAde S ::= aABe aABe A ::= Abc | b B ::= d S by Neng-Fa Zhou

Control Bottom-Up Parsing Handle A substring that matches the right side of a production Applying the production to the substring results in a right-sentential form, i.e., a sentential form occurring in a right-most derivation Example id + id * id E + id * id E + E * id E + E * E E ::= E+E E ::= E*E E ::= (E) E ::= id by Neng-Fa Zhou

Bottom-Up Parsing Shift-Reduce Parsing push '$' onto the stack; token = nextToken(); repeat if (there is a handle A::=b on top of the stack){ reduce b to A; /* reduce */ pop b off the stack; push A onto the stack; } else {/* shift */ shift token onto the stack; } until (top of stack is S and token is eof) by Neng-Fa Zhou

Example by Neng-Fa Zhou

A Problem in Shift-Reduce Parser The stack has to be scaned to see whether a handle appears on it. Use a state to uniquely identify a part of a handle (viable prefix) so that stack scanning becomes unnecessary by Neng-Fa Zhou

LR Parser by Neng-Fa Zhou

LR Parsing Program by Neng-Fa Zhou

Example (1) E ::= E+T (2) E ::= T (3) T ::= T*F (4) T ::= F (5) F ::= (E) (6) F ::= id id * id + id by Neng-Fa Zhou

LR Grammars LR grammar LR(k) grammar A grammar is said to be an LR grammar if we can construct a parsing table for it. LR(k) grammar lookahead of up to k input symbols SLR(1), LR(1), and LALR(1) grammars by Neng-Fa Zhou

SLR Parsing Tables LR(0) item A production with a dot at some position of the RHS A ::= XYZ we are expecting XYZ A ::= X YZ A ::= XY Z A ::= XYZ we have seen XYZ by Neng-Fa Zhou

Closure of a Set of Items I by Neng-Fa Zhou

Closure of a Set of Items I Example E' ::= E E ::= E+T | T T ::= T*F | F F ::= (E) | id closure({E'::= E}) = ? by Neng-Fa Zhou

The goto Operation Example goto(I,X) =closure({ A ::= aX b | A ::= a is in I}) Example I = {E' ::= E , E ::= E + T} goto(I,+) = ? by Neng-Fa Zhou

Canonical LR(0) Collection of Set of Items by Neng-Fa Zhou

E T F ( by Neng-Fa Zhou

Constructing SLR Parsing Table Construct C={I0,I1,…,In}, the collection of sets of LR(0) items for G’ (augmented grammar). If [Aaab] is in Ii where a is a terminal and goto(Ij,a)=Ij, the set action[i,a] to “shift j”. If [S’S] is in Ii, then set action[i,$] to “accept”. If [Aa] is in Ii, then set action[i,a] to “reduce Aa” for all a in FOLLOW(A). by Neng-Fa Zhou

Example (1) E ::= E+T (2) E ::= T (3) T ::= T*F (4) T ::= F (5) F ::= (E) (6) F ::= id id * id + id by Neng-Fa Zhou

Unambiguous Grammars that are not SLR(1) S ::= R L ::= * R L ::= id R ::= L by Neng-Fa Zhou

LR(1) Parsing Tables LR(1) item [A::=a ,a] LR(0) item + one look ahead terminal [A::=a ,a] reduce a to A only if the next symbol is a | by Neng-Fa Zhou

by Neng-Fa Zhou

LALR(1) Treat item closures Ii and Ij as one state if Ii and Ij differs from each other only in look ahead terminals. by Neng-Fa Zhou

Descriptive Power of Different Grammars LR(1) > LALR(1) > SLR(1) by Neng-Fa Zhou