LR parser

12
LR parser

description

LR parser. Types of LR Parser. LR Parser SLR(1) or LR(0) CLR or LR(1) LALR. Construct LR parser for given grammer. E->E+T E->T T->T*F T->F F->(E) F->id. Take first production rule in I o state - PowerPoint PPT Presentation

Transcript of LR parser

Page 1: LR parser

LR parser

Page 2: LR parser

Types of LR Parser

• LR Parser– SLR(1) or LR(0)– CLR or LR(1)– LALR

Page 3: LR parser

Construct LR parser for given grammer

• E->E+T

• E->T

• T->T*F

• T->F

• F->(E)

• F->id

Page 4: LR parser

• Take first production rule in Io state

• Then if rule A->a.Bb is a rule and B->c/d is production then then add B->.c , B->.d in closure.

• In I4 state don’t expand E->.T because I2 have same one.No need to apply it.

I1

I2

I3

I4

I5

Page 5: LR parser
Page 6: LR parser
Page 7: LR parser

CLR or LR(1)

• S->CC

• C->aC

• C->d

Page 8: LR parser

• In first production add $ as its canonical collection.

• If production is [A->aBb, a1] & if there is production

B->.b1,c then add

B->.b1,c

c belongs to First(b a1)

Here S’->.S null,$

so first (null $) is $ that’s why

S->.CC,$

Here C->.aC

So first (C)={a,d}

first(C $)={a,d} that’s why

C->.aC,a/d

No need to expand I3 =goto(I3,a)

Page 9: LR parser
Page 10: LR parser

LALR CLR

Page 11: LR parser
Page 12: LR parser