17
CH4.1 CSE244 SLR Parsing SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs, CT 06269-1155 [email protected] http://www.cse.uconn.edu/~akiayias

SLR Parsing

Embed Size (px)

DESCRIPTION

SLR Parsing. Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs, CT 06269-1155. [email protected] http://www.cse.uconn.edu/~akiayias. Items. SLR (Simple LR parsing) - PowerPoint PPT Presentation

Citation preview

Page 1: SLR Parsing

CH4.1

CSE244

SLR ParsingSLR Parsing

Aggelos KiayiasComputer Science & Engineering Department

The University of Connecticut371 Fairfield Road, Box U-155

Storrs, CT [email protected]

http://www.cse.uconn.edu/~akiayias

Page 2: SLR Parsing

CH4.2

CSE244

ItemsItems

SLR (Simple LR parsing)SLR (Simple LR parsing) DEF A DEF A LR(0) item LR(0) item is a production with a “marker.”is a production with a “marker.”

E.g. E.g. S aA.Beintuition: it indicates how much of a certain productionwe have seen already (up to the point of the marker)

CENTRAL IDEA OF SLR PARSING: construct a DFA that recognizes viable prefixes of the grammar.

Intuition: Shift/Reduce actions can be decided based on this DFA (what we have seen so far & what are our next options).

Use “LR(0) Items” for the creation of this DFA.

Page 3: SLR Parsing

CH4.3

CSE244

Basic OperationsBasic Operations

Augmented Grammar:Augmented Grammar:E’ EE E + T | T

T T * F | F F ( E ) | id

CLOSURE OPERATION of a set of Items:

Function closure(I){ J=I;

repeat for each A .B in J and each produtcion B of G such that B. is not in J: ADD B. to J

until … no more items can be added to Jreturn J

}EXAMPLE consider I={ E’.E }

E E + T | T

T T * F | FF ( E ) | id

Page 4: SLR Parsing

CH4.4

CSE244

GOTO functionGOTO function

Definition.Definition.Goto(I,X) = closure of the set of all items Goto(I,X) = closure of the set of all items A X. where A .X belongs to I

Intuitively: Goto(I,X) set of all items that “reachable” from the items of I once X has been “seen.”

E.g. consider I={E’ E. , E E.+T} and compute Goto(I,+)

Goto(I,+) = { E E+.T, T .T * F , T .F , F .( E ) , F .id }

Page 5: SLR Parsing

CH4.5

CSE244

The Canonical Collections of Items for GThe Canonical Collections of Items for G

Procedure Items(G’:augmented grammar) { C:={ closure [S’ .S] }

repeatfor each set of items I in C and each grammar symbol Xsuch that goto(I,X) is not empty and not

in Cdo add goto(I,X) to C

until no more sets of items can be added to C}

E’ EE E + T | T

T T * F | FF ( E ) | id

I0 E’ .EE .E + T E .T

T .T * F T .FF .( E ) F .id

I1 E’ E.E E. + T

I2

E T.

T T. * F

…I11

Page 6: SLR Parsing

CH4.6

CSE244

The DFA For Viable PrefixesThe DFA For Viable Prefixes

States = Canonical Collection of Sets of ItemsStates = Canonical Collection of Sets of Items Transitions defined by the Goto Function.Transitions defined by the Goto Function. All states final except IAll states final except I00

I2I1

+EI0

T *I3 I7

F

I3…… Look p. 226

Intuition: Imagine an NFA with states all the itemsin the grammar and transitions to be of the form:“A .X” goes to “A X.” with an arrowlabeled “X”Then the closure used in the Goto functions Essentially transforms this NFA into the DFA above

Page 7: SLR Parsing

CH4.7

CSE244

ExampleExample

S’ S S aABe A Abc A b B d

Start with I0 = closure(S’ .S)

Page 8: SLR Parsing

CH4.8

CSE244

22ndnd Example Example

E’ EE E + T | T

T T * F | FF ( E ) | id

Page 9: SLR Parsing

CH4.9

CSE244

Relation to ParsingRelation to Parsing

An item A 1.2 is valid for a viable prefix1 if we have a rightmost derivation that yields Aw which in one step yields 12w

An item will be valid for many viable prefixes.

Whether a certain item is valid for a certain viable prefix it helps on our decision whether to shift or reduce when 1 is on the stack.

If 2 looks like we still need to shift.

If 2= it looks like we should reduce A 1

It could be that two valid items mayIt could be that two valid items maytell us different things.tell us different things.

Page 10: SLR Parsing

CH4.10

CSE244

Valid Items for Viable PrefixesValid Items for Viable Prefixes

E+T* is a viable prefix (and the DFA will be at E+T* is a viable prefix (and the DFA will be at state Istate I77 after reading it) after reading it)

Indeed: Indeed: E’=>E=>E+T=>E+T*F is a rightmost derivation, T*F is the handle of E+T*F, thus E+T*F is a viable prefix, thus E+T* is also.

Examine state IExamine state I77 … it contains … it containsT T*.FF .(E)F .id

i.e., precisely the items valid for E+T*:E’=>E=>E+T=>E+T*FE’=>E=>E+T=>E+T*F=>E+T*(E)E’=>E=>E+T=>E+T*F=>E+T*id

There are no other valid items for for the viableprefix E+T*

Page 11: SLR Parsing

CH4.11

CSE244

SLR Parsing Table ConstructionSLR Parsing Table Construction

Input: the augmented grammar G’Output: The SLR Parsing table functions ACTION & GOTO

1. Construct C={I0,..,In} the collections of LR(0) items for G’

2. “State i” is constructed from Ii

If [A .a] is in Ii and goto(Ii,a)=Ik then we setACTION[i,a] to be “shift k” (a is a terminal)If [A .] is in Ii then we set ACTION[i,a] to reduce “A”

for all a in Follow(A) --- (note: A is not S’)If [S’ S.] is in Ii then we set ACTION[i,$] = accept

3. The goto transitions for state i are constructed as follows 3. The goto transitions for state i are constructed as follows forfor

all A, if goto(Iall A, if goto(Iii,A)=I,A)=Ikk then goto[i,A]=k then goto[i,A]=k4. All entries not defined by rules (2) and (3) are made 4. All entries not defined by rules (2) and (3) are made

“error”“error”5. The initial state of the parser is the one constructed from 5. The initial state of the parser is the one constructed from

the the

set of items Iset of items I00

Page 12: SLR Parsing

CH4.12

CSE244

Example.Example.

I0 E’ .EE .E + T E .T

T .T * F T .FF .( E ) F .id

I1 E’ E.E E. + T

I2

E T.

T T. * F

Goto(I0, E)=I1

Goto(I0,T)=I2

Goto(I0,( )=I4

I4 F (.E)E .E + T E .T

T .T * F T .FF .( E ) F .id

Since F .( E ) is in I0

And Goto(I0,( )=I4

we set ACTION(0, ( )=s4

Since E’ E. is in I1

We set ACTION(1,$)=acc

Since E T. is in I2 andFollow(E)={$,+,) }We set ACTION(2,$)=rE T

ACTION(2,+)=rE T

ACTION(2,))=rE T

Follow(T)=Follow(F)={ ) , + , * , $ }

Page 13: SLR Parsing

CH4.13

CSE244

33rdrd example – SLR Table Construction example – SLR Table Construction

S AB | a

A aA | bB a

Page 14: SLR Parsing

CH4.14

CSE244

ConflictsConflicts

Shift/ReduceShift/Reduce Reduce/ReduceReduce/Reduce

Sometimes unambiguous grammars produce multiply defined labels (s/r, r/r conflicts)in the SLR table.

Page 15: SLR Parsing

CH4.15

CSE244

Conflict ExampleConflict Example

S’ SS L = R | R

L * R | idR L

Page 16: SLR Parsing

CH4.16

CSE244

Conflict ExampleConflict Example

S’ SS L = R | R

L * R | idR L

I0 = {S’ .S , S .L = R , S .R , L .* R ,

L . id , R .L}I1 = {S’ S. }

I2 = {S L . = R , R L . }I3 = {S R.}I4 = {L *.R , R .L , L .* R , L . id } I5 = {L id. }I6 = {S L = . R , R .L , L .* R , L . id }I7 = {L *R. }

I8 = {R L. }

I9 = {S L = R. }action[2, = ] ? s 6(because of S L . = R )r R L

(because of R L . and = follows R)

Page 17: SLR Parsing

CH4.17

CSE244

But Why?But Why?

Let’s consider a string that will exhibit the conflict. Let’s consider a string that will exhibit the conflict. id=id

What is the correct move? (recall: grammar is non-What is the correct move? (recall: grammar is non-ambig.)ambig.)

RR=id is not a right sentential form!!!is not a right sentential form!!! Even though in general Even though in general = might follow R … but it might follow R … but it

does not in this case.does not in this case. ……Actually it does only when R is preceded by Actually it does only when R is preceded by * SLR finds a conflict because using Follow + LR(0) SLR finds a conflict because using Follow + LR(0)

items as the guide to find when to reduce is not the items as the guide to find when to reduce is not the best method.best method.

$0 id=id$ s5$0id5 =id$ r L id

$0L2 =id$ conflict…