Professional Documents
Culture Documents
Top-down parsers
start at the root of derivation tree and ll in
choosing production for nonterminal is the key
problem
LL (predictive) parsers use lookahead to choose
production
Bottom-up parsers
start at leaves and ll in
choosing right-hand side (rhs) of production is
the key problem
LR parsers use current stack and lookahead to
choose production
3 b j
4 B ::= d
h i
unique.
2. a unique production A ::= applied to take
)
?1 to
i i
applied
4. a handle (A ::= ;k)
)
1 goal
h ::= expr
i h i
2 expr
h ::= expr + term
i h i h i
3 expr - term
j h i h i
4 term j h i
6 term / factor
j h i h i
7 factor j h i
9 j id
do i = n
to 1 by -1
(1)
find the handle (A ::= ; k )
i iin i
i
(2)
replace i with A i to generate
?1
i
stack
6
?
table-
source - scanner - driven - il
code parser
6
?
action
& goto
tables
ACTION GOTO
id + * $ <expr> <term> <factor>
S0 s4 | | | 1 2 3
S1 | | | acc | | |
S2 | s5 | r3 | | |
S3 | r5 s6 r5 | | |
S4 | r6 r6 r6 | | |
S5 s4 | | | 7 2 3
S6 s4 | | | | 8 3
S7 | | | r2 | | |
S8 | r4 | r4 | | |
The Grammar
1 <goal> ::= <expr>
2 <expr> ::= <term> + <expr>
3 <term>
j
6 <factor> ::= id