Title: Bottom-Up Parsing Basics
1 Bottom-Up Parsing Basics
2 Example Rightmost Derivation
Input String aaabffc
S -gt ABc A -gt Aa a B -gt bF b F -gt f F
f
S gt ABc gt AbFc gt AbfFc gt Abffc gt
Aabffc gt aaabffc
3 Example Symbol Stack
Input String aa abffc Symbol Stack A
The symbol stack summarizes the consumed
string.
S A B c A a
b F A a f F a f
Input String aaab ffc Symbol Stack Ab
Input String aaabff c Symbol Stack AbF
Input String aaabff c Symbol Stack AB
4 Example Item (NFA state)
Input String aa abffc NFA State A -gt
A.a
The NFA state shows the part of a rule
reachable using the consumed string.
S A B c A a
b F A a f F a f
Input String aaab ffc NFA State B -gt
b.F
Input String aaabff c NFA State B -gt
bF.
Input String aaabff c NFA State S -gt
AB.c
5 Example State transitions
Input String aaa bffc aaabffc aaabffc
S gt A . B c gt A b . F c gt A b f . F c
S A B c A a b
F A a f F a f
State Transition
B -gt b.F B -gt b. F -gt .f F F -gt .f
S -gt A.Bc B -gt .bF B -gt .b
b
6Symbol stack and State stack
Input String aaa b
f fc
Symbol Stack A b
f
State stack
S -gt .ABc A -gt .Aa A -gt .a
S -gt A . Bc A -gt A.a B -gt .bF B -gt .b
B -gt b.F B -gt b. F -gt . f F F -gt . f
F -gt f.F F -gt f.
A
b
f
reduce
shift
7Note 1 Redundancy
- The state stack can be generated from the
sequence of symbols on the symbol stack, by
tracing the derivation from the start symbol,
using the productions. - The state stack is used for efficiency.
- It avoids running the entire DFA (containing
production fragments) to determine the next state
each time.
8Why trace the entire symbol stack?
It is not possible to determine the current
production (state) by merely inspecting a fixed
number of stack symbols.
S -gt A B A -gt a A a B -gt a B
a
Input 1 a a a a Symbol Stack
a a a State
Input 2 a a a a Symbol Stack
a a a State
A -gt a . A A -gt .a
B -gt a . B B -gt .a
9 DFA State Input strings with eqvt. history
RHS of rules sharing a prefix
Input String1 ab ffc Input String2 ab ge
The DFA state captures all possible rule
prefixes reachable from the start symbol using
the consumed input.
Symbol Stack A b
B -gt b.F B -gt b. F -gt . f F F -gt . f D -gt b.g
S -gt ABc ADe A -gt Aa a B -gt bF b D -gt
bg dB F -gt f F f
State
10Stacking states is adequate
S gt A B c gt A b c
S gt A D e gt A d B e gt A d b e
The effect of reduction by B -gt b depends on
the previous state.
S -gt A.Bc B -gt .b
B
b
S -gt AB.c
B -gt b.
b
D -gt d.B B -gt .b
S -gt A.De D -gt .dB D -gt .bg
d
D -gt dB.
B
D
S -gt AD.e
11Note 2 Redundancy
- The state stack contains enough information to
regenerate the symbol stack. Note that all arcs
into a DFA state carry the same symbol label. - As a consequence, the symbol stack can be avoided
by making the reduce-entry in the parse table
specify the number of stack elements (length of
RHS) to be popped and the (LHS) nonterminal to be
pushed to make the transition. - In practice, the (synthesized) semantic attribute
associated with a stack symbol is allocated and
computed on a parallel stack.
12Relation to Grammars
- The DFA naturally corresponds to a left-linear
grammar.
S -gt A.Bc B -gt .b
B -gt b.
b
Grammar Rule P -gt Q b
Q
P
- The DFA state is obtained from the NFA states
using - the standard construction.