0% found this document useful (0 votes)
49 views

Bottom Up Parser - : Canonical (CLR) Parsing

The document describes bottom up parsing using canonical LR(1) parsing. It provides examples of shift-reduce and reduce-reduce conflicts that can occur in LR parsing. It explains how canonical LR(1) parsing resolves conflicts by adding lookahead symbols to parsing items. This allows the construction of LR(1) parsing tables that are free of conflicts. An example grammar is used to demonstrate the canonical LR(1) collection process and identification of states to construct the LR(1) parsing tables.

Uploaded by

abdul rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Bottom Up Parser - : Canonical (CLR) Parsing

The document describes bottom up parsing using canonical LR(1) parsing. It provides examples of shift-reduce and reduce-reduce conflicts that can occur in LR parsing. It explains how canonical LR(1) parsing resolves conflicts by adding lookahead symbols to parsing items. This allows the construction of LR(1) parsing tables that are free of conflicts. An example grammar is used to demonstrate the canonical LR(1) collection process and identification of states to construct the LR(1) parsing tables.

Uploaded by

abdul rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1

BOTTOM UP PARSER –
CANONICAL (CLR)
PARSING
Conflict Example
2

S  L=R I0: S’  .S I1: S’  S. I6: S  L=.R I9: S  L=R.


SR S  .L=R R  .L
L *R S  .R I2: S  L.=RL .*R
L  id L  .*R R  L. L  .id
RL L  .id
R  .L I3: S  R.

I4: L  *.R I7: L  *R.


Problem R  .L
FOLLOW(R)={=,$} L .*R I8: R  L.
= shift 6 L  .id
reduce by R  L
shift/reduce conflict I5: L  id.
Conflict Example2
3

S  AaAb I0: S’  .S
S  BbBa S  .AaAb
A S  .BbBa
B A.
B.

Problem
FOLLOW(A)={a,b}
FOLLOW(B)={a,b}
a reduce by A   b reduce by A  
reduce by B   reduce by B  
reduce/reduce conflict reduce/reduce conflict
Constructing Canonical LR(1) Parsing
4
Tables
 To avoid some of invalid reductions, the states need to
carry more information.
 Extra information is put into a state by including a
terminal symbol as a second component in an item.
 A LR(1) item is:
.
A   ,a where a is the look-head of the
LR(1) item
(a is a terminal or end-marker.)
CLR(1) Parsing table
5

 S->AA
 A->aA | b

 Step 1: Augmented Grammar


 Step 2: Canonical states
Identifying the states
6
Canonical LR(1) Collection –
7
Example2
I :S’  .S,$
S’  S 0 I1:S’  S.,$ I4:L  *.R, {$,=} R to I7
1) S  L=R S  .L=R,$ S * R  .L, {$,=} L
to I8
2) S  R S  .R,$ L I2:S  L.=R,$ to I6 L .*R, {$,=} *
3) L *R L  .*R,{$,=} R  L.,$ L  .id, {$,=}
to I4
id
4) L  id R to I5
L  .id, {$,=} I3:S  R.,$ id
I5:L  id., {$,=}
5) R  L R  .L,$

I9:S  L=R.,$
R I13:L  *R.,$
I6:S  L=.R,$ to I9
L I10:R  L.,$
R  .L,$ to I10
L  .*R,$ * I4 and I11
to I11 R
L  .id,$ I11:L  *.R,$ to I13
id L
to I12 R  .L,$ to I10 I5 and I12
I7:L  *R., {$,=} L .*R,$ *
to I11
L  .id,$ id I7 and I13
I8: R  L., {$,=} to I12
I12:L  id.,$ I8 and I10
LR(1) Parsing Tables – (for
8
Example2)
id * = $ S L R
0 s5 s4 1 2 3
1 acc
2 s6 r5
3 r2 no shift/reduce or
4 s5 s4 8 7 no reduce/reduce conflict


5 r4 r4
6 s12 s11 10 9
7 r3 r3 so, it is a LR(1) grammar
8 r5 r5
9 r1
10 r5
11 s12 s11 10 13
12 r4
13 r3
Canonical LR(1) Collection --
9
Example S
S  AaAb I0: S’  .S ,$ A I1: S’  S. ,$
a to I4
S  BbBa S  .AaAb ,$
B
A S  .BbBa ,$ I2: S  A.aAb ,$b
to I5
B A  . ,a
A B  . ,b a I3: S  B.bBa ,$

I4: S  Aa.Ab ,$ I6: S  AaA.b ,$ I8: S  AaAb. ,$


A  . ,b B b

I5: S  Bb.Ba ,$ I7: S  BbB.a ,$ I9: S  BbBa. ,$


B  . ,a

You might also like