Professional Documents
Culture Documents
Model of an LR Parser
input a1 a2 … ai … an $
stack
LR Parsing Program
sm (driver) output
Xm
sm-1
Xm-1 action goto Constructed with
… LR(0) method,
shift DFA SLR method,
s0 reduce LR(1) method, or
accept LALR(1) method
error
2
LR Parsing (Driver)
Configuration ( = LR parser state):
stack input
If action[sm,ai] = shift s then push ai, push s, and advance input:
(s0 X1 s1 X2 s2 … Xm sm ai s, ai+1 … an $)
Example LR Parsing
Stack Input Action
$0 id*id+id$ shift 5
$ 0 id 5 *id+id$ reduce 6 goto 3
Grammar: $0F3 *id+id$ reduce 4 goto 2
1. E E + T $0T2 *id+id$ shift 7
$0T2*7 id+id$ shift 5
2. E T $ 0 T 2 * 7 id 5 +id$ reduce 6 goto 10
3. T T * F $ 0 T 2 * 7 F 10 +id$ reduce 3 goto 2
4. T F $0T2 +id$ reduce 2 goto 1
5. F ( E ) $0E1 +id$ shift 6
6. F id $0E1+6 id$ shift 5
$ 0 E 1 + 6 id 5 $ reduce 6 goto 3
$0E1+6F3 $ reduce 4 goto 9
$0E1+6T9 $ reduce 1 goto 1
$0E1 $ accept
5
SLR Grammars
• SLR (Simple LR): SLR is a simple
extension of LR(0) shift-reduce parsing
• SLR eliminates some conflicts by
populating the parsing table with reductions
A on symbols in FOLLOW(A)
Shift on +
State I2:
State I0:
S •E goto(I0,id) E id•+ E goto(I3,+)
SE
E id•
E id + E E •id + E
E id E •id FOLLOW(E)={$}
thus reduce on $
6
SLR Parsing
• An LR(0) state is a set of LR(0) items
• An LR(0) item is a production with a • (dot) in the
right-hand side
• Build the LR(0) DFA by
– Closure operation to construct LR(0) items
– Goto operation to determine transitions
• Construct the SLR parsing table from the DFA
• LR parser program uses the SLR parsing table to
determine shift/reduce operations
8
LR(1) Grammars
• SLR too simple
• LR(1) parsing uses lookahead to avoid
unnecessary conflicts in parsing table
• LR(1) item = LR(0) item + lookahead
LR(0) item: LR(1) item:
[A•] [A•, a]
27
1. S L = R
2. S R S L•=R
lookahead=$
R L•
3. L * R
4. L id
action[2,=]=s6 action[2,$]=r5
5. R L
Should not reduce on =, because no
right-sentential form begins with R=
28
LR(1) Items
• An LR(1) item
[A•, a]
contains a lookahead terminal a, meaning
already on top of the stack, expect to see a
• For items of the form
[A•, a]
the lookahead a is used to reduce A only if the
next input is a
• For items of the form
[A•, a]
with the lookahead has no effect
29
Grammar: 3 r3
1. S’ S 4 s5 s4 8 7
2. S L = R 5 r5 r5
3. S R 6 s12 s11 10 9
4. L * R 7 r4 r4
5. L id 8 r6 r6
6. R L 9 r2
10 r6
11 s12 s11 10 13
12 r5
13 r4
36
LALR(1) Grammars
• LR(1) parsing tables have many states
• LALR(1) parsing (Look-Ahead LR) combines two
or more LR(1) sets into one to reduce table size
• Less powerful than LR(1)
– Will not introduce shift-reduce conflicts, because shifts
do not use lookaheads
– May introduce reduce-reduce conflicts, but seldom do
so for grammars of programming languages
37
id * = $ S L R
0 s5 s4 1 2 3
Grammar: 1 acc
1. S’ S 2 s6 r6
2. S L = R 3 r3
3. S R 4 s5 s4 9 7
4. L * R 5 r5 r5
5. L id 6 s5 s4 9 8
6. R L 7 r4 r4
8 r2
9 r6 r6
41
LR(1)
LALR(1)
LL(1) SLR
LR(0)
43
stack input
$0 id*id+id$
S’ E
EE+E … …
EE*E $0E1*3E5 +id$ reduce E E * E
E id
45