Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views12 pages

Unit III CLR & LALR Additional Problems

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

Unit III CLR & LALR Additional Problems

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Example:

Check whether the following grammar is CLR(1) or not.


S ’L=R|R
L ’ *R| id

R ’ L

Parse the input string id=id.


Solution: We willnumber the production rules in the grammar.
S ’ L=R rule (1)
S ’R rule (2)
L ’ *R rule (3)
L ’ id rule (4)
R ’ L rule (5)

Non-terminals =( S, L, R}
Terminals = {=,*, id }
Fist convert given grammar into augmented grammar.
Augmented grammar:
G S' ’ S

S ’ L = R

S ’R

L ’ *R

L ’ id

R ’ L

Closure:
I0: S' ’.S, $
S ’ L = R, $

S’ .R, $
L ’.*R,

L ’ jd, =
R ’.L, $

L ’.*R, $
L ’.id, $
like,
Me canewte this closure
S'S, S
S.L R, $

S .R, S
L .*R, =$
L ’.id, =/$

R ’.L, $

GOTO Functions:

GOTO0, S) GOTOI5, S, L, R, =, *, id)


I1: S' S. .S NULL

GOTOIO, L) GOTO16, S, =)
I2: S L. = R, S NULL
R’ L.. S
GOTOJ6, L)
GOTOI0, R) 19: R ’ L., $
I3: S ’ R., $
GOTOIO, =) GOTOL6, R)
NULL I10: S ’ L= R., S

GOTOI0, *) GOTOJ6, *)
14: L ’ *.R, =/$ Ill: L ’ *.R, $
R ’ .L, =/$ R ’ L, $
L ’.*R, =/$ L ’*R, $
L ’ .id, =/$ L ’.id, $
GOTOI0, id) GOTOM6, id)
I5: L ’ id., =/S
I12: L id., $
GOTOI, S, L, R)
NULL GOTOU7, S, L, R, =, * id)
NULL
GOTOUl, =, ", id)
NULL GOTOU8, s, L, R, =, *, id)
NULL
SYNI

GOTOU2, S, L, R) GOTO19, S, L, R,, *, id)


NULL NULL
GOTOU2,-) GOTO10, S, L, R, -, *, id)
S
lo: S ’ L=.R, NULL
R ’.L, $
L’.*R, S GOTOW|1, S, =)
NULL
L .id, S
GOTOU2, *, id) GOTOU1, L)
19: R ’ L., $
NULL
GOTOI11, R)
GOTOI3, S, L, R) I13: L ’ *R., S
NULL
GOTOI3, =, *, id)
GOTOI11, *)
Il1: L ’ *.R, $
NULL
R ’ .L, $
GOTOI4, S, =) L’.*R, $
NULL L ’.id, $
GOTOI4, L) GOTOI11, id)
I7: R L., =/$ I12: L ’ id., $
GOTO(L4, R) GOTO12, S, L, R, =, *, id)
I8: L ’ *R., =/S NULL

GOTOI4, *) GOTO13, S, L, R, =, *, id)


14: L *.R, =/S NULL
R ’L, =/$
L ’.*R, =/$
L ’id, =/$

GOTO(14, id)
I5: L - id., =/S

Inus we can find the match for the rule Aa: from states I| to I13 and fill up
he
action table by respective "reduce" rule.
Fall.
’ aceept action I,S|- accept)
r5 action[2,$S]-r5}
I2: R ’ L., $
(action[3,$]r2}
13: S ’ R., $
L ’ id ., =S r4 faction(5,=-T4, action[5,S]-r4:
15:

17: R ’ L., =/S ’r5 faction[7,=-r5, action[7,S]=r5)


I8: L’ *R., =/S r3 (action[8,=]3, action[8,5]-r3}
19: R ’ L., $ ’r5 {action[9,S]5}
I10: S ’ L-R., S ’r1 (action[10,S]-Tl}
I12: L ’ id., S ’r4 {action[12,S]=r4}
A13: L ’ *R., S r3 {action[13,S]-r3}
The parsing table is filling up with the shift actions (from 10 to 19), reduce actions
(from rl to r5) and also fill up the goto table for all the non-terminals. In state I0,
goto(10,S) = I1. Hence goto[0,S] = 1, similarly goto(lO,L) = 12. Hence goto[0,L] =
2. Continuing in this fashion we can fill up the goto entries of CLR parsing table.
Finally the CLR parsing table will look as:
State Action Goto
id $ L R
s4 3
2
accept
2 s6
r5
r2
4 s4 s5
5 7
r4
r4
6
sl] sl2 10
7 9
r5
NTAA ANALYSIS
3.79

r3

r5

10 rl

sll s12 13/


12 r4

13 r3

Sack implementation: Input string id=id


Stack Input Action

0 id=id $ GOTO ((I0 , id ) ==s5; shift id


0id 5 =id $ GOTO (I5, =) = r4: reduce by L ’ id
0L2 =id I$GOTO(
$ I0, L) = 2
GOTO (I2, =) =só ; shift =
0L2 = 6 id $ GOTO (I6, id ) = s12 ; shift id
0L2 = 6 id 12 SGOT0 (I12, S )= 4; reduce by L’ id
0L2 =6 L9 $GOTO( I6, L)=9
GOTO (I9, S) =r5 ; reduce by R ’L
ÞL 2 = 6 R 10 GOTO( I6, R) =10
GOTO (I10, $) = rl ; reduce by S ’ L = R
SGOTO (I0, S ) =1
GOTO(I1,S) =accept

Thus input string is parsed.


Every SLR(1) grammar is an LR(1) grammar. But the number of states of in
LRU) parsing table is too large compared to SLR parser for the same grammar.
10 reduce the number of states in LR(|) parser, we need to
introduce an
another method called LALR (Lookahead-LR) technique.
Example:
Check whether the following grammar is LALR(1) or not.
S ’L= R|R
L. ’ "R id
R ’L
Parse the input string id id.
solution: We will number the production nules in the grammar.
S LR rule (1)
SR rule (2)
L *R rule (3)
L ’ id rule (4)
R ’L rule (5)
Non-terminals = S, L, R}
Terminals ==, *, id}
Firstconvert given grammar into augmented grammar.
Augmented grammar:
S'’ S
S ’L = R

S’R
L’ *R
L ’ id

R ’ L

Closure:
I0: S ’ .S, $
S ’ L = R, $
S ’.R, $ /
L ’.*R, -/$
L ’jd, -/$
R’ L, $
GOTO Functions:
GOTOU0, S) GOTO14, id)
l,: S' ’ S. , S lç: L’ id., =/S
GOTOTO, L) GOTOI6, L)
I,: S L, = R, s lo: R ’ L., $
R ’ L., $
GOTOI6, R)
GOTO10, R) I: S ’ L = R., S
I,: S ’ R., $
GOTOI6, *)
GOTOIO, *) I,: L’ *.R, $
I,: L’ *.R, =/$ R ’.L, S
R ’ L, =/S L ’.*R, $
L ’.*R, =/$ L ’ .id, $
L ’.id, =/$ GOTOI6, id)
GOTOJO, id) I: L ’ id., S
I,: L’ id., =/$
GOTO(Il1, L)
GOTO(I2, =) L: R ’ L., $
k: S’ L = .R, $
GOTO(Il1, R)
R ’ L, $
I,: L’ *R., $
L ’.*R, $
L ’ id, $ GOTOI1, ")
I,: L ’ *.R, S
GOTOJ4, L) R ’.L, S
I,: R ’ L., =/$
L ’.*R, $
GOTOJ4, R) L ’.id, S
L,: L ’ *R., =IS
GOTOL11, id)
GOTO14, *) Ig: L ’ id., $
L,: L ’ *.R, =/$
R ’ .L, =/$
L ’.*R, =/S
L’.id, =/S
From above set of items we have got,

0 l,and l giv same production but lookaheads are different. Hence merge them
o fom l
Hence ls12
() 1.3 Hence l13
Hence lg10
Therefore the set of items for LALR are

GOTOJO, S) GOTOU2, =)
1: S S..s I; S ’’L=
L = R, $
R ’ L, $
GOTO(O. L) L’.*R, $
I: S L. = R. S
L ’ .id, $
R ’ L., S
GOTOI4, L)
GOTO10. R) Lo: R ’ L., =/S
L: S’ R.. S
GOTO14, R)
GOTOIO. *) Ig13: L ’ *R., =/S
I: L ’ *.R, =/S
R ’ .L, =/$ GOTOJ, R)
L ’.*R, =/S Io: S -’L =R., S
L ’.id, =/S

GOTOTO, id)
Igg: L ’ id., =/$

tinally, the reduce" rules are


S' ’ S., $ ’ accept faction[ 1,$]-accept}
R’ L., $ (action(2,S] r5}
S’ R., $ ’ r2 (action(3,$]-r2}
's12: L ’ id .. -/$ ’ r4 Kaction[5 I2,-| r4, action[S 12,S]-r4}
3 88
taction| 79., ]r5, action| 79,s] r5:
(action( 813,-]-r3. action[8|3.S]-r3;
1R.
(action( 10,S]-rl;
SR., S rl

as: -
Einaily the L4LR persing table will look
Action
Goto
State
ld S S L R

s$12 2 3

accept

r5

r2

411 s411 s$12 79

S12 r4

s411 s$12 79

r5
813 r3
10
rl

Stack implementation: Input string id=id


Stack
Input Action

GOTO ( 10, id ) = s$12: shift id

GOTO (I512,-)= H reduee hy l


0 L2
(GOTO|0, L)
NTAA ANALYSIS
3.89

id S GOTO ( l6, id ) = s512: shift id


GOTO (1512,S) = r4 ; reduce by L ’ id
L=ó L 79 GOTO( 16, L ) = 79
GOTO ( 179,S ) = r5; reduce by R ’L
0L= 6 R 10 GOTO( I6, R) =10
GOTO ( I10, S )=rl:reduce by S ’ L = R
GOTO ( I0,S) =|
GOTO ( II,S) = accept
Thus input string is parsed.
37.23 Comparison of LR Parsers
It's atime to compare SLR, LALR and LR parser for the common factors such
25 size. class of CFG, efficiency and cost in terms of time and space.
SL.So SLR parser LALR parser Canonical LR parser
1. SLR parser is smallest in The LALR and SLR LR parser or canonical
SIZe. have the same size. LR parser is largest in
size.
It is an easiest mnethod This method is applicable| This method is most
based on FOLLOW to wider class than SLR powerful than SLR
funcion. and LALR

This method exposes less Most of the syntactic This method exposes
syntactic features than features of a language less syntactic features
that of LR parsers. are expressed in LALR. than that of LR
parsers.
Error detection is not Error detection is not Immediate error
immediate in SLR. immediate in LALR. detection is done by
LR parsers.
rcquires less time The time and space The time and space
and space complexity. complexity is more in complexity is more for
LALR but cflicient. canonical LR parser.
methods exist for
constructing LALR
parsers directly,

You might also like