LR(0)
LR(0)
C.Naga Raju
B.Tech(CSE),M.Tech(CSE),PhD(CSE),MIEEE,MCSI,MISTE
Professor
Department of CSE
YSR Engineering College of YVU
Proddatur
id id F id T*F
id F id
id
Prof.C.NagaRaju YSRCE of YVU CSE 9949218570
Shift-Reduce Parsing
• The general idea is to shift some symbols of input to the
stack until a reduction can be applied
• At each reduction step, if a specific substring is matched
then the body of a production is replaced by the Non
Terminal at the head of the production A—>ac/b
• The key decisions during bottom-up parsing are about when
to reduce and what production should apply
• A reduction is a reverse of a step in a derivation
• The goal of a bottom-up parser is to construct a derivation in
reverse:
– E=>T=>T*F=>T*id=>F*id=>id*id
Prof.C.NagaRaju YSRCE of YVU CSE 9949218570
LR Parsers
LR(k)
LR k
LL(1)
a1 a2 … ai … an $ Scanner
sm LR Parsing Engine
Xm
sm-1
Xm-1
Compiler Construction
…
s0 Parser
Action Goto Grammar
Generator
Stack
LR Parsing Tables
s0X1s1X2…Xmsm
2. A GOTO function.
✓ Shift
✓ Reduce
✓ Accept
✓ Error
✓ Parsing is completed
18
Prof.C.NagaRaju YSRCE of YVU CSE 9949218570
❖ The GOTO Table is important to find out the next
state after every reduction.
ex : GOTO[S, A]
Example:
Example
( S ) S. With S → Є
( S ) .S With S → S
((S)S.) With S → ( S ) S
id*id id F->id
F*id F T->F
T*id id F->id
4. A→XYZ•
T T S7
S0
S → •E$ T → (•E)
E → •T E → •T
( E → •E+T
E → •E+T
T → •i T → •i
T → •(E) S5 T → •(E) (
i
i
E T → i• E
( S8
S1
S → E•$ T → (E•)
E → E•+T i E → E•+T
+ +
)
$ E → E+•T
S3 T → •i
S2 T → •(E) S9
S → E$• T → (E)•
T
S4
Prof.C.NagaRaju YSRCE of YVU 28
E → E+T•
CSE 9949218570
Construction of LR(0) Items
I0 T i (
I6
T
( I9
I7 I8 S
E
A. n, E + n and E + n × n
B. n, E + n and E + E × n
C. n, n + n and n + n × n
D. n, E + n and E × n
• E → E * n {Applying E → E * n }
• E→ E + n * n {Applying E → E + n }
• E→ n + n * n {Applying E → n } Hence, the handles in right
sentential form is n, E + n and E × n.
• Hence Option D is the right choice
A. Only I
B. Only II
C. Both I and II
D. Neither I nor II
A. Only I)
B. I) and ii)
C. I), ii) and iii)
D. I), ii) , iii) and iv)
A. Shift/reduce conflict
B. Reduce /shift conflict
C. Shift conflict
D. Reduce conflict
SLR(1) LR(0)
E’ → E# E’ →.E#
E → .E + T
E→E+T
E→T
E → .T
T→T * F
T→F
T → .T * F
F→(E)
F → id
T → .F
F → .( E )
+ T
S6 S9 *
S1
( S7
F
id
E S3
S4
S2 S5
T *
T ( F
( S10
S0 S7
S4 S3 (
id id
F S4
id E
F S5
S5
S8 )
S11
+
S3
S6 YSRCE of YVU
Prof.C.NagaRaju 61
CSE 9949218570
Construction of Follow Function
E’ →.E# Follow (E) = { # , + , ) }
E →.E + T ( r1 )
Follow (T) = { * , # , + , ) }
E →.T ( r2 )
T →.T * F ( r3 ) Follow (F) = { * , # , + , ) }
T →.F ( r4 )
F →.( E ) ( r5 )
F →.id ( r6 )
id + * ( ) $ E T F
0 S5 S4 1 2 3
1 S6 Acc
2 r2 S7 r2 r2
3 r4 r4 r4 r4
4 S5 S4 8 2 3
5 r6 r6 r6 r6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 r1 S7 r1 r1
10 r3 r3 r3 YSRCE
Prof.C.NagaRaju r3 of YVU 63
11 r5 r5 r5 r5
CSE 9949218570
Input Acceptance
In S0 with input
symbol id, shift the
symbol onto the stack
and enter state S5
Reduce
using grammar
production6
Reduce
using grammar
production6
T2
Reduction exposes S0
and goto of S0 gives
next state for the
leading non-terminal,
T. The next state is S2
(7) 0T2*7 F 10
Reduction exposes S7
and goto of S7 gives
next state for the
leading non-terminal,
F. The next state is S10
(8) 0T2 + id $
(8) 0T 2 + id $ Reduce E → T
(8) 0T + id $ Reduce E → T
(9) 0 E 1 + id $
(9) 0E 1 + id $ Shift
(10) 0E1+6 id $
(11) 0 E 1 + 6 id 5 $ Reduce F → id
(12) 0E1+6 F $
(12) 0E1+6 F 3 $
(12) 0E1+6 F3 $
(13) 0E1+6 T $
(13) 0E1+6 T 9 $
(14) 0E1 $
A. Will be affecting.
B. Does not have any affect.
C. Shift will take place.
D. Reduction will take place.