Bottom-up Parsing
Building the Automata
Augmented grammar
G is the grammar and S is the staring symbol
Construct G’ by adding production S’ S into G
S’ is the new starting symbol
E.g.: G: S | G’: S’ S, S |
Meaning
The starting symbol may have several production rules and may be
used in other non-terminal’s production rules
Add S’ S to force the starting symbol to have a single production
When S’ S is seen, it is clear that parsing is done
Building the Automata
Given a grammar G
Step 1: augment G
Step 2: initial state
Construct the valid item set “I” of State 0 (the initial state)
Add S’ S into I
o All expansions have to start from here
Compute Closure(I) as the complete valid item set of state 0
o All possible expansions S can lead into
Step 3:
From state I, for all grammar symbol X
Construct J = Goto(I, X)
Compute Closure(J)
Create the new state with the corresponding Goto transition
o Only if the valid item set is non-empty and does not exist yet
Repeat Step 3 till no new states can be derived
Building the Automata -- Example
Grammar G:
SE
EE+T|T
T id | ( E )
Step 1: Augment G
S’ S SE EE+T|T T id | ( E )
Step 2:
Construct Closure(I0) for State 0 Expect to see S next
First add into I0: S’ S
S won’t just appear
Compute Closure(I0) May have to see E first and
S’ S SE reduce it to S using this rule
EE+T ET
T id T(E)
Building the Automata -- Example
Step 3 I0:
I1 S’ S SE
EE+T ET
Add into I1: Goto(I0, S) = S’ S
T id T(E)
No new items to be added to Closure (I1)
I2
Add into I2: Goto(I0, E) = S E EE+T
No new items to be added to Closure (I2)
I3 When E is moved to the stack (after a reduction),
Add into I3: Goto(I0, these
T) = two are the
E T possible handles
S E implies a reduction is to be done
No new items to be added to Closure
o should(Ibe
3) done if seeing Follow(S)
I4 E E + T implies + is expected to be the next input
Add into I4: Goto(I0, id) = T id
No new items to be added to Closure (I4)
Building the Automata -- Example
Step 3 I0:
S’ S SE
I5
EE+T ET
Add into I5: Goto(I0, “(”) = T ( E ) T id T(E)
Closure(I5)
EE+T ET
T id T(E) After seeing (, we expect E next
E could be reduced from other
No more moves from I0 E-production rules
So, put E-productions in the set
No possible moves from I1
I6
Add into I6: Goto(I2, +) = E E + T
Closure(I5)
T id T(E)
No possible moves from I3 and I4
Building the Automata -- Example
Step 3
I7
Add into I7: Goto(I5, E) =
T ( E ) E E + T
No new items to be added to Closure (I7)
Goto(I5, T) = I3
Goto(I5, id) = I4
Goto(I5, “(”) = I5
No more moves from I5
I8
Add into I8: Goto(I6, T) = E E + T
No new items to be added to Closure (I8)
Goto(I6, id) = I4
Goto(I6, “(”) = I5
Building the Automata -- Example
Step 3
I9
Add into I9: Goto(I7, “)”) =
T ( E )
No new items to be added to Closure (I9)
Goto(I7, +) = I6
No possible moves from I8 and I9
Building the Automata -- Example
S S I1
I6
S EE+T
I2 S E + T
T id EE+T
I0 EE+T T(E)
E I8
S’ S
SE I3 id
EE+T T (
ET
ET
T id id
T(E) I4
+
T T id
id
(
T ( E ) I7
EE+T
E T ( E ) )
ET T(E)
EE+T
I5 T id ( I9
T(E)
Building the Parsing Table
Action [M, N]
M states
N tokens
Actions =
Shift i: shift the input token into the stack and go to state i
Reduce i: reduce by the i-th production
Accept
Error
Goto [M, L]
M states
L non-terminals
Goto[i, j] = x
Move to state Sx
Building the Action Table
If state Ii has item A a , and
Goto(Ii, a) = Ij
Next symbol in the input is a
Then Action[Ii, a] = Ij
Meaning: Shift “a” to the stack and move to state Ij
Need to wait for the handle to appear or to complete
If State Ii has item A
Then Action[S, b] = reduce using A
For all b in Follow(A)
Meaning: The entire handle is in the stack, need to reduce
Need to wait to see Follow(A) to know that the handle is ready
E.g. S E EE+T
Current input can be either Follow(S) or +
Building the Action Table
If state has S’ S0
Then Action[S, $] = accept
Current state
The action to be taken depends on the current state
In LL, it depends on the current non-terminal on the top of the stack
In LR, non-terminal is not known till reduction is done
Who is keeping track of current state?
The stack
Need to push the state also into the stack
The stack includes the viable prefix and the corresponding state for
each symbol in the viable prefix
Building the Goto Table
If Goto(Ii, A) = Ij
Then Goto[i, A] = j
Meaning
When a reduction X taken place
The non-terminal X is added to the stack replacing
What should the state be after adding X
This information is kept in Goto table
Building the Parsing Table -- Example
Follow(S) = {$}
Follow(E) = {+, ), $}
Follow(T) = {+, ), $}
+ id ( ) $ S E T
0 4 5 1 2 3
1 Acc
2 6 SE
3 ET ET ET
4 Tid Tid Tid
5 4 5 7 3
6 4 5 8
7 6 9
8 EE+T EE+T EE+T
9 T(E) T(E) T(E)
LR Parsing Algorithm
Elements
Parser, parsing tables, stack, input
Initialization
Append the $ at the end of the input
Push state 0 into the stack
On the top of the stack, it is always a state
It is the current state of parsing
LR Parsing Algorithm
Steps
If Action[x, a] = y
x is the current state, on the top of the stack
a is the input token
Then shift a into the stack and put y on top of the stack
If Action[x, a] = A
Note that a is in Follow(A)
Then
x is the current state, on the top of the stack
Pop the handle and all the state corresponding to out of the stack
y is the state on the top of the stack after popping
Check Goto table, if Goto[y, A] = z
Push A and then z into the stack
LR Parsing - Example
Stack Input Action
+ id ( ) $ S E T 0 id + id $ S4
0 4 5 1 2 3 0 id 4 + id $ Tid,
Rightmost derivation:
1S E E + T E + id Acc Goto[0,T]=3
T + id id + id
2 6 SE 0T3 + id $ ET,
Reverse trace back: Goto[0,E]=2
3 ET ET ET
Reduce left most input first.
4 Tid Tid Tid 0E2 + id $ s6
5 4 5 7 3 0E2+6 id $ S4
6 4 5 8 0 E 2 + 6 id 4 $ Tid,
Goto[6,T]=8
7 6 9
0E2+6T8 $ EE+T,
8 EE+ EE+T EE+T
T Goto[0,E]=2
9 T(E) T(E) T(E) 0E2 $ SE,
Goto[0,S]=1
0S1 $ accept