Syntactic Analysis Part II

Slides:



Advertisements
Similar presentations
Grammar and Algorithm }
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Top-Down Parsing.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
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.
Professor Yihjia Tsai Tamkang University
Top-Down Parsing.
– 1 – CSCE 531 Spring 2006 Lecture 7 Predictive Parsing Topics Review Top Down Parsing First Follow LL (1) Table construction Readings: 4.4 Homework: Program.
Compiler Construction Sohail Aslam Lecture LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
Joey Paquet, 2000, 2002, Lecture 3 Syntactic Analysis Part I.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Parsing Top-Down.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Error recovery in top-down.
Compiler design Bottom-up parsing Concepts
Context free grammars Terminals Nonterminals Start symbol productions
Unit-3 Bottom-Up-Parsing.
Lecture #12 Parsing Types.
Parsing IV Bottom-up Parsing
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
SYNTAX ANALYSIS (PARSING).
Compiler design Bottom-up parsing: Canonical LR and LALR
Top-Down Parsing.
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
CS 540 George Mason University
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Top-Down Parsing Identify a leftmost derivation for an input string
Top-Down Parsing The parse tree is created top to bottom.
Chapter 4 Top Down Parser.
Lecture 8: Top-Down Parsing
Compiler design Error recovery in top-down predictive parsing
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Parsing IV Bottom-up Parsing
Computing Follow(A) : All Non-Terminals
Syntax Analysis - Parsing
Compiler design Syntactic analysis – part II First and follow sets
Lecture 7 Predictive Parsing
Chapter 3 Syntactic Analysis I.
Recursive descent parsing
Nonrecursive Predictive Parsing
Compiler Construction
Context Free Grammar – Quick Review
Predictive Parsing Program
Recursive descent parsing
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Syntactic Analysis Part II Lecture 4 Syntactic Analysis Part II Joey Paquet, 2000, 2002, 2008, 2012, 2013

Recursive Descent Predictive Parsers Part I Recursive Descent Predictive Parsers Joey Paquet, 2000, 2002, 2008, 2012, 2013

Method Build FIRST and FOLLOW sets For each non-terminal we have a corresponding function In each function, for each possible right-hand-side of the corresponding productions, we have a possible path to follow. The choice is made according to the FIRST set of the right hand sides If one of the alternatives is of the form A, the path is followed according to the FOLLOW set of the left-hand-side of the production If no valid path is found, the function returns false If any of the paths is followed without error, the function return true and does any appropriate computation Joey Paquet, 2000, 2002, 2008, 2012, 2013

Generating FIRST sets FIRST(A) = 1. if ( (AT)  (A is ) )  If , where  begins with a terminal symbol x, then x  FIRST(). Algorithmic definition: Or, generate the lookahead tree for A FIRST(A) = 1. if ( (AT)  (A is ) ) then FIRST(A) = {A} 2. if ( (A  N)  (AS1S2…Sk  R) | Si  (NT) ) then FIRST(A)  (FIRST(S1) – {}) if,  i < k (  FIRST(S1), …, FIRST(Si) ) then FIRST(A)  FIRST(Si+1) if (  FIRST(S1), …, FIRST(Sk) ) then FIRST(A)  {} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example E  TE E   | +TE T  FT T   | FT F  ( E ) | 0 | 1 FT’E’ 1 ( FIRST(E) = {0,1,(} E’  +TE’ FIRST(E’) = {,+} T FT’ 1 ( FIRST(T) = {0,1,(} T’ FT’  FIRST(T’) = {,} 1 ( F FIRST(F) = {0,1,(} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Generating the FOLLOW sets FOLLOW(A) is the set of terminals that can come right after A in any sentential form derivable from the grammar of the language. Algorithmic definition: FOLLOW( A | A  N ) = 1. if ( A == S ) then ( FOLLOW(A)  {$}) 2. if ( BA  R ) then (FOLLOW(A)  (FIRST() - {}) ) 3. if ( (BA  R)  () then ( FOLLOW(A)  FOLLOW(B) )  Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example E  TE E   | +TE T  FT T   | FT F  ( E ) | 0 | 1 FST(E) : { 0,1,( } FST(E’) : { ,+ } FST(T) : { 0,1,( } FST(T’) : { ,* } FST(F) : { 0,1,( } FLW(E) : { $1, )4 } : { $,) } FLW(E’) : { {$,)}6 } : { $,) } FLW(T) : { {+}2, {$,)}5,7 } : { +,$,) } FLW(T’) : { {+,$,)}9 } : { +,$,) } FLW(F) : { {}3, {+,$,)}8,10 } : { ,+,$,) } 1. (1) : : (A == S)  (‘$’  FLW(A)) : ‘$’  FLW(E) 2. (2) : ETE’ : (BA)  ( FLW(A)  (FST() - {}) ) : FLW(T)  (FST(E’) – {}) (2) : E’+TE’ : (BA)  ( FLW(A)  (FST() - {}) ) : FLW(T)  (FST(E’) – {}) 3. (2) : TFT’ : (BA)  ( FLW(A)  (FST() - {}) ) : FLW(F)  (FST(T’) – {}) (2) : T’*FT’ : (BA)  ( FLW(A)  (FST() - {}) ) : FLW(F)  (FST(T’) – {}) 4. (2) : F (E) : (BA)  ( FLW(A)  (FST() - {}) ) : FLW(E)  (FST( ) ) – {}) (3) : ETE’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(T)  FLW(E) 6. (3) : ETE’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(E’)  FLW(E) 7. (3) : E’+TE’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(T)  FLW(E’) (3) : E’+TE’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(E’)  FLW(E’) 8. (3) : TFT’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(F)  FLW(T) 9. (3) : TFT’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(T’)  FLW(T) 10. (3) : T’*FT’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(F)  FLW(T’) (3) : T’*FT’ : ( (BA)  (  FST() )  ( FLW(A)  FLW(B) ) : FLW(T’)  FLW(T’) Joey Paquet, 2000, 2002, 2008, 2012, 2013

Constructing the Parser Main parser function is: function to match tokens with the lookahead symbol (next token in input): parse(){ lookahead = nextToken() if (startSymbol()  match(“$”) ) return(true) else return(false)} match(token){ if (lookahead == token) lookahead = nextToken(); return(true) else lookahead = nextToken(); return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Constructing the Parser LHS(){ // LHSRHS1 | RHS2 | … |  if (lookahead  FIRST(RHS1) ) // LHSRHS1 if (non-terminals()  match(terminals) ) write(“LHSRHS1”) ; return(true) else return(false) else if (lookahead  FIRST(RHS2) ) // LHSRHS2 write(“LHSRHS2”) ; return(true) else if … // other right hand sides else if (lookahead  FOLLOW(LHS) ) // only if LHS exists write(“LHS”) ; return(true) else return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example E(){ // ETE’ if (lookahead  FIRST(TE’) ) if (T()  E’() ) write(“ETE’ ”) ; return(true) else return(false) else return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example E’(){ // E’+TE’ |  if (lookahead  FIRST(+TE’) ) if ( match(‘+’)  T()  E’() ) write(“E’+TE’ ”) ; return(true) else return(false) else if (lookahead  FOLLOW(E’) ) write(“E’”) ; return(true) else return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example T(){ // TFT’ if (lookahead  FIRST(FT’) ) if (F()  T’() ) write(“TFT’ ”) ; return(true) else return(false) else return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example T’(){ // T’FT’ |  if (lookahead  FIRST(FT’) ) if ( match(‘’)  F()  T’() ) write(“T’FT’ ”) ; return(true) else return(false) else if (lookahead  FOLLOW(T’) ) write(“T’”) ; return(true) else return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Example F(){ // F 0 | 1 | (E) if (lookahead  FIRST(0) ) if ( match(‘0’) ) write(“F0”) ; return(true) else return(false) else if (lookahead  FIRST(1) ) if ( match(‘1’) ) write(“F1”) ; return(true) else if (lookahead  FIRST(“(E)”) if ( match(‘(’)  E()  match(‘)’) ) write(“F(E)”) ; return(true) else return(false)} Joey Paquet, 2000, 2002, 2008, 2012, 2013

Table-Driven Predictive Parsers Part II Table-Driven Predictive Parsers Joey Paquet, 2000, 2002, 2008, 2012, 2013

Method Build FIRST and FOLLOW sets Build the parser table Implement the parser algorithm Joey Paquet, 2000, 2002, 2008, 2012, 2013

Building the Parser Table Algorithm: 1. p : ( (p  R)  (p : A) ) do steps 2 and 3 2. t : ( (t  T)  (t  FIRST()) ) add A to TT[A, t] 3. If (   FIRST() ) t : ((t  T)  (t  FOLLOW(A)) ) 4. e : ( (e  TT)  (e == ) ) add “error” to e Joey Paquet, 2000, 2002, 2008, 2012, 2013

Building the Parser Table r1: E  TE’ : FIRST(TE’) = {0,1,(} : TT[E,0][E,1][E,(] r2: E’  +TE’ : FIRST(+TE’) = {+} : TT[E’,+] r3: E’   : FOLLOW(E’) = {$,)} : TT[E’,$][E’,)] r4: T  FT’ : FIRST(FT’) = {0,1,(} : TT[T,0][T,1][T,(] r5: T’  *FT’ : FIRST(*FT’) = {*} : TT[T’,*] r6: T’   : FOLLOW(T’) = {+,$,)} : TT[T’,+][T’,$][T’,)] r7: F  0 : FIRST(0) = {0} : TT[F,0] r8: F  1 : FIRST(1) = {1} : TT[F,1] r9: F  (E) : FIRST( (E) ) = {(} : TT[F,(] Joey Paquet, 2000, 2002, 2008, 2012, 2013

Building the Parser Table r1: E  TE’ r2: E’  +TE’ r3: E’   r4: T  FT’ r5: T’  *FT’ r6: T’   r7: F  0 r8: F  1 r9: F  (E) 1 ( ) + * $ E r1 E’ r3 r2 T r4 T’ r6 r5 F r7 r8 r9 Joey Paquet, 2000, 2002, 2008, 2012, 2013

Parser Algorithm parse(){ push($) push(S) a = nextToken() while ( top()  $ ) do x = top() if ( x  T ) if ( x == a ) pop() ; a = nextToken() else skipErrors() ; error = true if ( TT[x,a]  ‘error’ ) pop() ; inverseMultiplePush(TT[x,a]) if ( ( a  $ )  ( error == true ) ) return(false) return(true)} Parser Algorithm *: skipErrors() will be explained in lecture 5 Joey Paquet, 2000, 2002, 2008, 2012, 2013

Table Parsing Example Stack Input Production Derivation 1 $E (0+1)*0$ 2 r1:ETE’ TE’ 3 $E’T r4:TFT’ FT’E’ 4 $E’T’F r9:F(E) (E)T’E’ 5 $E’T’)E( 6 $E’T’)E 0+1)*0$ (TE’)T’E’ 7 $E’T’)E’T (FT’E’)T’E’ 8 $E’T’)E’T’F r7:F0 (0T’E’)T’E’ 9 $E’T’)E’T’0 10 $E’T’)E’T’ +1)*0$ r6:T’ (0E’)T’E’ 11 $E’T’)E’ r2:E’+TE’ (0+TE’)T’E’ 12 $E’T’)E’T+ Table Parsing Example Joey Paquet, 2000, 2002, 2008, 2012, 2013

Table Parsing Example Stack Input Production Derivation 13 $E’T’)E’T 1)*0$ r4:TFT’ (0+FT’E’)T’E’ 14 $E’T’)E’T’F r8:F1 (0+1T’E’)T’E’ 15 $E’T’)E’T’1 16 $E’T’)E’T’ )*0$ r6:T’ (0+1E’)T’E’ 17 $E’T’)E’ r3:E’ (0+1)T’E’ 18 $E’T’) 19 $E’T’ *0$ r5:T’*FT’ (0+1)*FT’E’ 20 $E’T’F* 21 $E’T’F 0$ r7:F0 (0+1)*0T’E’ 22 $E’T’0 23 $ (0+1)*0E’ 24 $E’ (0+1)*0 25 recognized Table Parsing Example Joey Paquet, 2000, 2002, 2008, 2012, 2013