SlideShare a Scribd company logo
1
1
Syntax Analysis
Part II
Chapter 4
COP5621 Compiler Construction
Copyright Robert van Engelen, Florida State University, 2007
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
Operator-Precedence Parsing
• Special case of shift-reduce parsing
• We will not further discuss (you can skip
textbook section 4.6)
2
4
Shift-Reduce Parsing
Grammar:
S → a A B e
A → A b c | b
B → d
Shift-reduce corresponds
to a rightmost derivation:
S ⇒rm a A B e
⇒rm a A d e
⇒rm a A b c d e
⇒rm a b b c d e
Reducing a sentence:
a b b c d e
a A b c d e
a A d e
a A B e
S
S
a b b c d e
A
A
B
a b b c d e
A
A
B
a b b c d e
A
A
a b b c d e
A
These match
production’s
right-hand sides
5
Handles
Handle
Grammar:
S → a A B e
A → A b c | b
B → d
A handle is a substring of grammar symbols in a
right-sentential form that matches a right-hand side
of a production
NOT a handle, because
further reductions will fail
(result is not a sentential form)
a b b c d e
a A b c d e
a A A e
… ?
a b b c d e
a A b c d e
a A d e
a A B e
S
6
Stack Implementation of
Shift-Reduce Parsing
Stack
$
$id
$E
$E+
$E+id
$E+E
$E+E*
$E+E*id
$E+E*E
$E+E
$E
Input
id+id*id$
+id*id$
+id*id$
id*id$
*id$
*id$
id$
$
$
$
$
Action
shift
reduce E → id
shift
shift
reduce E → id
shift (or reduce?)
shift
reduce E → id
reduce E → E * E
reduce E → E + E
accept
Grammar:
E → E + E
E → E * E
E → ( E )
E → id
Find handles
to reduce
How to
resolve
conflicts?
`
3
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
$…
$…if E then S
Input
…$
else…$
Action
…
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
$
$a
Input
aa$
a$
Action
shift
reduce A → a or B → a ?
Grammar:
C → A B
A → a
B → a
Resolve in favor
of reduce A → a,
otherwise we’re stuck!
4
10
LR(k) Parsers: Use a DFA for
Shift/Reduce Decisions
1
2
4
5
3
0
start
a
A
C
B
a
Grammar:
S → C
C → A B
A → a
B → a
State I0:
S → •C
C → •A B
A → •a
State I1:
S → C•
State I2:
C → A•B
B → •a
State I3:
A → a•
State I4:
C → A B•
State I5:
B → a•
goto(I0,C)
goto(I0,a)
goto(I0,A)
goto(I2,a)
goto(I2,B)
Can only
reduce A → a
(not B → a)
11
DFA for Shift/Reduce Decisions
Stack
$ 0
$ 0
$ 0 a 3
$ 0 A 2
$ 0 A 2 a 5
$ 0 A 2 B 4
$ 0 C 1
Input
aa$
aa$
a$
a$
$
$
$
Action
start in state 0
shift (and goto state 3)
reduce A → a (goto 2)
shift (goto 5)
reduce B → a (goto 4)
reduce C → AB (goto 1)
accept (S → C)
Grammar:
S → C
C → A B
A → a
B → a
The states of the DFA are used to determine
if a handle is on top of the stack
State I0:
S → •C
C → •A B
A → •a
State I3:
A → a•
goto(I0,a)
12
DFA for Shift/Reduce Decisions
Stack
$ 0
$ 0
$ 0 a 3
$ 0 A 2
$ 0 A 2 a 5
$ 0 A 2 B 4
$ 0 C 1
Input
aa$
aa$
a$
a$
$
$
$
Action
start in state 0
shift (and goto state 3)
reduce A → a (goto 2)
shift (goto 5)
reduce B → a (goto 4)
reduce C → AB (goto 1)
accept (S → C)
Grammar:
S → C
C → A B
A → a
B → a
The states of the DFA are used to determine
if a handle is on top of the stack
State I0:
S → •C
C → •A B
A → •a
State I2:
C → A•B
B → •a
goto(I0,A)
5
13
DFA for Shift/Reduce Decisions
Stack
$ 0
$ 0
$ 0 a 3
$ 0 A 2
$ 0 A 2 a 5
$ 0 A 2 B 4
$ 0 C 1
Input
aa$
aa$
a$
a$
$
$
$
Action
start in state 0
shift (and goto state 3)
reduce A → a (goto 2)
shift (goto 5)
reduce B → a (goto 4)
reduce C → AB (goto 1)
accept (S → C)
Grammar:
S → C
C → A B
A → a
B → a
The states of the DFA are used to determine
if a handle is on top of the stack
State I2:
C → A•B
B → •a
State I5:
B → a•
goto(I2,a)
14
DFA for Shift/Reduce Decisions
Stack
$ 0
$ 0
$ 0 a 3
$ 0 A 2
$ 0 A 2 a 5
$ 0 A 2 B 4
$ 0 C 1
Input
aa$
aa$
a$
a$
$
$
$
Action
start in state 0
shift (and goto state 3)
reduce A → a (goto 2)
shift (goto 5)
reduce B → a (goto 4)
reduce C → AB (goto 1)
accept (S → C)
Grammar:
S → C
C → A B
A → a
B → a
The states of the DFA are used to determine
if a handle is on top of the stack
State I2:
C → A•B
B → •a
State I4:
C → A B•
goto(I2,B)
15
DFA for Shift/Reduce Decisions
Stack
$ 0
$ 0
$ 0 a 3
$ 0 A 2
$ 0 A 2 a 5
$ 0 A 2 B 4
$ 0 C 1
Input
aa$
aa$
a$
a$
$
$
$
Action
start in state 0
shift (and goto state 3)
reduce A → a (goto 2)
shift (goto 5)
reduce B → a (goto 4)
reduce C → AB (goto 1)
accept (S → C)
Grammar:
S → C
C → A B
A → a
B → a
The states of the DFA are used to determine
if a handle is on top of the stack
State I0:
S → •C
C → •A B
A → •a
State I1:
S → C•
goto(I0,C)
6
16
DFA for Shift/Reduce Decisions
Stack
$ 0
$ 0
$ 0 a 3
$ 0 A 2
$ 0 A 2 a 5
$ 0 A 2 B 4
$ 0 C 1
Input
aa$
aa$
a$
a$
$
$
$
Action
start in state 0
shift (and goto state 3)
reduce A → a (goto 2)
shift (goto 5)
reduce B → a (goto 4)
reduce C → AB (goto 1)
accept (S → C)
Grammar:
S → C
C → A B
A → a
B → a
The states of the DFA are used to determine
if a handle is on top of the stack
State I0:
S → •C
C → •A B
A → •a
State I1:
S → C•
goto(I0,C)
17
Model of an LR Parser
$
an
…
ai
…
a2
a1
LR Parsing Program
(driver)
goto
action
s0
…
Xm-1
sm-1
Xm
sm output
stack
input
DFA
shift
reduce
accept
error
Constructed with
LR(0) method,
SLR method,
LR(1) method, or
LALR(1) method
18
LR Parsing (Driver)
(s0 X1 s1 X2 s2 … Xm sm, ai ai+1 … an $)
stack input
Configuration ( = LR parser state):
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
7
19
Example LR Parse Table
Grammar:
1. E → E + T
2. E → T
3. T → T * F
4. T → F
5. F → ( E )
6. F → id
r5
r5
r5
r5
r3
r3
r3
r3
r1
r1
s7
r1
s11
s6
s4
s5
s4
s5
r6
r6
r6
r6
s4
s5
r4
r4
r4
r4
r2
r2
s7
r2
acc
s6
s4
s5
$
)
(
*
+
id
11
10
9
8
7
6
5
4
3
2
1
0
F
T
E
10
3
9
3
2
8
3
2
1
Shift & goto 5
Reduce by
production #1
action goto
state
20
Example LR Parsing
Stack
$ 0
$ 0 id 5
$ 0 F 3
$ 0 T 2
$ 0 T 2 * 7
$ 0 T 2 * 7 id 5
$ 0 T 2 * 7 F 10
$ 0 T 2
$ 0 E 1
$ 0 E 1 + 6
$ 0 E 1 + 6 id 5
$ 0 E 1 + 6 F 3
$ 0 E 1 + 6 T 9
$ 0 E 1
Input
id*id+id$
*id+id$
*id+id$
*id+id$
id+id$
+id$
+id$
+id$
+id$
id$
$
$
$
$
Action
shift 5
reduce 6 goto 3
reduce 4 goto 2
shift 7
shift 5
reduce 6 goto 10
reduce 3 goto 2
reduce 2 goto 1
shift 6
shift 5
reduce 6 goto 3
reduce 4 goto 9
reduce 1 goto 1
accept
Grammar:
1. E → E + T
2. E → T
3. T → T * F
4. T → F
5. F → ( E )
6. F → id
21
SLR Grammars
• SLR (Simple LR): 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)
S → E
E → id + E
E → id
State I0:
S → •E
E → •id + E
E → •id
State I2:
E → id•+ E
E → id•
goto(I0,id) goto(I3,+)
FOLLOW(E)={$}
thus reduce on $
Shift on +
8
22
SLR Parsing Table
r2
s2
r3
s3
acc
s2
$
+
id
4
3
2
1
0
E
4
1
1. S → E
2. E → id + E
3. E → id
FOLLOW(E)={$}
thus reduce on $
Shift on +
• Reductions do not fill entire rows
• Otherwise the same as LR(0)
23
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
24
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]
9
25
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 → X Y Z
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 → •]
26
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
27
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
10
28
The Closure Operation
(Example)
Grammar:
E → E + T | T
T → T * F | F
F → ( E )
F → id
{ [E’ → • E] }
closure({[E’ → •E]}) =
{ [E’ → • E]
[E → • E + T]
[E → • T] }
{ [E’ → • E]
[E → • E + T]
[E → • T]
[T → • T * F]
[T → • F] }
{ [E’ → • E]
[E → • E + T]
[E → • T]
[T → • T * F]
[T → • F]
[F → • ( E )]
[F → • id] }
Add [E→•γ]
Add [T→•γ]
Add [F→•γ]
29
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 γ
30
The Goto Operation (Example 1)
Suppose I = Then goto(I,E)
= closure({[E’ → E •, E → E • + T]})
= { [E’ → E •]
[E → E • + T] }
Grammar:
E → E + T | T
T → T * F | F
F → ( E )
F → id
{ [E’ → • E]
[E → • E + T]
[E → • T]
[T → • T * F]
[T → • F]
[F → • ( E )]
[F → • id] }
11
31
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
32
Example SLR Grammar and
LR(0) Items
Augmented
grammar:
1. C’ → C
2. C → A B
3. A → a
4. B → a
State I0:
C’ → •C
C → •A B
A → •a
State I1:
C’ → C•
State I2:
C → A•B
B → •a
State I3:
A → a•
State I4:
C → A B•
State I5:
B → a•
goto(I0,C)
goto(I0,a)
goto(I0,A)
goto(I2,a)
goto(I2,B)
I0 = closure({[C’ → •C]})
I1 = goto(I0,C) = closure({[C’ → C•]})
…
start
final
33
Example SLR Parsing Table
r4
r2
r3
s5
acc
s3
$
a
5
4
3
2
1
0
B
A
C
4
2
1
State I0:
C’ → •C
C → •A B
A → •a
State I1:
C’ → C•
State I2:
C → A•B
B → •a
State I3:
A → a•
State I4:
C → A B•
State I5:
B → a•
1
2
4
5
3
0
start
a
A
C
B
a
Grammar:
1. C’ → C
2. C → A B
3. A → a
4. B → a
12
34
SLR and Ambiguity
• Every SLR grammar is unambiguous, but not
every unambiguous grammar is SLR
• Consider for example the unambiguous grammar
S → L = R | R
L → * R | id
R → L
I0:
S’ → •S
S → •L=R
S → •R
L → •*R
L → •id
R → •L
I1:
S’ → S•
I2:
S → L•=R
R → L•
I3:
S → R•
I4:
L → *•R
R → •L
L → •*R
L → •id
I5:
L → id•
I6:
S → L=•R
R → •L
L → •*R
L → •id
I7:
L → *R•
I8:
R → L•
I9:
S → L=R•
action[2,=]=s6
action[2,=]=r5
no
Has no SLR
parsing table
35
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:
[A→α•β]
LR(1) item:
[A→α•β, a]
36
I2:
S → L•=R
R → L•
action[2,=]=s6
Should not reduce on =, because no
right-sentential form begins with R=
split
R → L•
S → L•=R
SLR Versus LR(1)
• Split the SLR states by
adding LR(1) lookahead
• Unambiguous grammar
1. S → L = R
2. | R
3. L → * R
4. | id
5. R → L
lookahead=$
action[2,$]=r5
13
37
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
38
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
39
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)
14
40
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
41
Example Grammar and LR(1)
Items
• Unambiguous LR(1) grammar:
S → L = R
| R
L → * R
| id
R → L
• Augment with S’ → S
• LR(1) items (next slide)
42
[S’ → •S, $] goto(I0,S)=I1
[S → •L=R, $] goto(I0,L)=I2
[S → •R, $] goto(I0,R)=I3
[L → •*R, =/$] goto(I0,*)=I4
[L → •id, =/$] goto(I0,id)=I5
[R → •L, $] goto(I0,L)=I2
[S’ → S•, $]
[S → L•=R, $] goto(I0,=)=I6
[R → L•, $]
[S → R•, $]
[L → *•R, =/$] goto(I4,R)=I7
[R → •L, =/$] goto(I4,L)=I8
[L → •*R, =/$] goto(I4,*)=I4
[L → •id, =/$] goto(I4,id)=I5
[L → id•, =/$]
[S → L=•R, $] goto(I6,R)=I9
[R → •L, $] goto(I6,L)=I10
[L → •*R, $] goto(I6,*)=I11
[L → •id, $] goto(I6,id)=I12
[L → *R•, =/$]
[R → L•, =/$]
[S → L=R•, $]
[R → L•, $]
[L → *•R, $] goto(I11,R)=I13
[R → •L, $] goto(I11,L)=I10
[L → •*R, $] goto(I11,*)=I11
[L → •id, $] goto(I11,id)=I12
[L → id•, $]
[L → *R•, $]
I0:
I1:
I2:
I3:
I4:
I5:
I6:
I7:
I8:
I9:
I10:
I12:
I11:
I13:
15
43
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,$]
44
Example LR(1) Parsing Table
r4
r5
s11
s12
r6
r2
r6
r6
r4
r4
s11
s12
r5
r5
s4
s5
r3
r6
s6
acc
s4
s5
$
=
*
id
13
12
11
10
9
8
7
6
5
4
3
2
1
0
R
L
S
13
10
4
10
7
8
3
2
1
Grammar:
1. S’ → S
2. S → L = R
3. S → R
4. L → * R
5. L → id
6. R → L
45
LALR(1) Grammars
• LR(1) parsing tables have many states
• LALR(1) parsing (Look-Ahead LR) combines
LR(1) states 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
16
46
Constructing LALR(1) Parsing
Tables
1. Construct sets of LR(1) items
2. Combine LR(1) sets with sets of items that
share the same first part
[L → *•R, =]
[R → •L, =]
[L → •*R, =]
[L → •id, =]
[L → *•R, $]
[R → •L, $]
[L → •*R, $]
[L → •id, $]
I4:
I11:
[L → *•R, =/$]
[R → •L, =/$]
[L → •*R, =/$]
[L → •id, =/$]
Shorthand
for two items
in the same set
47
Example LALR(1) Grammar
• Unambiguous LR(1) grammar:
S → L = R
| R
L → * R
| id
R → L
• Augment with S’ → S
• LALR(1) items (next slide)
48
[S’ → •S, $] goto(I0,S)=I1
[S → •L=R, $] goto(I0,L)=I2
[S → •R, $] goto(I0,R)=I3
[L → •*R, =/$] goto(I0,*)=I4
[L → •id, =/$] goto(I0,id)=I5
[R → •L, $] goto(I0,L)=I2
[S’ → S•, $]
[S → L•=R, $] goto(I0,=)=I6
[R → L•, $]
[S → R•, $]
[L → *•R, =/$] goto(I4,R)=I7
[R → •L, =/$] goto(I4,L)=I9
[L → •*R, =/$] goto(I4,*)=I4
[L → •id, =/$] goto(I4,id)=I5
[L → id•, =/$]
[S → L=•R, $] goto(I6,R)=I8
[R → •L, $] goto(I6,L)=I9
[L → •*R, $] goto(I6,*)=I4
[L → •id, $] goto(I6,id)=I5
[L → *R•, =/$]
[S → L=R•, $]
[R → L•, =/$]
I0:
I1:
I2:
I3:
I4:
I5:
I6:
I7:
I8:
I9:
Shorthand
for two items
[R → L•, =]
[R → L•, $]
17
49
Example LALR(1) Parsing Table
r6
r6
r2
r4
r4
s4
s5
r5
r5
s4
s5
r3
r6
s6
acc
s4
s5
$
=
*
id
9
8
7
6
5
4
3
2
1
0
R
L
S
8
9
7
9
3
2
1
Grammar:
1. S’ → S
2. S → L = R
3. S → R
4. L → * R
5. L → id
6. R → L
50
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(1) if its LALR(1) parse table has no conflicts
– LR(1) if its LR(1) parse table has no conflicts
51
LL, SLR, LR, LALR Grammars
LL(1)
LR(1)
LR(0)
SLR
LALR(1)
18
52
Dealing with Ambiguous
Grammars
1. S’ → E
2. E → E + E
3. E → id
r2
s3/r2
s2
r3
r3
acc
s3
s2
$
+
id
4
3
2
1
0
E
4
1
Shift/reduce conflict:
action[4,+] = shift 4
action[4,+] = reduce E → E + E
When reducing on +:
yields left associativity
(id+id)+id
When shifting on +:
yields right associativity
id+(id+id)
$ 0 E 1 + 3 E 4
id+id+id$
+id$
$ 0
… …
stack input
≈ ≈ ≈
53
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
S’ → E
E → E + E
E → E * E
E → id
$ 0 E 1 * 3 E 5
id*id+id$
+id$
$ 0
… …
stack input
reduce E → E * E
≈ ≈ ≈
54
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
19
55
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

More Related Content

Similar to Syntax Analysis (Bottom-Up Parser) PPTs for Third Year Computer Science Engineering (20)

PDF
Bottomupparser
Royalzig Luxury Furniture
 
PPTX
LR(1) and SLR(1) parsing
R Islam
 
PPT
LR-Parsing.ppt
BapanKar2
 
PPTX
Compiler Design Bottom Up Parsing Technique S
poojasharmabu
 
PPTX
Compiler Design Unit 2
Jena Catherine Bel D
 
PPT
BUP-1 (1).pptLec 11-BUP-1 (1).pptpptpptppt
Aliza530614
 
PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
PPTX
CD M2 Lect 4 - Bottom Up Parsers (1).pptx
tanilkumarreddy173
 
PPT
Chapter Five(2)
bolovv
 
PPTX
UNIT 2 (1).pptx
Abhishek Tirkey
 
PPTX
optimization of DFA
Maulik Togadiya
 
PPT
LR_Parser compiler how to compiler lr1 ll1
compengwaelalahmar
 
PPT
Lecture 15 16
Najmul Hassan
 
PPTX
sameermlr0parser-200701133032.pptx
BapanKar2
 
PPTX
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
PDF
LR0Examples.pdf LR(0) LR(1) it's my pres
MuqadasWajid004
 
PPT
CC Week 07-08.ppt
KamranAli649587
 
PDF
lalr. fo engineering student those who to
HjJordTzong
 
LR(1) and SLR(1) parsing
R Islam
 
LR-Parsing.ppt
BapanKar2
 
Compiler Design Bottom Up Parsing Technique S
poojasharmabu
 
Compiler Design Unit 2
Jena Catherine Bel D
 
BUP-1 (1).pptLec 11-BUP-1 (1).pptpptpptppt
Aliza530614
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
CD M2 Lect 4 - Bottom Up Parsers (1).pptx
tanilkumarreddy173
 
Chapter Five(2)
bolovv
 
UNIT 2 (1).pptx
Abhishek Tirkey
 
optimization of DFA
Maulik Togadiya
 
LR_Parser compiler how to compiler lr1 ll1
compengwaelalahmar
 
Lecture 15 16
Najmul Hassan
 
sameermlr0parser-200701133032.pptx
BapanKar2
 
Compiler Design LR parsing SLR ,LALR CLR
Riazul Islam
 
LR0Examples.pdf LR(0) LR(1) it's my pres
MuqadasWajid004
 
CC Week 07-08.ppt
KamranAli649587
 
lalr. fo engineering student those who to
HjJordTzong
 

Recently uploaded (20)

PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Day2 B2 Best.pptx
helenjenefa1
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Ad

Syntax Analysis (Bottom-Up Parser) PPTs for Third Year Computer Science Engineering

  • 1. 1 1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007 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 Operator-Precedence Parsing • Special case of shift-reduce parsing • We will not further discuss (you can skip textbook section 4.6)
  • 2. 2 4 Shift-Reduce Parsing Grammar: S → a A B e A → A b c | b B → d Shift-reduce corresponds to a rightmost derivation: S ⇒rm a A B e ⇒rm a A d e ⇒rm a A b c d e ⇒rm a b b c d e Reducing a sentence: a b b c d e a A b c d e a A d e a A B e S S a b b c d e A A B a b b c d e A A B a b b c d e A A a b b c d e A These match production’s right-hand sides 5 Handles Handle Grammar: S → a A B e A → A b c | b B → d A handle is a substring of grammar symbols in a right-sentential form that matches a right-hand side of a production NOT a handle, because further reductions will fail (result is not a sentential form) a b b c d e a A b c d e a A A e … ? a b b c d e a A b c d e a A d e a A B e S 6 Stack Implementation of Shift-Reduce Parsing Stack $ $id $E $E+ $E+id $E+E $E+E* $E+E*id $E+E*E $E+E $E Input id+id*id$ +id*id$ +id*id$ id*id$ *id$ *id$ id$ $ $ $ $ Action shift reduce E → id shift shift reduce E → id shift (or reduce?) shift reduce E → id reduce E → E * E reduce E → E + E accept Grammar: E → E + E E → E * E E → ( E ) E → id Find handles to reduce How to resolve conflicts? `
  • 3. 3 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 $… $…if E then S Input …$ else…$ Action … 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 $ $a Input aa$ a$ Action shift reduce A → a or B → a ? Grammar: C → A B A → a B → a Resolve in favor of reduce A → a, otherwise we’re stuck!
  • 4. 4 10 LR(k) Parsers: Use a DFA for Shift/Reduce Decisions 1 2 4 5 3 0 start a A C B a Grammar: S → C C → A B A → a B → a State I0: S → •C C → •A B A → •a State I1: S → C• State I2: C → A•B B → •a State I3: A → a• State I4: C → A B• State I5: B → a• goto(I0,C) goto(I0,a) goto(I0,A) goto(I2,a) goto(I2,B) Can only reduce A → a (not B → a) 11 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ aa$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A → a (goto 2) shift (goto 5) reduce B → a (goto 4) reduce C → AB (goto 1) accept (S → C) Grammar: S → C C → A B A → a B → a The states of the DFA are used to determine if a handle is on top of the stack State I0: S → •C C → •A B A → •a State I3: A → a• goto(I0,a) 12 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ aa$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A → a (goto 2) shift (goto 5) reduce B → a (goto 4) reduce C → AB (goto 1) accept (S → C) Grammar: S → C C → A B A → a B → a The states of the DFA are used to determine if a handle is on top of the stack State I0: S → •C C → •A B A → •a State I2: C → A•B B → •a goto(I0,A)
  • 5. 5 13 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ aa$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A → a (goto 2) shift (goto 5) reduce B → a (goto 4) reduce C → AB (goto 1) accept (S → C) Grammar: S → C C → A B A → a B → a The states of the DFA are used to determine if a handle is on top of the stack State I2: C → A•B B → •a State I5: B → a• goto(I2,a) 14 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ aa$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A → a (goto 2) shift (goto 5) reduce B → a (goto 4) reduce C → AB (goto 1) accept (S → C) Grammar: S → C C → A B A → a B → a The states of the DFA are used to determine if a handle is on top of the stack State I2: C → A•B B → •a State I4: C → A B• goto(I2,B) 15 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ aa$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A → a (goto 2) shift (goto 5) reduce B → a (goto 4) reduce C → AB (goto 1) accept (S → C) Grammar: S → C C → A B A → a B → a The states of the DFA are used to determine if a handle is on top of the stack State I0: S → •C C → •A B A → •a State I1: S → C• goto(I0,C)
  • 6. 6 16 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ aa$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A → a (goto 2) shift (goto 5) reduce B → a (goto 4) reduce C → AB (goto 1) accept (S → C) Grammar: S → C C → A B A → a B → a The states of the DFA are used to determine if a handle is on top of the stack State I0: S → •C C → •A B A → •a State I1: S → C• goto(I0,C) 17 Model of an LR Parser $ an … ai … a2 a1 LR Parsing Program (driver) goto action s0 … Xm-1 sm-1 Xm sm output stack input DFA shift reduce accept error Constructed with LR(0) method, SLR method, LR(1) method, or LALR(1) method 18 LR Parsing (Driver) (s0 X1 s1 X2 s2 … Xm sm, ai ai+1 … an $) stack input Configuration ( = LR parser state): 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
  • 7. 7 19 Example LR Parse Table Grammar: 1. E → E + T 2. E → T 3. T → T * F 4. T → F 5. F → ( E ) 6. F → id r5 r5 r5 r5 r3 r3 r3 r3 r1 r1 s7 r1 s11 s6 s4 s5 s4 s5 r6 r6 r6 r6 s4 s5 r4 r4 r4 r4 r2 r2 s7 r2 acc s6 s4 s5 $ ) ( * + id 11 10 9 8 7 6 5 4 3 2 1 0 F T E 10 3 9 3 2 8 3 2 1 Shift & goto 5 Reduce by production #1 action goto state 20 Example LR Parsing Stack $ 0 $ 0 id 5 $ 0 F 3 $ 0 T 2 $ 0 T 2 * 7 $ 0 T 2 * 7 id 5 $ 0 T 2 * 7 F 10 $ 0 T 2 $ 0 E 1 $ 0 E 1 + 6 $ 0 E 1 + 6 id 5 $ 0 E 1 + 6 F 3 $ 0 E 1 + 6 T 9 $ 0 E 1 Input id*id+id$ *id+id$ *id+id$ *id+id$ id+id$ +id$ +id$ +id$ +id$ id$ $ $ $ $ Action shift 5 reduce 6 goto 3 reduce 4 goto 2 shift 7 shift 5 reduce 6 goto 10 reduce 3 goto 2 reduce 2 goto 1 shift 6 shift 5 reduce 6 goto 3 reduce 4 goto 9 reduce 1 goto 1 accept Grammar: 1. E → E + T 2. E → T 3. T → T * F 4. T → F 5. F → ( E ) 6. F → id 21 SLR Grammars • SLR (Simple LR): 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) S → E E → id + E E → id State I0: S → •E E → •id + E E → •id State I2: E → id•+ E E → id• goto(I0,id) goto(I3,+) FOLLOW(E)={$} thus reduce on $ Shift on +
  • 8. 8 22 SLR Parsing Table r2 s2 r3 s3 acc s2 $ + id 4 3 2 1 0 E 4 1 1. S → E 2. E → id + E 3. E → id FOLLOW(E)={$} thus reduce on $ Shift on + • Reductions do not fill entire rows • Otherwise the same as LR(0) 23 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 24 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]
  • 9. 9 25 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 → X Y Z 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 → •] 26 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 27 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
  • 10. 10 28 The Closure Operation (Example) Grammar: E → E + T | T T → T * F | F F → ( E ) F → id { [E’ → • E] } closure({[E’ → •E]}) = { [E’ → • E] [E → • E + T] [E → • T] } { [E’ → • E] [E → • E + T] [E → • T] [T → • T * F] [T → • F] } { [E’ → • E] [E → • E + T] [E → • T] [T → • T * F] [T → • F] [F → • ( E )] [F → • id] } Add [E→•γ] Add [T→•γ] Add [F→•γ] 29 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 γ 30 The Goto Operation (Example 1) Suppose I = Then goto(I,E) = closure({[E’ → E •, E → E • + T]}) = { [E’ → E •] [E → E • + T] } Grammar: E → E + T | T T → T * F | F F → ( E ) F → id { [E’ → • E] [E → • E + T] [E → • T] [T → • T * F] [T → • F] [F → • ( E )] [F → • id] }
  • 11. 11 31 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 32 Example SLR Grammar and LR(0) Items Augmented grammar: 1. C’ → C 2. C → A B 3. A → a 4. B → a State I0: C’ → •C C → •A B A → •a State I1: C’ → C• State I2: C → A•B B → •a State I3: A → a• State I4: C → A B• State I5: B → a• goto(I0,C) goto(I0,a) goto(I0,A) goto(I2,a) goto(I2,B) I0 = closure({[C’ → •C]}) I1 = goto(I0,C) = closure({[C’ → C•]}) … start final 33 Example SLR Parsing Table r4 r2 r3 s5 acc s3 $ a 5 4 3 2 1 0 B A C 4 2 1 State I0: C’ → •C C → •A B A → •a State I1: C’ → C• State I2: C → A•B B → •a State I3: A → a• State I4: C → A B• State I5: B → a• 1 2 4 5 3 0 start a A C B a Grammar: 1. C’ → C 2. C → A B 3. A → a 4. B → a
  • 12. 12 34 SLR and Ambiguity • Every SLR grammar is unambiguous, but not every unambiguous grammar is SLR • Consider for example the unambiguous grammar S → L = R | R L → * R | id R → L I0: S’ → •S S → •L=R S → •R L → •*R L → •id R → •L I1: S’ → S• I2: S → L•=R R → L• I3: S → R• I4: L → *•R R → •L L → •*R L → •id I5: L → id• I6: S → L=•R R → •L L → •*R L → •id I7: L → *R• I8: R → L• I9: S → L=R• action[2,=]=s6 action[2,=]=r5 no Has no SLR parsing table 35 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: [A→α•β] LR(1) item: [A→α•β, a] 36 I2: S → L•=R R → L• action[2,=]=s6 Should not reduce on =, because no right-sentential form begins with R= split R → L• S → L•=R SLR Versus LR(1) • Split the SLR states by adding LR(1) lookahead • Unambiguous grammar 1. S → L = R 2. | R 3. L → * R 4. | id 5. R → L lookahead=$ action[2,$]=r5
  • 13. 13 37 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 38 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 39 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)
  • 14. 14 40 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 41 Example Grammar and LR(1) Items • Unambiguous LR(1) grammar: S → L = R | R L → * R | id R → L • Augment with S’ → S • LR(1) items (next slide) 42 [S’ → •S, $] goto(I0,S)=I1 [S → •L=R, $] goto(I0,L)=I2 [S → •R, $] goto(I0,R)=I3 [L → •*R, =/$] goto(I0,*)=I4 [L → •id, =/$] goto(I0,id)=I5 [R → •L, $] goto(I0,L)=I2 [S’ → S•, $] [S → L•=R, $] goto(I0,=)=I6 [R → L•, $] [S → R•, $] [L → *•R, =/$] goto(I4,R)=I7 [R → •L, =/$] goto(I4,L)=I8 [L → •*R, =/$] goto(I4,*)=I4 [L → •id, =/$] goto(I4,id)=I5 [L → id•, =/$] [S → L=•R, $] goto(I6,R)=I9 [R → •L, $] goto(I6,L)=I10 [L → •*R, $] goto(I6,*)=I11 [L → •id, $] goto(I6,id)=I12 [L → *R•, =/$] [R → L•, =/$] [S → L=R•, $] [R → L•, $] [L → *•R, $] goto(I11,R)=I13 [R → •L, $] goto(I11,L)=I10 [L → •*R, $] goto(I11,*)=I11 [L → •id, $] goto(I11,id)=I12 [L → id•, $] [L → *R•, $] I0: I1: I2: I3: I4: I5: I6: I7: I8: I9: I10: I12: I11: I13:
  • 15. 15 43 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,$] 44 Example LR(1) Parsing Table r4 r5 s11 s12 r6 r2 r6 r6 r4 r4 s11 s12 r5 r5 s4 s5 r3 r6 s6 acc s4 s5 $ = * id 13 12 11 10 9 8 7 6 5 4 3 2 1 0 R L S 13 10 4 10 7 8 3 2 1 Grammar: 1. S’ → S 2. S → L = R 3. S → R 4. L → * R 5. L → id 6. R → L 45 LALR(1) Grammars • LR(1) parsing tables have many states • LALR(1) parsing (Look-Ahead LR) combines LR(1) states 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
  • 16. 16 46 Constructing LALR(1) Parsing Tables 1. Construct sets of LR(1) items 2. Combine LR(1) sets with sets of items that share the same first part [L → *•R, =] [R → •L, =] [L → •*R, =] [L → •id, =] [L → *•R, $] [R → •L, $] [L → •*R, $] [L → •id, $] I4: I11: [L → *•R, =/$] [R → •L, =/$] [L → •*R, =/$] [L → •id, =/$] Shorthand for two items in the same set 47 Example LALR(1) Grammar • Unambiguous LR(1) grammar: S → L = R | R L → * R | id R → L • Augment with S’ → S • LALR(1) items (next slide) 48 [S’ → •S, $] goto(I0,S)=I1 [S → •L=R, $] goto(I0,L)=I2 [S → •R, $] goto(I0,R)=I3 [L → •*R, =/$] goto(I0,*)=I4 [L → •id, =/$] goto(I0,id)=I5 [R → •L, $] goto(I0,L)=I2 [S’ → S•, $] [S → L•=R, $] goto(I0,=)=I6 [R → L•, $] [S → R•, $] [L → *•R, =/$] goto(I4,R)=I7 [R → •L, =/$] goto(I4,L)=I9 [L → •*R, =/$] goto(I4,*)=I4 [L → •id, =/$] goto(I4,id)=I5 [L → id•, =/$] [S → L=•R, $] goto(I6,R)=I8 [R → •L, $] goto(I6,L)=I9 [L → •*R, $] goto(I6,*)=I4 [L → •id, $] goto(I6,id)=I5 [L → *R•, =/$] [S → L=R•, $] [R → L•, =/$] I0: I1: I2: I3: I4: I5: I6: I7: I8: I9: Shorthand for two items [R → L•, =] [R → L•, $]
  • 17. 17 49 Example LALR(1) Parsing Table r6 r6 r2 r4 r4 s4 s5 r5 r5 s4 s5 r3 r6 s6 acc s4 s5 $ = * id 9 8 7 6 5 4 3 2 1 0 R L S 8 9 7 9 3 2 1 Grammar: 1. S’ → S 2. S → L = R 3. S → R 4. L → * R 5. L → id 6. R → L 50 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(1) if its LALR(1) parse table has no conflicts – LR(1) if its LR(1) parse table has no conflicts 51 LL, SLR, LR, LALR Grammars LL(1) LR(1) LR(0) SLR LALR(1)
  • 18. 18 52 Dealing with Ambiguous Grammars 1. S’ → E 2. E → E + E 3. E → id r2 s3/r2 s2 r3 r3 acc s3 s2 $ + id 4 3 2 1 0 E 4 1 Shift/reduce conflict: action[4,+] = shift 4 action[4,+] = reduce E → E + E When reducing on +: yields left associativity (id+id)+id When shifting on +: yields right associativity id+(id+id) $ 0 E 1 + 3 E 4 id+id+id$ +id$ $ 0 … … stack input ≈ ≈ ≈ 53 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 S’ → E E → E + E E → E * E E → id $ 0 E 1 * 3 E 5 id*id+id$ +id$ $ 0 … … stack input reduce E → E * E ≈ ≈ ≈ 54 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
  • 19. 19 55 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