L Rparser Edos

Preview:

DESCRIPTION

Aho, Ullman

Citation preview

LR Parsers

Canonical Collection LR

LR Parser1

Tokens

•Driver Routine

STACK

Prg Ejem1 ; Real X2

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

ParsingTable

eof…

Driver Routine

Determines Sn, the state on top of stack

Uses the current token ai

Consults Table[Sn , ai ] Edo .Shift sReduceAcceptError

Shift

The parser executes a shift move:

Stack EdoShifts the current token

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

Accept

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

Error

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

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

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

Example: G is a grammar with start symbol E

EE + TETTT/FTFF(E)Fid

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

E’ EEE + TETTT/FTFF(E)Fid

Blue nucleous

Closure(E’)

E’♥E

Blue nucleous

Closure(E)

E’♥EE♥E + TE♥T

Closure(T)

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

Closure(F)

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

State 1

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

State 2=GoTo(1,E)

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

E’E♥EE♥ + T

State 3 =GoTo(1,T)

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

E’E♥EE♥ + T

E’T♥TT♥ /F

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♥

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

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♥

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♥

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

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)♥

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)♥

END

Canonical Collection LR