cs453 lr 1 lalr ambiguity
play

CS453 LR(1), LALR, AMBIGUITY CS453 Shift-Reduce Cont' 1 LR(1), - PowerPoint PPT Presentation

CS453 LR(1), LALR, AMBIGUITY CS453 Shift-Reduce Cont' 1 LR(1), LALR, Ambiguity The plan: Shift reduce parsing LR(1) and LALR Ambiguous grammars - Precedence and associativity rules - Dangling else Java-cup precedence and associativity


  1. CS453 LR(1), LALR, AMBIGUITY CS453 Shift-Reduce Cont' 1

  2. LR(1), LALR, Ambiguity The plan: Shift reduce parsing LR(1) and LALR Ambiguous grammars - Precedence and associativity rules - Dangling else Java-cup precedence and associativity declarations Shift Reduce conflict in dangling else CS453 Shift-Reduce Cont' 2

  3. SLR still not good enough wrt reduce Grammar for C style assignment: 0: S’ à à S$ 1: S à à V = E 2: S à à E 3: E à à V 4:V à à x 5: V à à *E E is a simplified expression, in reality more complex (E+T …) Let’s build the SLR parse table: x * = $ S V E Is this grammar ambiguous? 1 s5 s6 g2 g3 g4 2 a NO 3 s7,r3 4 r2 But we still have a 5 r4 r4 shift reduce conflict. 6 s5 s6 g9 g8 7 s5 s6 g9 g10 8 r5 r5 We need to look ahead and 9 r3 r3 decide when to reduce. 10 r1 r1 CS453 Shift-Reduce Cont' 3

  4. LR(1) look ahead sets LR(1) items take a look ahead into account: (A à à α . β , z ) item is pair: (dotted rule, look-ahead symbol ) indicating α is on top of stack, and β z on input, but z is not part of A We will now reduce to A, not on follow(A), but if αβ is on top of stack and z on input, i.e. we have more precise information. Closure(I)= Reduce: Goto(I,X)= repeat R ={} J={} for all (A à α . X β , z ) in I for all (A à α .) in I for all (A à α . X β , z ) in I for any X à γ R+= (I, z, A à α ) J+=(A à α X . β , z ) for any w in First( β z) return Closure(J) I+= (X à . γ , w) (I, z, A à α ) means: until I does no change in State I, return I on symbol z reduce: A à α CS453 Shift-Reduce Cont' 4

  5. LR(1) state diagram: look-ahead sets 0: S’ à à S$ 1: S à à V = E 2: S à à E 3: E à à V 4:V à à x 5: V à à *E We will not shift the $, so the look ahead symbol for S’ does not matter This is indicated by a ? S’ à à . S$ ? when we have S’ à à . S$ ? S à à . V=E $ the same dotted S à à . V=E $ S à à . E $ rule with S à à . E $ E à à . V $ different E à à . V $ V à à . x $ look aheads V à à . x $, = V à à . *E $ we combine V à à . *E $, = V à à . x = them in V à à . *E = sets CS453 Shift-Reduce Cont' 5

  6. LR(1) state diagram (incomplete) 0: S’ à à S$ 1: S à à V = E 2: S à à E 3: E à à V 4:V à à x 5: V à à *E S’ à . S$ ? S S’ à S . $ ? S à . V=E $ 2 S à . E $ E à . V $ V S à V = . E $ = S à V . = E $ V à . x $, = E à . V $ E à V . $ V à . *E $,= V à .x $ 3 1 V à . *E $ In state 3: 4 we reduce E à à V on $, not on = ( even though = is in follow(E) ) we shift on = conflict resolved CS453 Shift-Reduce Cont' 6

  7. LALR(1) Look Ahead LR(1) : state diagram (incomplete) 0: S’ à à S$ 1: S à à V = E 2: S à à E 3: E à à V 4:V à à x 5: V à à *E S’ à . S$ ? S S’ à S . $ ? S à . V=E $ 2 S à . E $ V S à V = . E $ = E à . V $ S à V . = E $ E à . V $ V à . x $, = E à V . $ V à .x $ V à . *E $,= 3 V à . *E $ 1 x x 4 LALR combines states that are equal V à x . $,= V à x . $ except for the 5 6 look aheads CS453 Shift-Reduce Cont' 7

  8. LALR(1) Look ahead LR(1) : state diagram (incomplete) 0: S’ à à S$ 1: S à à V = E 2: S à à E 3: E à à V 4:V à à x 5: V à à *E S’ à . S$ ? S S’ à S . $ ? S à . V=E $ 2 S à . E $ V S à V = . E $ = E à . V $ S à V . = E $ E à . V $ V à . x $, = E à V . $ V à .x $ V à . *E $,= 3 V à . *E $ 1 x x 4 V à x . $,= 5 CS453 Shift-Reduce Cont' 8

  9. Shift reduce: ambiguous expression grammar (incomplete) 0: S ‘ à E$ 1: E à E+E 2: E à E*E 3: E à (E) 4: E à id E + E S’ à E.$ E à E+.E E à E +E. S’ à . E$ E à E . +E E à .E+E E à E.+E E à . E+E E à E . *E E à .E*E E à E.*E E à . E*E 2 3 4 … * 1 E E à E+.E E à E *E. State 4: two Shift/Reduce conflicts E à .E+E E à E.+E input +: R1 Why? E à .E*E E à E.*E input *: S Why? 5 6 NOTICE: State 6: two Shift/Reduce conflicts input +: R2 Why? + and * are left associative input *: R2 Why? CS453 Shift-Reduce Cont' 9

  10. Shift reduce: ambiguous expression grammar (incomplete) 0: S ‘ à E$ 1: E à E^E 2: E à id // ^ for exponent E ^ E S’ à E.$ E à E^ .E E à E ^ E. S’ à . E$ E à E . ^E E à .E^E E à E.^E E à . E^E 2 3 4 … 1 State 4: two Shift/Reduce conflicts input ^ : S Why? CS453 Shift-Reduce Cont' 10

  11. Shift reduce: ambiguous dangling else grammar 1: S à if c S else S 2: S à if c S 3: S à other abstracted to: 1: S à i S e S 2: i S 3: o i S S à i . S e S S à i S . e S S’ à . S$ S à i . S S à i S . S à . i S e S … S à . i S 4 5 S à .o State 5: Shift/Reduce conflict 1 o input e: S Why? S à o. 3 Is else left or right associative? CS453 Shift-Reduce Cont' 11

  12. Shift reduce: ambiguous cast grammar 1: E à E+E 2: E à ( byte ) E 3: E à ( E ) 4: E à id E à E . + E E à . E+E … E à ( byte ) E . E à . ( byte ) E E à . ( E ) E à . id Shift/Reduce conflict input +: R2 Why? ( type ) is a right associative cast operator, but consists of three tokens. This is a place where the strict nesting of ( ) does not hold. The conflict can be resolved by declaring ( ) a right associative unary operator with higher precedence than the other operators. CS453 Shift-Reduce Cont' 12

  13. Ambiguity in JavaCUP There is no conflict free LR parser for ambiguous grammars - So we have to fix the problem in other ways Expressions: operators and productions are given precedence – Tokens explicitly in precedence declarations – Productions implicitly based on last operator in the stack When in shift-reduce conflict – Shift if look ahead token has higher precedence than production on stack – Reduce if operator in the stack has higher precedence than look ahead Within same precedence level – Left associativity results in a reduce – Right associativity results in a shift Dangling else - Shifting the else will bind else to last unbound if (Java semantics) - Not shifting can cause a valid program to be rejected CS453 Shift-Reduce Cont' 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend