You are on page 1of 26

18CSC304J

COMPILER DESIGN

UNIT 3
SESSIONS 12 & 13
Topics that will be covered in this Session

• Construction of Canonical LR(1) and LALR


• Construction of LALR
• Problems related to Canonical LR(1) and LALR Parsing
Table
More Powerful LR Parsers
1. The “canonical-LR” or “CLR” just “LR” method makes full use of the lookahead symbol(s).
This method uses a large set of items, called the LR(1) items
2. The “lookahead-LR” or “LALR” method is based on the LR(0) sets of items and has many fewer
states than parsers based on the LR(1) items. By carefully introducing lookaheads into the
LR(0) items, we can handle many more grammars with the LALR method than with the SLR
method, and build parsing tables that are no bigger than SLR tables. LALR is the method of
choice in most situations
Canonical LR(1) Items
• The items are redefined to include a terminal symbol as a second component
• The general form of an item becomes [A→•β,a] where A→β is a production and a is a terminal
or the right end-marker $
• Such an object is called an LR(1) item
• The 1 refers to the length of the second component, called the lookahead of the item
Algorithm :
Construction of
LR(1) Sets of Items
Algorithm :
Construction of
CLR Parsing Table
Construction of CLR Parsing Table - Example 1 :
• Construct CLR parsing table for the grammar
S → CC
C → cC | d

Step 1 : Find FIRST for all the non terminals


FIRST(S) = { c , d }
FIRST(C) = { c , d }

Step 2 : Number all the productions


1. S → CC
2. C → cC
3. C → d
Example 1 –cont.. goto(I2,d) - closure of { [ C → d • , $ ] }
• Step 3 : Write the goto(I0,S) - closure of { [ S’ → S • , $ ] } I7
C→d•,$
Augmented Grammar S’ → S • , $ I1
goto(I3,C) - closure of { [ C → cC •, c|d ] }
S’ → S goto(I0,C) - closure of { [ S → C • C , $ ] }
C → cC • , c|d I8
S → CC S→C•C,$
I2 goto(I3,c) - closure of { [ C → c •C, c|d ] }
C → cC C → • cC , $
C→d C → c • C , c|d
C→•d,$ I3
• Step 4 : Construct the goto(I0,c) - closure of { [ C → c • C , c|d ] }
C → • cC , c|d
canonical collection of sets C → • d , c|d
C → c • C , c|d
of LR(1) items
C → • cC , c|d
I3 goto(I3,d) - closure of { [ C → d • , c|d ] }
I4
C → • d , c|d C → d • , c|d
closure of { [ S’ → • S , $ ] } goto(I0,d) - closure of { [ C → d • , c|d ] } goto(I6,C) - closure of { [ C → cC •, $ ] }
I4 I9
S’ → • S , $ C → d • , c|d C → cC • , $
S → • CC , $ goto(I2,C) - closure of { [ S → CC • , $ ] } goto(I 6,c) - closure of { [ C → c • C , $ ] }
C → • cC , c|d I0 I5
S → CC • , $ C→c•C,$ I6
C → • d , c|d goto(I2,c) - closure of { [ C → c • C , $ ] } C → • cC , $
C→c•C,$ I6 C→•d,$
C → • cC , $ goto(I6,d) - closure of { [ C I→ d • , $ ] }
7
C→•d,$
C→d•,$
Example 1 –cont.. NOTE
• If there is a transition on a terminal then it
goto(I2,d) - closure of { [ C → d • , $ ] } corresponds to a Shift move.
goto(I0,S) - closure of { [ S’ → S • , $ ] } I7 • If there is a transition on a non-terminal
C→d•,$
S’ → S • , $ I1 then it corresponds to a GOTO move.
goto(I3,C) - closure of { [ C → cC •, c|d ] } • If a state contains the final item in the
goto(I0,C) - closure of { [ S → C • C , $ ] } I8 particular row then write the reduce move
C → cC • , c|d
S’ → C • C , $
I2 goto(I3,c) - closure of { [ C → c •C, c|d ] } CLR Parsing Table
C → • cC , $
C → c • C , c|d
C→•d,$ I3
goto(I0,c) - closure of { [ C → c • C , c|d ] } C → • cC , c|d
C → • d , c|d
C → c • C , c|d
C → • cC , c|d
I3 goto(I3,d) - closure of { [ C → d • , c|d ] }
I4
C → • d , c|d C → d • , c|d
goto(I0,d) - closure of { [ C → d • , c|d ] } goto(I6,C) - closure of { [ C → cC •, $ ] }
I4 I9
C → d • , c|d C → cC • , $
goto(I2,C) - closure of { [ S → CC • , $ ] } goto(I 6,c) - closure of { [ C → c • C , $ ] }
I5
S → CC • , $ C→c•C,$ I6
goto(I2,c) - closure of { [ C → c • C , $ ] } C → • cC , $
C→c•C,$
1. S → CC
I6 C→•d,$
C → • cC , $ goto(I6,d) - closure of { [ C I→ d • , $ ] }
2. C → cC
7
C→•d,$
C→d•,$
3. C → d
Construction of CLR Parsing Table - Example 2 :
• Construct CLR parsing table for the grammar
S→L=R|R
L → * R | id
R→L
• Step 1 : Find FIRST for all the non terminals
FIRST(S) = { * , id }
FIRST(L) = { * , id }
FIRST(R) = { * , id }
Step 2 : Number all the productions
1. S → L = R
2. S → R
3. L → * R
4. L → id
5. R → L
Example 2 –cont..
• Step 3 : Write the Augmented Grammar
goto(I0,S) – closure( { [S’ → S • , $ ] } )
S’ → S I1
S’ → S • , $
S→L=R
S→R goto(I0,L) – closure( { [S→L•=R ,$ ] [R→L• ,$ ] } )
L→*R
S→L•=R,$ I2
L → id
R→L•,$
R→L
• Step 4 : Construct the canonical collection of sets of goto(I0,R) – closure( { [S → R • , $ ] } )
LR(1) items
I3
S→R•,$
closure of { [ S’ → • S , $ ] }
S’ → • S , $
goto(I0,*) – closure( { [L → * • R , =|$ ] } )
S→•L=R,$ S’ → • S , $ L → * • R , =|$
S→•R,$ S→•L=R,$ I4
R → • L , =|$
L→•*R,= S→•R,$
I0 L → • * R , =|$
L → • id , = L → • * R , =|$
R→•L,$ L → • id , =|$ L → • id , =|$
L→•*R,$ R→•L,$ goto(I0,id) – closure( { [L → id • , =|$ ] } )
I5
L → • id , $
L → id • , =|$
Example 2 –cont..
goto(I2,=) – closure( { [S → L = • R , $ ] } ) goto(I4,id) – closure( { [L → id• , =|$ ] } )
S→L=•R,$ L → id • , =|$ I5
R→•L,$
I6 goto(I6,R) – closure( { [S → L = R • , $ ] } )
L→•*R,$
S→L=R•,$ I9
L → • id , $
goto(I6,L) – closure( { [R → L • , $ ] } )
goto(I4,R) – closure( { [L → * R • , =|$ ] } ) I10
R→L•,$
L → * R • , =|$ I7
goto(I6,*) – closure( { [L → *• R , $ ] } )

goto(I4,L) – closure( { [R → L • , =|$ ] } ) L→*•R,$

R → L • , =|$ I8 R→•L,$ I11


L → •* R , $
goto(I4,*) – closure( { [L → *• R , =|$ ] } ) L → •id , $
L → * • R , =|$ goto(I6,id) – closure( { [L → id• , $ ] } )
R → • L , =|$ I12
I4
L → id • , $
L → •* R , =|$
L → •id , =|$
Example 2 –cont..
goto(I11,R) – closure( { [L → * R • , $ ] } )
L→*R•,$ I13

goto(I11,L) – closure( { [R → L • , $ ] } )
R→L•,$ I10

goto(I11,*) – closure( { [L → *• R , $ ] } )
L→*•R,$
R→•L,$ I11
L → •* R , $
L → •id , $

goto(I11,id) – closure( { [L → id• , $ ] } )


I12
L → id • , $
CLR Parsing Table
ACTION GOTO
State
= * id $ S L R
0 s4 s5 1 2 3
1 acc
2 s6 r5
3 r2
4 s4 s5 8 7
5 r4 r4
6 s11 s12 10 9
7 r3 r3
8 r5 r5
9 r1
10 r5
11 S11 S12 10 13
12 r4
13 r3
Construction of LALR Parsing Tables
-An easy but space-consuming LALR table construction

• The table produced by


this algorithm is called
LALR parsing table
• If there are no parsing
action conflicts, then the
given grammar is said to
be an LALR(1) grammar
• The collection of sets of
items constructed is
called LALR(1) collection
Construction of LALR Parsing Table - Example 1 :
• Construct LALR parsing table for the grammar
S → CC
C → cC | d

Step 1 : Find FIRST for all the non terminals


FIRST(S) = { c , d }
FIRST(C) = { c , d }

Step 2 : Number all the productions


1. S → CC
2. C → cC
3. C → d
Example 1 –cont.. goto(I2,d) - closure of { [ C → d • , $ ] }
• Step 3 : Write the goto(I0,S) - closure of { [ S’ → S • , $ ] } I7
C→d•,$
Augmented Grammar S’ → S • , $ I1
goto(I3,C) - closure of { [ C → cC •, c|d ] }
S’ → S goto(I0,C) - closure of { [ S → C • C , $ ] }
C → cC • , c|d I8
S → CC S→C•C,$
I2 goto(I3,c) - closure of { [ C → c •C, c|d ] }
C → cC C → • cC , $
C→d C → c • C , c|d
C→•d,$ I3
• Step 4 : Construct the goto(I0,c) - closure of { [ C → c • C , c|d ] }
C → • cC , c|d
canonical collection of sets C → • d , c|d
C → c • C , c|d
of LR(1) items
C → • cC , c|d
I3 goto(I3,d) - closure of { [ C → d • , c|d ] }
I4
C → • d , c|d C → d • , c|d
closure of { [ S’ → • S , $ ] } goto(I0,d) - closure of { [ C → d • , c|d ] } goto(I6,C) - closure of { [ C → cC •, $ ] }
I4 I9
S’ → • S , $ C → d • , c|d C → cC • , $
S → • CC , $ goto(I2,C) - closure of { [ S → CC • , $ ] } goto(I 6,c) - closure of { [ C → c • C , $ ] }
C → • cC , c|d I0 I5
S → CC • , $ C→c•C,$ I6
C → • d , c|d goto(I2,c) - closure of { [ C → c • C , $ ] } C → • cC , $
C→c•C,$ I6 C→•d,$
C → • cC , $ goto(I6,d) - closure of { [ C I→ d • , $ ] }
7
C→•d,$
C→d•,$
Example 1 –cont.. Step 5 : Construct the LALR(1)
collection of items
I0:
S’ → • S , $
GOTO functions of LR(1) collection (For reference) S → • CC , $
C → • cC , c|d
C → • d , c|d LALR Parsing Table
I1:
S’ → S • , $
I2:
S→C•C,$
C → • cC , $
C→•d,$
I36:
C → c • C , c|d|$
C → • cC , c|d|$
C → • d , c|d|$
I47:
C → d • , c|d|$
I5:
S → CC • , $
I89:
C → cC • , c|d|$
Example: Parsing a string using LALR Parsing Table
Parse the string cdcd using LALR parsing
Table for the grammar given below:
1. S → CC
2. C → cC Stack Input Action
3. C → d 0 c d c d $ Shift
0 c 36 d c d $ Shift
LALR Parsing Table
0 c 36 d 47 c d $ Reduce by C → d
0 c 36 C 89 c d $ Reduce by C → cC
0C2 c d $ Shift
0 C 2 c 36 d $ Shift
0 C 2 c 36 d 47 $ Reduce by C → d
0 C 2 c 36 C 89 $ Reduce by C → cC
0C2C5 $ Reduce by S → CC
0S1 $ Accept
Construction of LALR Parsing Table - Example 2 :
• Construct LALR parsing table for the grammar
S→L=R|R
L → * R | id
R→L
• Step 1 : Find FIRST for all the non terminals
FIRST(S) = { * , id }
FIRST(L) = { * , id }
FIRST(R) = { * , id }
Step 2 : Number all the productions
1. S → L = R
2. S → R
3. L → * R
4. L → id
5. R → L
Example 2 –cont..
• Step 3 : Write the Augmented Grammar
goto(I0,S) – closure( { [S’ → S • , $ ] } )
S’ → S I1
S’ → S • , $
S→L=R
S→R goto(I0,L) – closure( { [S→L•=R ,$ ] [R→L• ,$ ] } )
L→*R
S→L•=R,$ I2
L → id
R→L•,$
R→L
• Step 4 : Construct the canonical collection of sets of goto(I0,R) – closure( { [S → R • , $ ] } )
LR(1) items
I3
S→R•,$
closure of { [ S’ → • S , $ ] }
S’ → • S , $
goto(I0,*) – closure( { [L → * • R , =|$ ] } )
S→•L=R,$ S’ → • S , $ L → * • R , =|$
S→•R,$ S→•L=R,$ I4
R → • L , =|$
L→•*R,= S→•R,$
I0 L → • * R , =|$
L → • id , = L → • * R , =|$
R→•L,$ L → • id , =|$ L → • id , =|$
L→•*R,$ R→•L,$ goto(I0,id) – closure( { [L → id • , =|$ ] } )
I5
L → • id , $
L → id • , =|$
Example 2 –cont..
goto(I2,=) – closure( { [S → L = • R , $ ] } ) goto(I4,id) – closure( { [L → id• , =|$ ] } )
S→L=•R,$ L → id • , =|$ I5
R→•L,$
I6 goto(I6,R) – closure( { [S → L = R • , $ ] } )
L→•*R,$
S→L=R•,$ I9
L → • id , $
goto(I6,L) – closure( { [R → L • , $ ] } )
goto(I4,R) – closure( { [L → * R • , =|$ ] } ) I10
R→L•,$
L → * R • , =|$ I7
goto(I6,*) – closure( { [L → *• R , $ ] } )

goto(I4,L) – closure( { [R → L • , =|$ ] } ) L→*•R,$

R → L • , =|$ I8 R→•L,$ I11


L → •* R , $
goto(I4,*) – closure( { [L → *• R , =|$ ] } ) L → •id , $
L → * • R , =|$ goto(I6,id) – closure( { [L → id• , $ ] } )
R → • L , =|$ I12
I4
L → id • , $
L → •* R , =|$
L → •id , =|$
Example 2 –cont..
goto(I11,R) – closure( { [L → * R • , $ ] } )
L→*R•,$ I13

goto(I11,L) – closure( { [R → L • , $ ] } )
R→L•,$ I10

goto(I11,*) – closure( { [L → *• R , $ ] } )
L→*•R,$
R→•L,$ I11
L → •* R , $
L → •id , $

goto(I11,id) – closure( { [L → id• , $ ] } )


I12
L → id • , $
Step 5 :
GOTO functions of LR(1) collection (For reference) Construct the LALR(1) collection
of items
I0 :
S’ → • S , $
S→•L=R,$
S→•R,$
L → • * R , =|$
L → • id , =|$ LALR Parsing Table
R→•L,$
I1 :
State Action Goto
S’ → S • , $ = * id $ S L R
I2 :
S→L•=R,$ 0 s411 s512 1 2 3
R→L•,$
I3 : 1 acc
S→R•,$
I411: 2 s6 r5
L → * • R , =|$
R → • L , =|$ 3 r2
L → • * R , =|$
L → • id , =|$ 411 s411 s512 810 713
I512:
512 r4 r4
L → id• , =|$
I6 : 6 s411 s512 810 9
S→L=•R,$
R→•L,$ 713 r3 r3
L → • * R ,$
L → • id , $ 810 r5 r5
I713:
L → * R • , =|$ 9 r1
I810:
R → L • , =|$
I9 :
S→L=R•,$
Example: Parsing a string using LALR Parsing Table
Parse the string * id = id using LALR
parsing table for the grammar given below:
1.S → L = R
2.S → R Stack Input Action
3.L → * R 0 * id = id $ Shift
4.L → id 0 * 411 id = id $ Shift
5.R → L 0 * 411 id 512 = id $ Reduce by L → id
0 * 411 L 810 = id $ Reduce by R → L
0 * 411 R 713 = id $ Reduce by L → * R
0L2 = id $ Shift
0L2=6 id $ Shift
0 L 2 = 6 id 512 $ Reduce by L → id
0 L 2 = 6 L 810 $ Reduce by R → L
0L2=6R9 $ Reduce by S → L=R
0S1 $ Accept
A comparison of SLR, CLR and LALR
• LALR (LookAhead-LR) is often used in practice, because the tables obtained by it are
considerably smaller than the canonical LR tables, yet most common syntactic constructs of
programming languages can be expressed conveniently by an LALR grammar
• For a comparison of parser size, the SLR and LALR tables for a grammar always have the same
number of states, and this number is typically several hundred states for a language like C
• The CLR table would typically have several thousand states for the same-size language
• It is much easier and more economical to construct SLR and LALR tables than the CLR tables

You might also like