Bottom Up Parser - : Canonical (CLR) Parsing
Bottom Up Parser - : Canonical (CLR) Parsing
BOTTOM UP PARSER –
CANONICAL (CLR)
PARSING
Conflict Example
2
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
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 ,$