You are on page 1of 17

TK3163 Compiler Constructions

Ch 4.6 : Introduction to LR Parsing -


SLR

Prof. Dr. Mohd Juzaiddin Ab Aziz


Dr. Bahari Idrus

1
LR Parsing: Simple LR (SLR) or LR(0)
 LR(k) parsing:
◦ L for left-to-right scanning of the input;
◦ R for rightmost derivation in reverse;
◦ k for the number of input symbols of
lookahead.
 The easiest method for constructing shift-
reduce parsers, called “simple LR” or SLR
or LR(0).

2
SLR Grammars
 LR parsers are table-driven
 A grammar for which we can construct a
parsing table is said to be an LR grammar
 How does a shift-reduce parser know
when to shift and when to reduce?
 An LR parser makes shift-reduce
decisions.

3
LR(0) Items of a Grammar
 States represent sets of items.
 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  XY 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  •]

4
LR(0) set of items or LR(0) automaton
 One collection of sets of LR(0) items,
called the canonical LR(0) collection or
LR(0) automaton.
 Each state of the LR(0) automaton
represents a set of items in the canonical
LR(0) collection.

5
Constructing the set of LR(0) Items
of a Grammar
1. Define an augmented grammar with a
new start symbol S’, production S’S
2. Implement function closure and goto.

6
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

7
The Closure Operation (Example)

closure({[E’  •E]}) =
{ [E’  • E] } { [E’  • E] { [E’  • E] { [E’  • E]
[E  • E + T] [E  • E + T] [E  • E + T]
Add [E•] [E  • T] } [E  • T] [E  • T]
[T  • T * F] [T  • T * F]
Grammar: [T  • F] } [T  • F]
EE+T Add [T•] [F  • ( E )]
ET Add [F•] [F  • id] }
TT*F
TF
F(E)
F  id
8
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 

9
Grammar:
0. E’  E
1. E  E + T
2. E  T
3. T  T * F
4. T  F
5. F  ( E )
6. F  id
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]

11
Example LR Parse Table
Grammar: action goto
1. E  E + T
state id + * ( ) $ E T F
2. E  T
0 s5 s4 1 2 3
3. T  T * F
1 s6 acc
4. T  F
5. F  ( E ) 2 r2 s7 r2 r2
6. F  id 3 r4 r4 r4 r4
4 s5 s4 8 2 3
Follow(E) = {+,) ,$} 5 r6 r6 r6 r6
Follow(T) = {+,*,), $} 6 s5 s4 9 3
Follow(F) = {+,*,), $} 7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5 12
Example LR(0)
Stack Symbols Input Action
(1) 0 $ id*id$ shift to 5
(2) 05 $id *id$ reduce F  id
(3) 03 $F *id$ reduce T  F
(4) 02 $T *id$ shift to 7
(5) 027 $T * id$ shift to 5
(6) 0275 $T * id $ reduce F  id
(7) 0 2 7 10 $T * F $ reduce T  T * F
(8) 02 $T $ reduce E T
(9) 01 $E $ accept

Grammar:
1. E  E + T
2. E  T
3. T  T * F
4. T  F
5. F  ( E )
6. F  id 13
Example LR(0) Grammar:
1. E  E + T
Stack Symbols Input Action
2. E  T
(1) 0 $ id*id+id$ shift s5
(2) 05 $id *id+id$ reduce F  id 3. T  T * F
(3) 03 $F *id+id$ reduce T  F 4. T  F
(4) 02 $T *id+id$ shift s7 5. F  ( E )
(5) 027 $T * id+id$ shift s5
(6) 0275 $T * id +id$ reduce F  id 6. F  id
(7) 0 2 7 10 $T * F +id$ reduce T  T * F
(8) 02 $T +id$ reduce E T
(9) 01 $E +id$ shift s6
(10) 016 $E + id$ shift s5
(11) 0165 $E + id $ reduce F  id
(12) 0163 $E + F $ reduce T  F
(13) 0169 $E + T $ reduce E  E + T
(14) 01 $E $ accept

14
Exercise 1
1. Construct the LR(0) sets of items for the
(augmented) grammar S SS+|SS*|a.
Compute the GOTO function for these sets of
items.
2. Show the SLR parsing table for the grammar.
3. Show the actions of your parsing table on the
input aa*a+.

15
Exercise 2
1. Construct the LR(0) sets of items for the
(augmented) grammar
S  A#
A  aA
Ab
2. Compute the GOTO function for these sets of
items.
3. Show the SLR parsing table for the grammar.
4. Show the actions of your parsing table on the
input aab#

16
Exercise 3
1. Construct the LR(0) sets of items for the
(augmented) grammar
C  AB
Aa
Ba
2. Compute the GOTO function for these sets of
items.
3. Show the SLR parsing table for the grammar.
4. Show the actions of your parsing table on the
input aa.

17

You might also like