D LR Parsing
D LR Parsing
LR Parsing
Hal Perkins
Autumn 2010
S ::= aAB e
A ::= Abc | b
B ::= d
a b b c d e
10/10/2010 © 2002-10 Hal Perkins & UW CSE D-5
Details
The bottom-up parser reconstructs a reverse
rightmost derivation
Given the rightmost derivation
S =>β1=>β2=>…=>βn-2=>βn-1=>βn = w
the parser will first discover βn-1=>βn , then
βn-2=>βn-1 , etc.
Parsing terminates when
β1 reduced to S (start symbol, success), or
No match can be found (syntax error)
e
accept
8 9 S ::= aABe
$ B
start a A b c
1 2 3 6 7 A ::= Abc
b d
4 5
A ::= b B ::= d
accept e
8 9 S ::= aABe
Stack Input B
$ abbcde$ start
$
a A b c
1 2 3 6 7 A ::= Abc
b d
4 5
A ::= b B ::= d
accept e
8 9 S ::= aABe
$ B
start a A b c
1 2 3 6 7 A ::= Abc
b d
4 5
A ::= b B ::= d
action goto
Stack Input S
a b c d e $ A B S
$ abbcde$
1 s2 ac g1
2 s4 g3
3 s6 s5 g8
4 r3 r3 r3 r3 r3 r3
5 r4 r4 r4 r4 r4 r4
6 s7
7 r2 r2 r2 r2 r2 r2
8 s9
9 r1 r1 r1 r1 r1 r1
1 8 9
$ e S ::= aABe.
S ::= .aABe accept S ::= aAB.e
B
a
2 3
S ::= a.ABe S ::= aA.Be 6
A b
A ::= .Abc A ::= A.bc A ::= Ab.c
A ::= .b B ::= .d c
7
b d
4 5 A ::= Abc.
A ::= b. B ::= d.
S ::= . ifthen S
1
S ::= . ifthen S else S State 3 has a shift-
ifthen reduce conflict
2 S ::= ifthen . S Can shift past else
S ::= ifthen . S else S into state 4 (s4)
S
Can reduce (r1)
3 S ::= ifthen S . S ::= ifthen S
S ::= ifthen S . else S
else
4 S ::= ifthen S else . S (Note: other S ::= . ifthen
items not included in states
2-4 to save space)