l rparser edos

26
LR Parsers Canonical Collection LR

Upload: guest192f06

Post on 29-Jun-2015

273 views

Category:

Technology


2 download

DESCRIPTION

Aho, Ullman

TRANSCRIPT

Page 1: L Rparser Edos

LR Parsers

Canonical Collection LR

Page 2: L Rparser Edos

LR Parser1

Tokens

•Driver Routine

STACK

Prg Ejem1 ; Real X2

Fig. 6.2 Aho, Ullman. “Principles of Compiler Design” pág. 199

ParsingTable

eof…

Page 3: L Rparser Edos

Driver Routine

Determines Sn, the state on top of stack

Uses the current token ai

Consults Table[Sn , ai ] Edo .Shift sReduceAcceptError

Page 4: L Rparser Edos

Shift

The parser executes a shift move:

Stack EdoShifts the current token

Page 5: L Rparser Edos

Reduce

Consults the specific production: A ß If n is the lenght of ß Pop n symbols off the stack Push A onto the stack Note: the current token is not changed in a

reduce move

Page 6: L Rparser Edos

Accept

IF Table[Sn , ai] = accept, parsing is completed

Page 7: L Rparser Edos

Error

IF Table[Sn , ai] = error, the parser has discovered an error and calls an error recovery routine

Page 8: L Rparser Edos

How to construct a simple LR2

Procedure Closure(P)Begin repeat for each item A@♥Bß in P and each production B ¥ in G such that B ♥ ¥ is not in P do add B ♥ ¥ to P until no more items can be aded to P; return P;end

IBID. Fig. 6.5 pág. 207

Page 9: L Rparser Edos

How to construct a simple LR2

Procedure ITEMS(G’)Begin

C := {CLOSURE(S’ ♥S)};

repeat

for each set of items P in C and each grammar symbol X

such that GOTO(P,X) is not empty and is not in C

do add GOTO(P,X) to C

until no more sets of items can be added to C

end

IBID. Fig. 6.5 pág. 207

Page 10: L Rparser Edos

Example: G is a grammar with start symbol E

EE + TETTT/FTFF(E)Fid

Page 11: L Rparser Edos

G’ is the augmented grammar with a new start symbol E’

E’ EEE + TETTT/FTFF(E)Fid

Blue nucleous

Page 12: L Rparser Edos

Closure(E’)

E’♥E

Blue nucleous

Page 13: L Rparser Edos

Closure(E)

E’♥EE♥E + TE♥T

Page 14: L Rparser Edos

Closure(T)

E’♥EE♥E + TE♥TT♥T/FT♥F

Page 15: L Rparser Edos

Closure(F)

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Page 16: L Rparser Edos

State 1

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Page 17: L Rparser Edos

State 2=GoTo(1,E)

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

Page 18: L Rparser Edos

State 3 =GoTo(1,T)

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

Page 19: L Rparser Edos

State 4=GoTo(1,F)

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

Page 20: L Rparser Edos

State 5: from state 1 with (

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

F(♥E)E♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Page 21: L Rparser Edos

State 6:from state1 with id

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

F(♥E)E♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Fid♥

Page 22: L Rparser Edos

States 1,2 ,3,4,5,6

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

F(♥E)E♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Fid♥

Page 23: L Rparser Edos

States 7,8,9

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

F(♥E)E♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Fid♥

E’E+♥TT♥T/FT♥FF♥(E)F♥id

T’T/♥FF♥(E)F♥id

F(E♥)EE♥+T

Page 24: L Rparser Edos

States 10,11,12

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

F(♥E)E♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Fid♥

E’E+♥TT♥T/FT♥FF♥(E)F♥id

T’T/♥FF♥(E)F♥id

F(E♥)EE♥+T

E’E+T♥TT♥/F

T’T/F♥

F(E)♥

Page 25: L Rparser Edos

States 1,2,3,4,... 12

E’♥EE♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

E’E♥EE♥ + T

E’T♥TT♥ /F

TF♥

F(♥E)E♥E + TE♥TT♥T/FT♥FF♥(E)F♥id

Fid♥

E’E+♥TT♥T/FT♥FF♥(E)F♥id

T’T/♥FF♥(E)F♥id

F(E♥)EE♥+T

E’E+T♥TT♥/F

T’T/F♥

F(E)♥

Page 26: L Rparser Edos

END

Canonical Collection LR