- 168 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' SLR Parsing' - dara-holcomb

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

SLR Parsing

Aggelos Kiayias

Computer Science & Engineering Department

The University of Connecticut

371 Fairfield Road, Box U-155

Storrs, CT 06269-1155

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

Items

- SLR (Simple LR parsing)
- DEF A LR(0) item is a production with a “marker.”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.

Basic Operations

- Augmented Grammar: E’ E E 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 J

return J

}

EXAMPLE consider I={ E’.E }

E E + T | T

T T * F | F

F ( E ) | id

GOTO function

- Definition.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 }

The Canonical Collections of Items for G

Procedure Items(G’:augmented grammar)

{ C:={ closure [S’ .S] }

repeat

for each set of items I in C and each

grammar symbol X

such that goto(I,X) is not empty and not in C

do add goto(I,X) to C

until no more sets of items can be added to C

}

I0

E’ .EE .E + T

E .T

T .T * F

T .F

F .( E )

F .id

I1

E’ E.E E. + T

I2

E T.

T T. * F

E’ EE E + T | T

T T * F | F

F ( E ) | id

…I11

The DFA For Viable Prefixes

E

I0

- States = Canonical Collection of Sets of Items
- Transitions defined by the Goto Function.
- All states final except I0

+

T

*

I1

I2

I3

I7

F

I3

…

…

Look p. 226

Intuition: Imagine an NFA with states all the items

in 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

Relation 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 maytell us different things.

Valid Items for Viable Prefixes

- E+T* is a viable prefix (and the DFA will be at state I7 after reading it)
- 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 I7 … 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*

SLR Parsing Table Construction

Input: the augmented grammar G’

Output: The SLR Parsing table functions ACTION & GOTO

- Construct C={I0,..,In} the collections of LR(0) items for G’
- “State i” is constructed from Ii
If [A .a] is in Ii and goto(Ii,a)=Ik then we set

ACTION[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 for

all A, if goto(Ii,A)=Ik then goto[i,A]=k

4. All entries not defined by rules (2) and (3) are made “error”

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

set of items I0

Example.

I0

E’ .E

E .E + T

E .T

T .T * F

T .F

F .( E )

F .id

I1

E’ E.

E E. + T

I2

E T.

T T. * F

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 and

Follow(E)={$,+,) }

We set ACTION(2,$)=rE T

ACTION(2,+)=rE T

ACTION(2,))=rE T

Goto(I0, E)=I1

Goto(I0,T)=I2

Goto(I0,( )=I4

I4

F (.E)

E .E + T

E .T

T .T * F

T .F

F .( E )

F .id

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

Conflicts

- Shift/Reduce
- Reduce/Reduce

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

Conflict Example

S’ SS L = R | R

L * R | id

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

But Why?

- Let’s consider a string that will exhibit the conflict. id=id
- What is the correct move? (recall: grammar is non-ambig.)
- R=id is not a right sentential form!!!
- Even though in general = might follow R … but it does not in this case.
- …Actually it does only when R is preceded by *
- SLR finds a conflict because using Follow + LR(0) items as the guide to find when to reduce is not the best method.

$0 id=id$ s5

$0id5 =id$ r L id

$0L2 =id$ conflict…

Download Presentation

Connecting to Server..