Download presentation

Presentation is loading. Please wait.

Published byNataly Kemble Modified over 2 years ago

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

2
CH4.2 CSE244Items SLR (Simple LR parsing) DEF A LR(0) item is a production with a “marker.” E.g. DEF A LR(0) item is a production with a “marker.” E.g. S aA.Be intuition: it indicates how much of a certain production we 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.

3
CH4.3 CSE244 Basic Operations Augmented Grammar: 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

4
CH4.4 CSE244 GOTO function Definition. Goto(I,X) = closure of the set of all items 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 }

5
CH4.5 CSE244 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 } E’ E E E + T | T T T * F | F F ( E ) | id I 0 E’ .E E .E + T E .T T .T * F T .F F .( E ) F .id I 1 E’ E. E E. + T I 2 E T. T T. * F … I 11

6
CH4.6 CSE244 The DFA For Viable Prefixes States = Canonical Collection of Sets of Items Transitions defined by the Goto Function. All states final except I 0 I2I2 I1I1 + E I0I0 T* I3I3 I7I7 F I3I3 … … 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 arrow labeled “X” Then the closure used in the Goto functions Essentially transforms this NFA into the DFA above

7
CH4.7 CSE244Example S’ S S aABe A Abc A b B d Start with I 0 = closure(S’ .S)

8
CH4.8 CSE244 2 nd Example E’ E E E + T | T T T * F | F F ( E ) | id

9
CH4.9 CSE244 Relation to Parsing An item A 1. 2 is valid for a viable prefix 1 if we have a rightmost derivation that yields Aw which in one step yields 1 2 w 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 may tell us different things.

10
CH4.10 CSE244 Valid Items for Viable Prefixes E+T* is a viable prefix (and the DFA will be at state I 7 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 I 7 … it contains Examine state I 7 … it contains T T*.F F .(E) F .id i.e., precisely the items valid for E+T*: E’=>E=>E+T=>E+T*F E’=>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 viable prefix E+T*

11
CH4.11 CSE244 SLR Parsing Table Construction Input: the augmented grammar G’ Output: The SLR Parsing table functions ACTION & GOTO 1. 1.Construct C={I 0,..,I n } the collections of LR(0) items for G’ 2. 2.“State i” is constructed from I i If [A .a ] is in I i and goto(I i,a)=I k then we set ACTION[i,a] to be “shift k”(a is a terminal) If [A .] is in I i 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 I i then we set ACTION[i,$] = accept 3. The goto transitions for state i are constructed as follows for all A, if goto(I i,A)=I k then goto[i,A]=k all A, if goto(I i,A)=I k 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 I 0

12
CH4.12 CSE244Example. I 0 E’ .E E .E + T E .T T .T * F T .F F .( E ) F .id I 1 E’ E. E E. + T I 2 E T. T T. * F Goto(I 0, E)=I 1 Goto(I 0,T)=I 2 Goto(I 0,( )=I 4 I 4 F (.E) E .E + T E .T T .T * F T .F F .( E ) F .id Since F .( E ) is in I 0 And Goto(I 0,( )=I 4 we set ACTION(0, ( )=s4 Since E’ E. is in I 1 We set ACTION(1,$)=acc Since E T. is in I 2 and Follow(E)={$,+,) } We set ACTION(2,$)=r E T ACTION(2,+)=r E T ACTION(2,))=r E T Follow(T)=Follow(F)={ ), +, *, $ }

13
CH4.13 CSE244 3 rd example – SLR Table Construction S AB | a A aA | b B a

14
CH4.14 CSE244Conflicts Shift/Reduce Reduce/Reduce Sometimes unambiguous grammars produce multiply defined labels (s/r, r/r conflicts)in the SLR table.

15
CH4.15 CSE244 Conflict Example S’ S S L = R | R L * R | id R L

16
CH4.16 CSE244 Conflict Example S’ S S L = R | R L * R | id R L I 0 = { S’ .S, S .L = R, S .R, L .* R, L . id, R .L } I 1 = { S’ S. } I 2 = { S L. = R, R L. } I 3 = { S R. } I 4 = { L *.R, R .L, L .* R, L . id } I 5 = { L id. } I 6 = { S L =. R, R .L, L .* R, L . id } I 7 = { L *R. } I 8 = { R L. } I 9 = { S L = R. } action[2, = ] ?s 6 (because of S L. = R ) r R L (because of R L. and = follows R )

17
CH4.17 CSE244 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- ambig.) Ris not a right sentential form!!! R =id is not a right sentential form!!! Even though in general might follow R … but it does not in this case. Even though in general = might follow R … but it 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) 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…

Similar presentations

OK

CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,

CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on human chromosomes have hundreds Ppt on bbc news channel Full ppt on electron beam machining set Ppt on bluetooth based smart sensor network Ppt on ufo and aliens in california Ppt on ideal gas law definition Ppt on production planning and inventory control Ppt on ac plant Ppt on solar power satellites Ppt on structure of foreign exchange market