CC LR Parser
CC LR Parser
Mrs.Soma Ghosh
[email protected]
Introduction
id id F id T*F
id F id
id
Shift-reduce parser
• Basic operations:
• Shift
• Reduce Stack Input Action
• Accept
• Error $ id*id$ shift
• Example: id*id $id *id$ reduce by F->id
$F *id$ reduce by T->F
$T *id$ shift
$T* id$ shift
$T*id $ reduce by F->id
$T*F $ reduce by T->T*F
$T $ reduce by E->T
$E $ accept
Handle will appear on top of the
stack
S S
A
B
B A
α β γ z α γ z
y x y
Stack Input Stack Input
$αβγ yz$ $αγ xyz$
$αβB yz$ $αBxy z$
$αβBy z$
Conflicts during shit reduce parsing
Stack Input
… if expr then stmt else …$
Reduce/reduce conflict
SetOfItems CLOSURE(I) {
J=I;
repeat
for (each item A->α.Bβ in J)
for (each production B->γ of G)
if (B->.γ is not in J)
add B->.γ to J;
until no more items are added to J on one round;
return J;
GOTO algorithm
SetOfItems GOTO(I,X) {
J=empty;
if (A->α.X β is in I)
add CLOSURE(A-> αX. β ) to J;
return J;
}
Canonical LR(0) items
Void items(G’) {
C= CLOSURE({[S’->.S]});
repeat
for (each set of items I in C)
for (each grammar symbol X)
if (GOTO(I,X) is not empty and not in C)
add GOTO(I,X) to C;
until no new set of items are added to C on a round;
}
E’->E
E -> E + T | T I4 I3
T -> T * F | F
F -> (E) | id acc ( F
$ I6 I9
I1
E->E+.T T
T->.T*F
E’->E. + T->.F
E->E+T.
T->T.*F
I0=closure E E->E.+T
F->.(E)
F->.id *
({[E’->.E]})
E’->.E I2
T I7
F
I10
E->.E+T E->T. * T->T*.F
E->.T F->.(E) T->T*F.
T->.T*F
T->T.*F id F->.id
T->.F id
I5
F->.(E) (
F->.id
( F->id. +
I4
F->(.E)
E I8 I11
E->.E+T
E->.T E->E.+T )
T->.T*F F->(E.) F->(E).
F ( T->.F
F->.(E)
F->.id
T
F
I3 I2
T>F.
STATE ACTON GOTO
id + * ( ) $ E T F
0 S5 S4 1 2 3
1 S6 Acc
2 R2 S7 R2 R2
3 R4 R4 R4 R4
4 S5 S4 8 2 3
5 R6 R6 R6 R6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 R1 S7 R1 R1
10 R3 R3 R3 R3
11 R5 R5 R5 R5
Use of LR(0) automaton
• Example: id*id
INPUT a1 … ai … an $
(0) E’->E
(1) E -> E + T
(2) E-> T
(3) T -> T * F
(4) T-> F
(5) F -> (E)
(6) F->id
Example id*id+id?
Line Stac Symbol Input Action
k s
STAT ACTON GOTO
E (1) 0 id*id+id$ Shift to 5
id + * ( ) $ E T F
(2) 05 id *id+id$ Reduce by F-
0 S5 S4 1 2 3 >id
1 S Ac (3) 03 F *id+id$ Reduce by T->F
6 c
2 R S R2 R2 (4) 02 T *id+id$ Shift to 7
2 7
(5) 027 T* id+id$ Shift to 5
3 R R R4 R4
(6) 0275 T*id +id$ Reduce by F-
4 4
>id
4 S5 S4 8 2 3
(7) 0271 T*F +id$ Reduce by T-
5 R R R6 R6 0 >T*F
6 6 (8) 02 T +id$ Reduce by E-
6 S5 S4 9 3 >T
(9) 01 E +id$ Shift
7 S5 S4 10
(10) 016 E+ id$ Shift
8 S S1
6 1 (11) 0165 E+id $ Reduce by F-
>id
9 R S R1 R1
1 7 (12) 0163 E+F $ Reduce by T->F
10 R R R3 R3
3 3 (13) 0169 E+T` $ Reduce by E-
>E+T
11 R R R5 R5
5 5 (14) 01 E $ accept
Constructing SLR parsing table
• Method
• Construct C={I0,I1, … , In}, the collection of LR(0) items for
G’
• State i is constructed from state Ii:
• If [A->α.aβ] is in Ii and Goto(Ii,a)=Ij, then set ACTION[i,a] to “shift j”
• If [A->α.] is in Ii, then set ACTION[i,a] to “reduce A->α” for all a in
follow(A)
• If {S’->.S] is in Ii, then set ACTION[I,$] to “Accept”
• If any conflicts appears then we say that the grammar is not
SLR(1).
• If GOTO(Ii,A) = Ij then GOTO[i,A]=j
• All entries not defined by above rules are made “error”
• The initial state of the parser is the one constructed from the
set of items containing [S’->.S]
Example grammar which is not
SLR(1)
S -> L=R | R
L -> *R | id
R -> L
I0 I1 I3 I5 I7
S’->.S S’->S. S ->R. L -> id. L -> *R.
S -> .L=R
S->.R I2 I4 I6
I8
L -> .*R | S ->L.=R L->*.R S->L=.R
R -> L.
L->.id R ->L. R->.L R->.L
R ->. L L->.*R L->.*R I9
L->.id L->.id S -> L=R.
Action
=
Shift 6
2 Reduce R->L
More powerful LR parsers
SetOfItems Goto(I,X) {
initialize J to be the empty set;
for (each item [A->α.Xβ,a] in I)
add item [A->αX.β,a] to set J;
return closure(J);
}
void items(G’){
initialize C to Closure({[S’->.S,$]});
repeat
for (each set of items I in C)
for (each grammar symbol X)
if (Goto(I,X) is not empty and not in C)
add Goto(I,X) to C;
until no new sets of items are added to C;
}
Example
S’->S
S->CC
C->cC
C->d
Canonical LR(1) parsing table
• Method
• Construct C={I0,I1, … , In}, the collection of LR(1) items for
G’
• State i is constructed from state Ii:
• If [A->α.aβ, b] is in Ii and Goto(Ii,a)=Ij, then set ACTION[i,a] to “shift j”
• If [A->α., a] is in Ii, then set ACTION[i,a] to “reduce A->α”
• If {S’->.S,$] is in Ii, then set ACTION[I,$] to “Accept”
• If any conflicts appears then we say that the grammar is not
LR(1).
• If GOTO(Ii,A) = Ij then GOTO[i,A]=j
• All entries not defined by above rules are made “error”
• The initial state of the parser is the one constructed from the
set of items containing [S’->.S,$]
Example
S’->S
S->CC
C->cC
C->d
LALR Parsing Table
I4
C->d. , c/d
I47
C->d. , c/d/$
I7
C->d. , $
E->E+E id + * ( ) $ E
E->E*E 0 S3 S2 1
E->(E) 1 S4 S5 Acc
2 S3 S2 6
E->id
3 R4 R4 R4 R4
4 S3 S2 7
5 S3 S2 8
6 S4 S5
I0: E’->.E I1: E’->E. I2: E->(.E)
E->.E+E E->E.+E E->.E+E 7 R1 S5 R1 R1