0% found this document useful (0 votes)
8 views

Syntax Analysis 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Syntax Analysis 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

1

Syntax Analysis
Part II
2

Bottom-Up Parsing
• LR methods (Left-to-right, Rightmost
derivation)
– SLR, Canonical LR, LALR
• Other special cases:
– Shift-reduce parsing
– Operator-precedence parsing
3

Shift-Reduce Parsing
Grammar:
S→aABe
A→Abc|b
B→d

w=a b b c d e
4

Shift-Reduce Parsing
Grammar: Reducing a sentence: Shift-reduce corresponds
S→aABe abbcde to a rightmost derivation:
A→Abc|b aAbcde S rm a A B e
B→d aAde rm a A d e
aABe rm a A b c d e
These match S rm a b b c d e
production’s
right-hand sides
S
A A A
A A A B A B
a b b c d e a b b c d e a b b c d e a b b c d e
5

Handles
A handle is a substring of grammar symbols in a
right-sentential form that matches a right-hand side
of a production
Grammar: abbcde
S→aABe aAbcde
A→Abc|b aAde Handle
B→d aABe
S
abbcde
aAbcde NOT a handle, because
aAAe further reductions will fail
…? (result is not a sentential form)
6

Stack Implementation of
Shift-Reduce Parsing
Stack Input Action
$ id+id*id$ shift
$id +id*id$ reduce E → id How to
Grammar: $E +id*id$ shift
$E+ id*id$ shift
resolve
E→E+E conflicts?
$E+id *id$ reduce E → id
E→E*E $E+E *id$ shift (or reduce?)
E→(E) $E+E* id$ shift
E → id $E+E*id $ reduce E → id
$E+E*E $ reduce E → E * E
$E+E $ reduce E → E + E
Found handles $E $ accept
to reduce
7

Conflicts
• Shift-reduce and reduce-reduce conflicts are
caused by
– The limitations of the LR parsing method (even
when the grammar is unambiguous)
– Ambiguity of the grammar
8

Shift-Reduce Parsing:
Shift-Reduce Conflicts
Stack Input Action
$… …$ …
$…if E then S else…$ shift or reduce?
Ambiguous grammar:
S → if E then S
| if E then S else S
| other

Resolve in favor
of shift, so else
matches closest if
9

Shift-Reduce Parsing:
Reduce-Reduce Conflicts
Stack Input Action
$ aa$ shift
$a a$ reduce A → a or B → a ?
Grammar:
C→AB
A→a
B→a

Resolve in favor
of reducing A → a,
otherwise we’re stuck!
10

LR(k) Parsers: Use a DFA for


Shift/Reduce Decisions
1
C 4
B
start A
0 2 State I1:
a State I4:
a 5 S → C• C → A B•
goto(I0,C)
3
Grammar: goto(I2,B)
State I0: State I2:
S→C goto(I ,A)
S → •C 0
C→AB C → A•B
C → •A B
A→a B → •a goto(I2,a)
A → •a
B→a
Can only goto(I0,a) State I5:
State I3:
reduce A → a B → a•
A → a•
(not B → a)
11

DFA for Shift/Reduce Decisions


The states of the DFA are used to determine
Grammar: if a handle is on top of the stack
S→C
C→AB Stack Input Action
$0 aa$ start in state 0
A→a $0 aa$ shift (and goto state 3)
B→a $0a3 a$ reduce A → a (goto 2)
$0A2 a$ shift (goto 5)
State I0: goto(I0,a) $0A2a5 $ reduce B → a (goto 4)
S → •C $0A2B4 $ reduce C → AB (goto 1)
State I3: $0C1 $ accept (S → C)
C → •A B A → a•
A → •a
12

DFA for Shift/Reduce Decisions


The states of the DFA are used to determine
Grammar: if a handle is on top of the stack
S→C
C→AB Stack Input Action
$0 aa$ start in state 0
A→a $0 aa$ shift (and goto state 3)
B→a $0a3 a$ reduce A → a (goto 2)
$0A2 a$ shift (goto 5)
$0A2a5 $ reduce B → a (goto 4)
State I0: goto(I0,A) $0A2B4 $ reduce C → AB (goto 1)
S → •C State I2: $0C1 $ accept (S → C)
C → •A B C → A•B
A → •a B → •a
13

DFA for Shift/Reduce Decisions


The states of the DFA are used to determine
Grammar: if a handle is on top of the stack
S→C
C→AB Stack Input Action
$0 aa$ start in state 0
A→a $0 aa$ shift (and goto state 3)
B→a $0a3 a$ reduce A → a (goto 2)
$0A2 a$ shift (goto 5)
$0A2a5 $ reduce B → a (goto 4)
$0A2B4 $ reduce C → AB (goto 1)
goto(I2,a) $0C1 $ accept (S → C)
State I2:
C → A•B State I5:
B → •a B → a•
14

DFA for Shift/Reduce Decisions


The states of the DFA are used to determine
Grammar: if a handle is on top of the stack
S→C
C→AB Stack Input Action
$0 aa$ start in state 0
A→a $0 aa$ shift (and goto state 3)
B→a $0a3 a$ reduce A → a (goto 2)
$0A2 a$ shift (goto 5)
$0A2a5 $ reduce B → a (goto 4)
$0A2B4 $ reduce C → AB (goto 1)
goto(I2,B) $0C1 $ accept (S → C)
State I2:
C → A•B State I4:
B → •a C → A B•
15

DFA for Shift/Reduce Decisions


The states of the DFA are used to determine
Grammar: if a handle is on top of the stack
S→C
C→AB Stack Input Action
$0 aa$ start in state 0
A→a $0 aa$ shift (and goto state 3)
B→a $0a3 a$ reduce A → a (goto 2)
$0A2 a$ shift (goto 5)
$0A2a5 $ reduce B → a (goto 4)
$0A2B4 $ reduce C → AB (goto 1)
goto(I0,C) $0C1 $ accept (S → C)
State I0:
S → •C State I1:
C → •A B S → C•
A → •a
16

DFA for Shift/Reduce Decisions


The states of the DFA are used to determine
Grammar: if a handle is on top of the stack
S→C
C→AB Stack Input Action
$0 aa$ start in state 0
A→a $0 aa$ shift (and goto state 3)
B→a $0a3 a$ reduce A → a (goto 2)
$0A2 a$ shift (goto 5)
$0A2a5 $ reduce B → a (goto 4)
$0A2B4 $ reduce C → AB (goto 1)
goto(I0,C) $0C1 $ accept (S → C)
State I0:
S → •C State I1:
C → •A B S → C•
A → •a
17

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 method
error
18

LR Parsing (Driver)
Configuration ( = LR parser state):
(s0 X1 s1 X2 s2 … Xm sm, ai ai+1 … an $)

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 $)

If action[sm,ai] = reduce A →  and goto[sm-r,A] = s with r=|| then


pop 2r symbols, push A, and push s:
(s0 X1 s1 X2 s2 … Xm-r sm-r A s, ai ai+1 … an $)
If action[sm,ai] = accept then stop
If action[sm,ai] = error then attempt recovery
19

Example LR(0) Parsing Table


State I0: State I1: State I2: State I3: State I4: State I5:
C’ → •C C’ → C• C → A•B A → a• C → A B• B → a•
C → •A B B → •a
A → •a
action goto
Shift & goto 3 state a $ C A B
1 0 s3 1 2
Grammar:
C 4 1 acc
B 1. C’ → C
start A 2 s5 4
0 2 2. C → A B
a 3 r3 r3 3. A → a
a 5
4 r2 r2 4. B → a
3 Reduce by 5 r4 r4
production #2
20

Another Example LR Parse Table


action goto
Grammar: state id + * ( ) $ E T F
1. E → E + T 0 s5 s4 1 2 3
2. E → T 1 s6 acc
3. T → T * F
2 r2 s7 r2 r2
4. T → F
3 r4 r4 r4 r4
5. F → ( E )
4 s5 s4 8 2 3
6. F → id
5 r6 r6 r6 r6
6 s5 s4 9 3
Shift & goto 5 7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
Reduce by
10 r3 r3 r3 r3
production #1
11 r5 r5 r5 r5
21
22

Example LR Shift-Reduce
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
23

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,+)
S→E
E → id•
E → id + E E → •id + E
E → id E → •id FOLLOW(E)={$}
thus reduce on $
24

SLR Parsing Table


• Reductions do not fill entire rows
• Otherwise the same as LR(0)
id + $ E
0 s2 1
1. S → E 1 acc
2. E → id + E 2 s3 r3
3. E → id
3 s2 4
4 r2
Shift on +
FOLLOW(E)={$}
thus reduce on $
25

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
26

Constructing SLR Parsing Tables


1. Augment the grammar with S’→S
2. Construct the set C={I0,I1,…,In} of LR(0) items
3. If [A→•a]  Ii and goto(Ii,a)=Ij then set
action[i,a]=shift j
4. If [A→•]  Ii then set action[i,a]=reduce A→
for all a  FOLLOW(A) (apply only if AS’)
5. If [S’→S•] is in Ii then set action[i,$]=accept
6. If goto(Ii,A)=Ij then set goto[i,A]=j
7. Repeat 3-6 until no more entries added
8. The initial state i is the Ii holding item [S’→•S]
27

LR(0) Items of a Grammar


• An LR(0) item of a grammar G is a production of
G with a • at some position of the right-hand side
• Thus, a production
A→XYZ
has four items:
[A → • X Y Z]
[A → X • Y Z]
[A → X Y • Z]
[A → X Y Z •]
• Note that production A →  has one item [A → •]
28

Constructing the set of LR(0)


Items of a Grammar
1. The grammar is augmented with a new start
symbol S’ and production S’→S
2. Initially, set C = closure({[S’→•S]})
(this is the start state of the DFA)
3. For each set of items I  C and each grammar
symbol X  (NT) such that goto(I,X)  C and
goto(I,X)  , add the set of items goto(I,X) to C
4. Repeat 3 until no more sets can be added to C
29

The Closure Operation for LR(0)


Items
1. Start with closure(I) = I
2. If [A→•B]  closure(I) then for each
production B→ in the grammar, add the
item [B→•] to I if not already in I
3. Repeat 2 until no new items can be added
30

SLR parsing table


for the Grammar:
1. E → E + T
2. E → T
3. T → T * F
4. T → F
5. F → ( E )
6. F → id
31

Another Example LR Parse Table


action goto
Grammar: state id + * ( ) $ E T F
1. E → E + T 0 s5 s4 1 2 3
2. E → T 1 s6 acc
3. T → T * F
2 r2 s7 r2 r2
4. T → F
3 r4 r4 r4 r4
5. F → ( E )
4 s5 s4 8 2 3
6. F → id
5 r6 r6 r6 r6
6 s5 s4 9 3
Shift & goto 5 7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
Reduce by
10 r3 r3 r3 r3
production #1
11 r5 r5 r5 r5
32

The Closure Operation


(Example)
closure({[E’ → •E]}) =
{ [E’ → • E] } { [E’ → • E] { [E’ → • E] { [E’ → • E]
[E → • E + T] [E → • E + T] [E → • E + T]
[E → • T] } [E → • T] [E → • T]
[T → • T * F] [T → • T * F]
Add [E→•] [T → • F] } [T → • F]
Add [T→•] [F → • ( E )]
Grammar: Add [F→•] [F → • id] }
E→E+T|T
T→T*F|F
F→(E)
F → id
33

The Goto Operation for LR(0)


Items
1. For each item [A→•X]  I, add the set
of items closure({[A→X•]}) to
goto(I,X) if not already there
2. Repeat step 1 until no more items can be
added to goto(I,X)
3. Intuitively, goto(I,X) is the set of items
that are valid for the viable prefix X when
I is the set of items that are valid for 
34

The Goto Operation (Example 1)

Suppose I = { [E’ → • E] Then goto(I,E)


[E → • E + T] = closure({[E’ → E •, E → E • + T]})
[E → • T] = { [E’ → E •]
[T → • T * F] [E → E • + T] }
[T → • F]
[F → • ( E )]
[F → • id] } Grammar:
E→E+T|T
T→T*F|F
F→(E)
F → id
35

The Goto Operation (Example 2)

Suppose I = { [E’ → E •], [E → E • + T] }

Then goto(I,+) = closure({[E → E + • T]}) = { [E → E + • T]


[T → • T * F]
[T → • F]
[F → • ( E )]
[F → • id] }
Grammar:
E→E+T|T
T→T*F|F
F→(E)
F → id
36

Example Grammar and LR(0)


Items
Augmented I0 = closure({[C’ → •C]})
grammar: I1 = goto(I0,C) = closure({[C’ → C•]})
1. C’ → C …
2. C → A B State I1:
final State I4:
3. A → a C’ → C•
goto(I0,C) C → A B•
4. B → a
State I0: goto(I2,B)
State I2:
start C’ → •C goto(I 0 ,A)
C → A•B
C → •A B B → •a goto(I2,a)
A → •a
goto(I0,a) State I5:
State I3:
B → a•
A → a•
37

Example SLR Parsing Table


State I0: State I1: State I2: State I3: State I4: State I5:
C’ → •C C’ → C• C → A•B A → a• C → A B• B → a•
C → •A B B → •a
A → •a Grammar:
action goto
state a $ C A B 1. C’ → C
0 s3 1 2
2. C → A B
1
3. A → a
C 4 1 acc
start
B 4. B → a
A 2 s5 4
0 2 FOLLOW(A) = {a}
a 3 r3
a 5 FOLLOW(C) = {$}
4 r2
3 5 r4 FOLLOW(B) = {$}
38

SLR, Ambiguity, and Conflicts


• SLR grammars are unambiguous
• But not every unambiguous grammar is
SLR
• Consider for example the unambiguous
grammar
1. S → L = R 2. S → R
3. L → * R 4. L → id
5. R → L
0. S’ →S
39
1. S → L = R
2. S → R
3. L → * R
4. L → id
5. R → L
40

SLR, Ambiguity, and Conflicts


• SLR grammars are unambiguous
• But not every unambiguous grammar is SLR
• Consider for example the unambiguous grammar
1. S → L = R 2. S → R
3. L → * R 4. L → id
5. R → L
I0: I1: I2: I3: I4: I5: I6: I7:
S’ → •S S’ → S• S → L•=R S → R• L → *•R L → id• S → L=•R L → *R•
S → •L=R R → L• R → •L R → •L
S → •R L → •*R L → •*R I8:
L → •*R L → •id L → •id R → L•
L → •id
action[2,=]=s6 Conflict: has no SLR I9:
R → •L
no S → L=R•
action[2,=]=r5 parsing table!
41

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]
42

SLR Versus LR(1)


• Split the SLR states by
adding LR(1) lookahead I2:
S → L•=R split
• Unambiguous grammar R → L•

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=
43

LR(1) Items
• An LR(1) item
[A→•, a]
contains a lookahead terminal a, meaning 
already on top of the stack, expect to parse a
• For items of the form
[A→•, a]
the lookahead a is used to reduce A→ only if the
next lookahead of the input is a
• For items of the form
[A→•, a]
with  the lookahead has no effect
44

The Closure Operation for LR(1)


Items
1. Start with closure(I) = I
2. If [A→•B, a]  closure(I) then for each
production B→ in the grammar and each
terminal b  FIRST(a), add the item
[B→•, b] to I if not already in I
3. Repeat 2 until no new items can be added
45

The Goto Operation for LR(1)


Items
1. For each item [A→•X, a]  I, add the
set of items closure({[A→X•, a]}) to
goto(I,X) if not already there
2. Repeat step 1 until no more items can be
added to goto(I,X)
46

Constructing the set of LR(1)


Items of a Grammar
1. Augment the grammar with a new start symbol
S’ and production S’→S
2. Initially, set C = closure({[S’→•S, $]})
(this is the start state of the DFA)
3. For each set of items I  C and each grammar
symbol X  (NT) such that goto(I,X)  C and
goto(I,X)  , add the set of items goto(I,X) to C
4. Repeat 3 until no more sets can be added to C
47

Example Grammar and LR(1) Items


0. S’ → S
1. S → CC
2. C → cC
3. C → d
48
49
50
51
52
53
54

Example Grammar and LR(1)


Items
• Unambiguous LR(1) grammar:
S→L=R
S→R
L→*R
L → id
R→L
• Augment with S’ → S
• LR(1) items (next slide)
55
I0: [S’ → •S, $] goto(I0,S)=I1 I6: [S → L=•R, $] goto(I6,R)=I9
[S → •L=R, $] goto(I0,L)=I2 [R → •L, $] goto(I6,L)=I10
[S → •R, $] goto(I0,R)=I3 [L → •*R, $] goto(I6,*)=I11
[L → •*R, =/$] goto(I0,*)=I4 [L → •id, $] goto(I6,id)=I12
[L → •id, =/$] goto(I0,id)=I5
[R → •L, $] goto(I0,L)=I2 I7: [L → *R•, =/$]

I1: [S’ → S•, $] I8: [R → L•, =/$]

I2: [S → L•=R, $] goto(I0,=)=I6 I9: [S → L=R•, $]


[R → L•, $]
I10: [R → L•, $]
I3: [S → R•, $]
I11: [L → *•R, $] goto(I11,R)=I13
I4: [L → *•R, =/$] goto(I4,R)=I7 [R → •L, $] goto(I11,L)=I10
[R → •L, =/$] goto(I4,L)=I8 [L → •*R, $] goto(I11,*)=I11
[L → •*R, =/$] goto(I4,*)=I4 [L → •id, $] goto(I11,id)=I12
[L → •id, =/$] goto(I4,id)=I5
I12: [L → id•, $]
I5: [L → id•, =/$]
I13: [L → *R•, $]
56

Constructing Canonical LR(1)


Parsing Tables
1. Augment the grammar with S’→S
2. Construct the set C={I0,I1,…,In} of LR(1) items
3. If [A→•a, b]  Ii and goto(Ii,a)=Ij then set
action[i,a]=shift j
4. If [A→•, a]  Ii then set action[i,a]=reduce
A→ (apply only if AS’)
5. If [S’→S•, $] is in Ii then set action[i,$]=accept
6. If goto(Ii,A)=Ij then set goto[i,A]=j
7. Repeat 3-6 until no more entries added
8. The initial state i is the Ii holding item
[S’→•S,$]
57

Example LR(1) Parsing Table


id * = $ S L R
0 s5 s4 1 2 3
1 acc
2 s6 r6

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
58

LALR Parsing
• LR(1) parsing tables have many states
• LALR parsing (Look-Ahead LR) merges two or
more LR(1) state into one state 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
59

Constructing LALR Parsing


Tables
1. Construct sets of LR(1) items
2. Combine LR(1) sets with sets of items that
share the same first part
I4: [L → *•R, =/$]
[R → •L, =/$] Shorthand
[L → •*R, =/$] for two items
[L → *•R, =/$]
[L → •id, =/$]
[R → •L, =/$] in the same set
[L → •*R, =/$]
I11: [L → *•R, $]
[L → •id, =/$]
[R → •L, $]
[L → •*R, $]
[L → •id, $]
60

Example Grammar and LALR


Parsing Table
• Unambiguous LR(1) grammar:
S→L=R
|R
L→*R
| id
R→L
• Augment with S’ → S
• LALR items (next slide)
61
I0: [S’ → •S, $] goto(I0,S)=I1 I6: [S → L=•R, $] goto(I6,R)=I8
[S → •L=R, $] goto(I0,L)=I2 [R → •L, $] goto(I6,L)=I9
[S → •R, $] goto(I0,R)=I3 [L → •*R, $] goto(I6,*)=I4
[L → •*R, =/$] goto(I0,*)=I4 [L → •id, $] goto(I6,id)=I5
[L → •id, =/$] goto(I0,id)=I5
[R → •L, $] goto(I0,L)=I2 I7: [L → *R•, =/$]

I1: [S’ → S•, $] I8: [S → L=R•, $]

I2: [S → L•=R, $] goto(I0,=)=I6 I9: [R → L•, =/$]


[R → L•, $]
Shorthand
I3: [S → R•, $]
for two items
I4: [L → *•R, =/$] goto(I4,R)=I7 [R → L•, =]
[R → •L, =/$] goto(I4,L)=I9 [R → L•, $]
[L → •*R, =/$] goto(I4,*)=I4
[L → •id, =/$] goto(I4,id)=I5

I5: [L → id•, =/$]


62

Example LALR Parsing Table

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
63

LL, SLR, LR, LALR Summary


• LL parse tables computed using FIRST/FOLLOW
– Nonterminals  terminals → productions
– Computed using FIRST/FOLLOW
• LR parsing tables computed using closure/goto
– LR states  terminals → shift/reduce actions
– LR states  nonterminals → goto state transitions
• A grammar is
– LL(1) if its LL(1) parse table has no conflicts
– SLR if its SLR parse table has no conflicts
– LALR if its LALR parse table has no conflicts
– LR(1) if its LR(1) parse table has no conflicts
64

LL, SLR, LR, LALR Grammars

LR(1)

LALR
LL(1) SLR

LR(0)
65

Dealing with Ambiguous


Grammars
stack input
1. S’ → E id + $ E $0 id+id+id$
2. E → E + E 0 s2 1 … …
3. E → id 1 s3 acc $0E1+3E4 +id$
2 r3 r3
3 s2 4   
4 s3/r2 r2
When shifting on +:
yields right associativity
id+(id+id)
Shift/reduce conflict: When reducing on +:
action[4,+] = shift 4 yields left associativity
action[4,+] = reduce E → E + E (id+id)+id
66

Using Associativity and


Precedence to Resolve Conflicts
• Left-associative operators: reduce
• Right-associative operators: shift
• Operator of higher precedence on stack: reduce
• Operator of lower precedence on stack: shift

stack input
$0 id*id+id$
S’ → E
E→E+E … …
E→E*E $0E1*3E5 +id$ reduce E → E * E
E → id
  
67

Error Detection in LR Parsing


• Canonical LR parser uses full LR(1) parse
tables and will never make a single
reduction before recognizing the error when
a syntax error occurs on the input
• SLR and LALR may still reduce when a
syntax error occurs on the input, but will
never shift the erroneous input symbol
68

Error Recovery in LR Parsing


• Panic mode
– Pop until state with a goto on a nonterminal A is found,
(where A represents a major programming construct),
push A
– Discard input symbols until one is found in the
FOLLOW set of A
• Phrase-level recovery
– Implement error routines for every error entry in table
• Error productions
– Pop until state has error production, then shift on stack
– Discard input until symbol is encountered that allows
parsing to continue
69
70

You might also like