Download presentation
Presentation is loading. Please wait.
1
Nonrecursive Predictive Parsing
It is possible to build a nonrecursive predictive parser This is done by maintaining an explicit stack
2
Nonrecursive Predictive Parsing
It is possible to build a nonrecursive predictive parser This is done by maintaining an explicit stack
3
Table-driven Parsers The nonrecursive LL(1) parser looks up the production to apply by looking up a parsing table
4
Table-driven Parsers LL(1) table:
One dimension for current non-terminal to expand One dimension for next token Table entry contains one production
5
Table-driven Parsers LL(1) table:
One dimension for current non-terminal to expand One dimension for next token Table entry contains one production
6
Table-driven Parsers LL(1) table:
One dimension for current non-terminal to expand One dimension for next token Table entry contains one production
7
* 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
8
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
9
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
10
Predictive Parsers The predictive parser uses an explicit stack to keep track of pending non-terminals It can thus be implemented without recursion.
11
Predictive Parsers The predictive parser uses an explicit stack to keep track of pending non-terminals It can thus be implemented without recursion.
12
Predictive Parsers a input + b $ stack X Predictive parser output Y Z
Parsing table M
13
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
14
LL(1) Parsing Algorithm
Initially, the stack contains the start symbol of the grammar on the top of $.
15
LL(1) Parsing Algorithm
The parser is controlled by a program that behaves as follows:
16
LL(1) Parsing Algorithm
The program considers X, the symbol on top of the stack, and a, the current input symbol.
17
LL(1) Parsing Algorithm
These two symbols, X and a determine the action of the parser. There are three possibilities.
18
LL(1) Parsing Algorithm
These two symbols, X and a determine the action of the parser. There are three possibilities.
19
LL(1) Parsing Algorithm
1. X a $, the parser halts and annouces successful completion.
20
LL(1) Parsing Algorithm
2. X a $ the parser pops X off the stack and advances input pointer to next input symbol
21
LL(1) Parsing Algorithm
3. If X is a nonterminal, the program consults entry M[X,a] of parsing table M.
22
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).
23
LL(1) Parsing Algorithm
As output, the parser just prints the production used: X → UVW However, any other code could be executed here.
24
LL(1) Parsing Algorithm
If M[X,a] =error, the parser calls an error recovery routine
25
LL(1) Parsing Algorithm
Example: Let’s parse the input string id+idid using the nonrecursive LL(1) parser
26
id + id id $ E $ Parsing Table M stack
27
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 )
28
Compiler Construction
Sohail Aslam Lecture 17 compiler: intro
29
id + id id $ E $ E → T E' Parsing Table M stack
30
id + id id $ T E' $ T → F T' Parsing Table M stack end of lec 16
compiler: intro
31
id + id id $ F T' E' $ F → id Parsing Table M stack
32
id + id id $ id T' E' $ Parsing Table M stack
33
+ id id id $ T' E' $ T' → Parsing Table M stack
34
+ id id id $ E' $ E' → + T E' Parsing Table M stack
35
+ id id id $ + T E' $ Parsing Table M stack
36
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'
37
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
38
Stack Input Ouput $E' T' $ $E' T' →e E' →e
39
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
40
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
41
LL(1) Table Construction
Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal
42
LL(1) Table Construction
Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal
43
id+idid E T E' F T' + T E' id e and so on ....
44
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
45
b only contains terminals
F id T' e + b only contains terminals
46
id + id id input string is bbd The prefix b matches Next token is b
47
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
48
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
49
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
50
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
51
LL(1) Table Construction
In this case we say that b FIRST(a)
52
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
53
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
54
LL(1) Table Construction
which means that b can appear after A in a derivation of the form S → bAbw
55
LL(1) Table Construction
We say that b FOLLOW(A)
56
LL(1) Table Construction
Any A → a can be used if a expands to e We say that e FIRST(A) in this case
57
Computing FIRST Sets Definition
FIRST(X) = { b | X → ba } { e | X → e }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.