Compiler Design Project: For DR, Elie Nasr by Anwar Zouyhed 12040411
Compiler Design Project: For DR, Elie Nasr by Anwar Zouyhed 12040411
Example 5.2
5.2 Consider the following grammar:
E E$
E (L)
Ea
L L,E
L E
5.2 / a
a. Construct the DFA of LR(1) items for this
grammar.
Stage 0
E.E, $
E.(L), $
E.a,
Stage 9
EE. , $
LL,.E, )
Stage 3
E.(L), )
E(L.), ,
Ea. , $
E.a,
LL.,E, )
LL,.E , ,
LL.,E, ,
$
Stage 4
E(L.) , $
Stage 2
E (.L) , $
LL.,E , )
LL.,E , ,
L .L,E , )
L .E
,)
a
, ,
E.a,
E .a , )
E.(L) ,
[E.a
Stage 12
E(L.), )
E.(L), ,
E(L)., )
E(L)., ,
Stage 11
LL,E. , )
LL,E. , ,
Stage 6
a
Stage 8
Stage 7
E(L)., $
Ea., )
E(.L),
E(.L),
L.L,E , )
L.E ,
Ea., ,
E .(L) , )
L .L,E , ,
L .E
Stage 10
Stage 1
L.L,E , ,
L.E ,
Stage 5
LE. , )
LE. , ,
E.(L), ()
E.a,
()
E.(L), ,
E.a, , ,
5.2 / b
b. Construct the general LR(1) parsing table.
b.
State
Input
S2
S3
Goto
)
S6
S7
10
r2
S8
S9
r4
r4
S6
S7
r2
r2
8
9
Accept
1
2
r1
S6
S7
11
10
S12
S9
11
r3
r3
12
r1
r1
5.2 / c
c. Construct the DFA of LALR(1) items for this
grammar.
Stage 1
Stage 0
EE. , $
E.E, $
E.(L), $
E.a,
Stage 3 / 7
Ea. , $
Ea. , ) /,
Stage 9
Stage 2 / 6
E (.L) , $ /, / )
L .L,E , )
L .E
,)
, ,
Stage 4 / 10
E .(L) , )
E .a , )
E.(L) ,
[E.a
E.(L), )
LE. , )
E.a,
LE. , ,
L .L,E , ,
L .E
LL,.E, )
Stage 5
E(L.) , $ /, / )
Stage 11
LL,.E , ,
LL,E. , )
E.(L), ,
LL,E. , ,
E.a,
LL.,E , )
LL.,E , ,
Stage 8 / 12
E(L)., $ / ) / ,
5.2 / d
d. Construct the LALR(1) parsing table.
5.2 / d
d.
State
Input
S2/6
S3/7
Goto
)
Accept
S2/6
S3/7
4/10
3/7
r2
r2
4/10
S8/12
S9
r4
r4
8/12
r1
r1
1
2/6
S2/6
S3/7
r2
r1
11
10
S8/12
S9
11
r3
r3
5.2 / e
e. Describe any difference that might occur
between the actions of a general LR(1) parser
and an LALR(1) parser.
5.2 / e
ANS : The consequence of using LALR(1)
parsing over general LR(1) parsing is that , in
the presence of errors, some spurious
reeducation may be made before error is
declared