Nonrecursive Predictive Parsing It is possible to build a nonrecursive predictive parser This is done by maintaining an explicit stack
Nonrecursive Predictive Parsing It is possible to build a nonrecursive predictive parser This is done by maintaining an explicit stack
Table-driven Parsers The nonrecursive LL(1) parser looks up the production to apply by looking up a parsing table
Table-driven Parsers LL(1) table: One dimension for current non-terminal to expand One dimension for next token Table entry contains one production
Table-driven Parsers LL(1) table: One dimension for current non-terminal to expand One dimension for next token Table entry contains one production
Table-driven Parsers LL(1) table: One dimension for current non-terminal to expand One dimension for next token Table entry contains one production
* F T' Consider the expression grammar 1 E → T E' 2 E' + T E' 3 | e 4 5 T' * F T' 6 7 F ( E ) 8 id
Predictive Parsing Table id + * ( ) $ E E →TE' E' E' → +TE' E' →e T T →FT' T' T' → e T →*FT' T' →e F F → id F →(E ) Columns for next token Rows for current non-terminal to expand
Predictive Parsing Table id + * ( ) $ E E →TE' E' E' → +TE' E' →e T T →FT' T' T' → e T →*FT' T' →e F F → id F →(E ) Blank entries are errors Table entries are productions
Predictive Parsers The predictive parser uses an explicit stack to keep track of pending non-terminals It can thus be implemented without recursion.
Predictive Parsers The predictive parser uses an explicit stack to keep track of pending non-terminals It can thus be implemented without recursion.
Predictive Parsers a input + b $ stack X Predictive parser output Y Z Parsing table M
LL(1) Parsing Algorithm The input buffer contains the string to be parsed; $ is the end-of-input marker The stack contains a sequence of grammar symbols
LL(1) Parsing Algorithm Initially, the stack contains the start symbol of the grammar on the top of $.
LL(1) Parsing Algorithm The parser is controlled by a program that behaves as follows:
LL(1) Parsing Algorithm The program considers X, the symbol on top of the stack, and a, the current input symbol.
LL(1) Parsing Algorithm These two symbols, X and a determine the action of the parser. There are three possibilities.
LL(1) Parsing Algorithm These two symbols, X and a determine the action of the parser. There are three possibilities.
LL(1) Parsing Algorithm 1. X a $, the parser halts and annouces successful completion.
LL(1) Parsing Algorithm 2. X a $ the parser pops X off the stack and advances input pointer to next input symbol
LL(1) Parsing Algorithm 3. If X is a nonterminal, the program consults entry M[X,a] of parsing table M.
LL(1) Parsing Algorithm If the entry is a production M[X,a] = {X → UVW } the parser replaces X on top of the stack by WVU (with U on top).
LL(1) Parsing Algorithm As output, the parser just prints the production used: X → UVW However, any other code could be executed here.
LL(1) Parsing Algorithm If M[X,a] =error, the parser calls an error recovery routine
LL(1) Parsing Algorithm Example: Let’s parse the input string id+idid using the nonrecursive LL(1) parser
id + id id $ E $ Parsing Table M stack
Predictive Parsing Table id + * ( ) $ E E →TE' E' E' → +TE' E' →e T T →FT' T' T' → e T →*FT' T' →e F F → id F →(E )
Compiler Construction Sohail Aslam Lecture 17 compiler: intro
id + id id $ E $ E → T E' Parsing Table M stack
id + id id $ T E' $ T → F T' Parsing Table M stack end of lec 16 compiler: intro
id + id id $ F T' E' $ F → id Parsing Table M stack
id + id id $ id T' E' $ Parsing Table M stack
+ id id id $ T' E' $ T' → Parsing Table M stack
+ id id id $ E' $ E' → + T E' Parsing Table M stack
+ id id id $ + T E' $ Parsing Table M stack
Stack Input Ouput $E id+idid$ $E' T E →TE' $E' T' F T →FT' $E'T' id F → id $E' T' +idid$ $E' T' →e $E' T + E' → +TE'
Stack Input Ouput $E' T idid$ $E' T' F T →FT' $E' T' id F → id $E' T' id$ $E' T' F T → FT' id$ $E'T' id
Stack Input Ouput $E' T' $ $E' T' →e E' →e
LL(1) Parsing Algorithm Note that productions output are tracing out a lefmost derivation The grammar symbols on the stack make up left-sentential forms
LL(1) Parsing Algorithm Note that productions output are tracing out a lefmost derivation The grammar symbols on the stack make up left-sentential forms
LL(1) Table Construction Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal
LL(1) Table Construction Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal
id+idid E T E' F T' + T E' id e and so on ....
The leaves at any point form a string b A g id T' e + The leaves at any point form a string b A g
b only contains terminals F id T' e + b only contains terminals
id + id id input string is bbd The prefix b matches Next token is b
LL(1) Table Construction Consider the state S → bAg with b the next token and we are trying to match bbg There are two possibilities
LL(1) Table Construction Consider the state S → bAg with b the next token and we are trying to match bbg There are two possibilities
LL(1) Table Construction b belongs to an expansion of A Any A → a can be used if b can start a string derived from a
LL(1) Table Construction b belongs to an expansion of A Any A → a can be used if b can start a string derived from a
LL(1) Table Construction In this case we say that b FIRST(a)
LL(1) Table Construction b does not belong to an expansion of A Expansion of A is empty, i.e., A → e and b belongs an expansion of g, e.g., bw
LL(1) Table Construction b does not belong to an expansion of A Expansion of A is empty, i.e., A → e and b belongs an expansion of g, e.g., bw
LL(1) Table Construction which means that b can appear after A in a derivation of the form S → bAbw
LL(1) Table Construction We say that b FOLLOW(A)
LL(1) Table Construction Any A → a can be used if a expands to e We say that e FIRST(A) in this case
Computing FIRST Sets Definition FIRST(X) = { b | X → ba } { e | X → e }