LR (0) and SLR
LR (0) and SLR
1
Contents
• Introduction to bottom up parsers
• LR(0) Parser
• Example problem
• Gate Questions and solutions
• SLR(1) Parser
• Example problem
• Gate Questions and solutions
Bottom up Parser
• Construction of parse tree for any given input string
beginning at the bottom and working towards the root is
called bottom up parser
For example the given input string : id*id
id id F id T*F
id F id
id
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
LR Parsers
LR(k)
LR k
5
❖ Types of LR Parsers
1.LR(0) Parser
2.Simple LR-Parser (SLR)
3.Canonical LR Parser (CLR)
4.LALR Parser.
6
Comparison of LL & LR Methods
LL(1)
7
▪ Advantages of LR Parsers
▪ LR parsers are constructed to recognize all
Programming Languages
8
•The LR Parsing Algorithm
Input
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
9
❖ The LR parser consists of 1) Input 2)Output
3)Stack 4) Driver Program 5) Parsing Table
10
❖ The Driver Program uses the Stack to store a
string
s0X1s1X2…Xmsm
11
❖ The Parsing Program reads characters from an
Input Buffer one at a time
12
Parse Table
❖ The LR Shift-Reduce Parsers can be efficiently
implemented by computing a table to guide the
processing
2. A GOTO function.
13
❖ The Action Table
❖ The Action Table specifies the actions of the parser
(e.g., shift or reduce), for the given parse state and
the next token
14
LR Driver Program
❖ The LR driver Program determines Sm, the state on
top of the stack and ai, the Current Input symbol.
✓ Shift
✓ Reduce
✓ Accept
✓ Error
15
❖ If Action[ Sm, ai ] = Shift S
16
❖ If Action[ Sm, ai ] = Accept,
✓ Parsing is completed
17
❖ GOTO Table
❖ The GOTO table specifies which state to put on
top of the stack after a reduce
18
❖ The GOTO Table is important to find out the next
state after every reduction.
ex : GOTO[S, A]
Example:
20
LR(0) Parser
❖ The LR Parser is a Shift-reduce Parser that makes
use of a Deterministic Finite Automata, recognizing
the Set Of All Viable Prefixes by reading the stack
from Bottom To Top.
21
❖ Handle: Handle is a substring that matches the
body of a production
❖ Handle is a Right Sentential Form + position
where reduction can be performed + production
used for reduction
Example
( S ) S. With S → Є
( S ) .S With S → S
((S)S.) With S → ( S ) S
22
• Handle pruning : Handle pruning specifies that the reduction
represents one step along the reverse of a rightmost
derivation
id*id id F->id
F*id F T->F
T*id id F->id
24
LR(0) Items
An LR(0) Item of a Grammar G is a Production of G
with a Dot ( ) at some position of the right side.
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
28
E → E+T•
Construction of LR(0) Items
29
Construction of DFA for LR(0) Items
$
I1 I2
+
E
T
i I3 I4
I5
i
I0 T i (
I6
T
( I9
I7 I8 S
E
30
Construction of LR(0) Parsing Table
Input
States Action Part (Terminals) Goto Part
(Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce 31
String Acceptance
Input
States Action Part (Terminals) Goto Part (Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce
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
Explanation
• 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
Question 2
A bottom-up parser generates:
A. Only I
B. Only II
C. Both I and II
D. Neither I nor II
Explanation
A. Only I)
B. I) and ii)
C. I), ii) and iii)
D. I), ii) , iii) and iv)
Explanation
A. Shift/reduce conflict
B. Reduce /shift conflict
C. Shift conflict
D. Reduce conflict
Explanation
53
❖ SLR(1) Parsers are going to use one token of
lookahead to eliminate the conflicts.
54
❖ SLR(1) parser will perform a reduce action for
configuration B→a• if the lookahead symbol is in
the set Follow(B)
SLR(1) LR(0)
55 55
Example: SLR(1) Parser
❖ Construct the SLR(1) Parser for the Following
Grammar
57
Step2 : Constructing SLR(1) Automaton
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 )
F → . id 58
E → E+.T S9
S1 E’ → E.# +
T → .T*F T E → E + T.
E → E.+T T → .F T → T.* F
F F → .(E) S6
F → .id
E
S3 id
( *
S5
S0 E’ →.E# T S4
S
E →.E+T E2→ T. S10
E →.T T→ T. * F * T → T*F.
F
T →.T*F S7
T →.F T → T*.F
F →.(E) F →.(E)
T F →.id
F →.id (
S11
id ( id
S4 F → (E).
S5 F →(.E) S5
F F → id. id E →.E+T
)
E →.T
F T →.T*F E
T →.F F → (E.)
S3 F →.(E) E → E.+T
T → F. F →.id S8
(
+ 59
S6
Constructing the SLR(1) Items
I0: I1:Goto(I0,E ) I6:Goto(I1,+) I3:Goto(I4,F) I4:Goto(I7,( )
E→ E.# T→ F.
E’ →.E# E →E+.T
E→ E.+T I5:Goto(I7,id)
E →.E + T T→.T * F I4:Goto(I4,( )
E →.T I2:Goto(I0,T) T→.F
E→ T. F→.( E ) I5:Goto(I4,id) I3:Goto(I4,F)
T →.T * F
E→ T.*F F →.id
T →.F I9:Goto(I6,T) I7:Goto(I9,* )
F →.( E ) I3:Goto(I0,F) I7:Goto(I2,*)
E →E+T.
F →.id T→ F. E→ T*.F I6:Goto(I8,+)
T→ T.* F
F→ .( E )
I4:Goto(I0,( ) I5:Goto(I0,id) I4:Goto(I6,( )
F→ .id
T→id.
F→(. E)
I10:Goto(I7,F) I5:Goto(I6,id)
E→.E+T I8:Goto(I4,E)
T→T * F . T→id.
E→.T E→ (E.)
T→.T * F E →E.+T I3:Goto(I6,F)
I11:Goto(I8,) )
T→.F T→ F.
I2:Goto(I4,T) T→(E).
F→.( E ) E→ T. 60
F →.id E→ T.*F
Construction of DFA for SLR(1) Items
+ 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
S 61
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 )
62
Constructing the SLR(1) Parsing Table
Input
States Action Part Goto Part
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 r3 63
11 r5 r5 r5 r5
Input Acceptance
64
String Acceptance
id * id + id id * id + id$
In S0 with input
symbol id, shift the
symbol onto the stack
and enter state S5
65
String Acceptance
id * id + id id * id + id$
Reduce
using grammar
production6
66
String Acceptance
id * id + id id * id + id$
Reduce
using grammar
production6
67
String Acceptance
id * id + id id * id + id$
T2
Reduction exposes S0
and goto of S0 gives
next state for the
leading non-terminal,
T. The next state is S2
68
String Acceptance
id * id + id id * id + id$
69
String Acceptance
id * id + id id * id + id$
70
String Acceptance
id * id + id id * id + id$
71
String Acceptance
id * id + id id * id + id$
(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
72
String Acceptance
id * id + id id * id + id$
(8) 0T2 + id $
73
String Acceptance
id * id + id id * id + id$
(8) 0T 2 + id $ Reduce E → T
74
String Acceptance
id * id + id id * id + id$
(8) 0T + id $ Reduce E → T
(9) 0 E 1 + id $
75
String Acceptance
id * id + id id * id + id$
(9) 0E 1 + id $ Shift
(10) 0E1+6 id $
76
String Acceptance
id * id + id id * id + id$
77
String Acceptance
id * id + id id * id + id$
(11) 0 E 1 + 6 id 5 $ Reduce F → id
(12) 0E1+6 F $
78
String Acceptance
id * id + id id * id + id$
(12) 0E1+6 F 3 $
(12) 0E1+6 F3 $
79
String Acceptance
id * id + id id * id + id$
(13) 0E1+6 T $
80
String Acceptance
id * id + id id * id + id$
(13) 0E1+6 T 9 $
81
String Acceptance
id * id + id id * id + id$
(14) 0E1 $
82
String Acceptance
id * id + id id * id + id$
83
String Acceptance id * id + id$
STACK INPUT ACTION
86
Drawbacks
❖ SLR(1) parsers cannot parse some LR grammars.
88
GATE QUESTIONS AND SOLUTIONS
Question 1
• Consider the following two statements: P: Every regular
grammar is LL(1) Q: Every regular set has a LR(1) grammar
Which of the following is TRUE? (GATE 2007)
A. Will be affecting.
B. Does not have any affect.
C. Shift will take place.
D. Reduction will take place.
Explanation
102