1. Consider the following grammar
E Ỉ T+E | T
T Ỉ id
a. Construct the collection of sets of LR(0) items for this grammar
Augmented grammar:
(0): E’ Ỉ E
(1): E Ỉ T+E
(2): E Æ T
(3): T Æ id
I0 = closure(E’ Æ E)
E’ Æ •E
E Ỉ •T+E
E Ỉ •T
T Ỉ •id
goto(I0,E) = I1
E’ Ỉ Eã
goto(I0,T) = I2
E ặ Tã+E
E ặ Tã
goto(I0,id) = I3
T ặ idã
goto(I4,T) = I2
goto(I4,id) = I3
goto(I2,+) = I4
E ặ T+ãE
E ặ •T+E
E Ỉ •T
T Ỉ •id
goto(I4,E) = I5
E Ỉ T+E•
b. Construct the parsing table using SLR method
id
+
0
s3
1
2
s4
3
r3
4
s3
5
$
E
1
T
2
5
2
acc
r2
r3
r1
c. Show the moves according to the table constructed above on the inputs of id+id and
id+id+id
Input
Stack
id+id$
0
+id$
0 id 3
+id$
0T2
id$
0T2+4
$
0 T 2 + 4 id 3
$
0T2+4T2
$
0T2+4E5
$
0E1
Stack
0
0 id 3
0T2
0T2+4
0 T 2 + 4 id 3
0T2+4T2
0T2+4T2+4
0 T 2 + 4 T 2 + 4 id 3
0T2+4T2+4T2
0T2+4T2+4E5
0T2+4E5
0E1
Input
id+id+id$
+id+id$
+id+id$
id+id$
+id$
+id$
id$
$
$
$
$
$
2. Show that the following grammar is LL(1) but not SLR(1). Also, show that the
grammar is LR(1) as well.
S Ỉ AaAb | BbBa
∈
BỈ∈
* The grammar is LL(1):
For SỈ AaAb | BbBa
Select(SỈAaAb) = {a}
Select(SỈBbBa) = {b}
For A Ỉ ∈
First(∈) = {∈} and Follow(∈) ={a,b}
For B Ỉ ∈
First(BỈ∈) = {∈} and Follow(BỈ∈) ={a,b}
No conflict found Ỵ the grammar is LL(1)
* The grammar is not SLR(1)
(0): S’ Ỉ S
(1): S ỈAaAb
(2): S ỈBbBa
(3): A Ỉ
(4): B ặ
I0 = closure(SặS)
S ặ ãAaAb
S ặ ã BbBa
Aặã
Bặã
Since Follow(A)∩Follow(B) ≠∅, state 0 (corresponding to I0) yields a reduce/reduce
conflict between A Ỉ ∈ and B Ỉ ∈. Thus, the grammar is not SLR(1).
* The grammar is LR(1)
I0 = closure(SặãS, $)
S ặ ãS, $
S ặ ãAaAb, $
S ặ ãBbBa, $
Aặã,a
Bặã,b
I1 = goto(I0, A)
S ặ AãaAb, $
I3 = goto(I1,a)
S ặ AaãAb, $
Aặã,b
I6 = goto(I4,B)
S ặ BbBãa, $
I7 = goto(I5,b)
S ặ AaAbã, $
I4 = goto(I2,b)
S ặ BbãBa, $
Bặã,a
I8 = goto(I6,a)
S ặ BbBaã, $
I5 = goto(I3,A)
S ặ AaAãb, $
I9 = goto(I0,S)
Sặ Sã, $
I2 = goto(I0, B)
S ặ BãbBa, $
0
1
2
3
4
5
6
7
8
9
a
r3
s3
b
r4
$
S
9
s4
r3
6
s7
s8
r1
r2
acc
No conflict found ẻ the grammar is LR(1)
3. Show that the following grammar is LR(1) but not SLR(1)
Augmented grammar
(0): S’ Ỉ S
(1): S Ỉ Aa
(2): S Ỉ bAc
(3): S Ỉ dc
(4): S Ỉ bda
(5): A Ỉ d
B
2
5
r4
S Ỉ Aa | bAc | dc | bda
d
A
1
* The grammar is not SLR
I1 = goto(I0,d)
S ặ dãc
A Æ d•
I0 = closure(S’Æ•S)
S’ Æ •S
S Æ •Aa
S Æ •bAc
S Æ •dc
S Æ •bda
A Æ •d
Since Follow(A) = {a,c}, a shift/reduce conflict will occur at state 1
* The grammar is LR(1)
I0 = closure(SặãS, $)
S ặ ãS, $
S ặ ãAa, $
S Æ •bAc, $
S Æ •dc, $
S Æ •bda, $
A Æ •d, a
I1 = goto(I0,S)
S’ỈS•, $
I7 = goto(I3,d)
S Ỉ bd•a, $
A Æ d•, c
I8 = goto(I4,d)
S Æ dc•, $
I4 = goto(I0,d)
S Æ d•c, $
A Æ d•, a
I9 = goto(I6,c)
S Æ bAc•, $
I5 = goto(I2,a)
S ặ Aaã, $
I2 = goto(I0,A)
SặAãa, $
a
I3 = goto(I0,b)
S ặ bãAc, $
S ặ bãda, $
A ặ ãd, c
b
s3
I6 = goto(I3,A)
S ặ bAãc, $
c
d
s4
0
1
2
s5
3
s7
4
r5
s8
5
6
s9
7
s10
r5
8
9
10
No conflict found ẻ the grammar is LR(1)
I10 = goto(I7,a)
S ặ bdaã, $
$
S
1
A
2
acc
6
r1
r3
r2
r4
4. Show that the following grammar is LR(1) but not LALR(1)
S Ỉ As | bAc | Bc | bBa
d
BỈd
Augmented grammar:
(0): S’ Ỉ S
(1): S Ỉ As
(2): S Ỉ bAc
(3): S Æ Bc
(4): S Æ bBa
(5): A Æ d
(6): B Æ d
* The grammar is LR(1)
I0 = closure(SặãS,$)
Sặã S,$
S ặ ã As, $
S ặ ã bAc, $
S ặ ã Bc, $
S Æ • bBa, $
A Æ • d, s
B Æ • d, c
I1 = goto(I0,S)
S ặ Sã, $
I2 = goto(I0,A)
S ặ Aãs, $
I3 = goto(I0,B)
S ặ Bãc, $
I4 = goto(I0,b)
S ặ bãAc, $
S ặ bãBa, $
A ặ ãd, c
B ặ ãd, a
I8 = goto(I4,A)
S ặ bAãc, $
I5 = goto(I0,d)
A ặ dã, s
B ặ dã, c
I10 = goto(I4,d)
A ặ dã, c
B ặ dã, a
I6 = goto(I2,s)
S ặ Asã, $
I11 = goto(I8,c)
S ặ bAcã, $
I7 = goto(I3,c)
S ặ Bcã, $
I12 = goto(I9,a)
S ặ bBaã, $
I9 = goto(I4,B)
S ặ bBãa, $
s
0
1
2
3
4
5
6
7
8
9
10
11
12
a
b
s4
c
d
s5
$
S
1
A
2
B
3
8
9
acc
s6
s7
s10
r5
r6
r1
r3
s11
s12
r6
r5
r2
r4
No conflict found Ỵ the grammar is LR(1)
* The grammar is not LALR(1)
When merging state 5 and 10, we have reduce/reduce conflict, caused by the fact that
Follow(A) = {s,c}∩ Follow(B) = {c,a} ≠ ∅