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

cs453 lr 1 lalr ambiguity
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS453 LR(1), LALR, AMBIGUITY

CS453 Shift-Reduce Cont' 1

slide-2
SLIDE 2

CS453 Shift-Reduce Cont' 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

slide-3
SLIDE 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:

CS453 Shift-Reduce Cont' 3

x * = $ S V E 1 s5 s6 g2 g3 g4 2 a 3 s7,r3 4 r2 5 r4 r4 6 s5 s6 g9 g8 7 s5 s6 g9 g10 8 r5 r5 9 r3 r3 10 r1 r1

Is this grammar ambiguous? NO But we still have a shift reduce conflict. We need to look ahead and decide when to reduce.

slide-4
SLIDE 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

  • n input, i.e. we have more precise information.

CS453 Shift-Reduce Cont' 4

Closure(I)= repeat for all (Aà α . Xβ , z ) in I for any Xàγ for any w in First(βz) I+= (Xà.γ , w) until I does no change return I Goto(I,X)= J={} for all (Aà α . Xβ , z ) in I J+=(Aà αX . β , z ) return Closure(J) Reduce: R ={} for all (Aà α.) in I R+= (I, z, Aàα) (I, z, Aàα) means: in State I,

  • n symbol z

reduce: Aàα

slide-5
SLIDE 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

slide-6
SLIDE 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

CS453 Shift-Reduce Cont' 6

S’à. S$ ? Sà . V=E $ Sà . E $ Eà . V $ Và . x $, = Và . *E $,= 1 S S’à S . $ ? 2 V Sà V . = E $ Eà V . $ 3 = Sà V = . E $ E à . V $ V à .x $ V à . *E $ 4 In state 3: we reduce Eà àV on $, not on = ( even though = is in follow(E) ) we shift on = conflict resolved

slide-7
SLIDE 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

CS453 Shift-Reduce Cont' 7

S’à. S$ ? Sà . V=E $ Sà . E $ Eà . V $ Và . x $, = Và . *E $,= 1 S S’à S . $ ? 2 V Sà V . = E $ Eà V . $ 3 = Sà V = . E $ E à . V $ V à .x $ V à . *E $ 4 x Và x . $,= 5 x Và x . $ 6 LALR combines states that are equal except for the look aheads

slide-8
SLIDE 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

CS453 Shift-Reduce Cont' 8

S’à. S$ ? Sà . V=E $ Sà . E $ Eà . V $ Và . x $, = Và . *E $,= 1 S S’à S . $ ? 2 V Sà V . = E $ Eà V . $ 3 = Sà V = . E $ E à . V $ V à .x $ V à . *E $ 4 x Và x . $,= 5 x

slide-9
SLIDE 9

CS453 Shift-Reduce Cont' 9

Shift reduce: ambiguous expression grammar (incomplete)

0: S‘à E$ 1: Eà E+E 2: Eà E*E 3: Eà (E) 4: Eà id

S’à. E$ Eà . E+E Eà . E*E … 1 E S’àE.$ EàE . +E EàE . *E 2 + * EàE+.E Eà.E+E Eà.E*E 3 E EàE +E. EàE.+E EàE.*E 4 EàE+.E Eà.E+E Eà.E*E 5 E EàE *E. EàE.+E EàE.*E 6 State 4: two Shift/Reduce conflicts input +: R1 Why? input *: S Why? State 6: two Shift/Reduce conflicts input +: R2 Why? input *: R2 Why? NOTICE: + and * are left associative

slide-10
SLIDE 10

CS453 Shift-Reduce Cont' 10

Shift reduce: ambiguous expression grammar (incomplete)

0: S‘à E$ 1: Eà E^E 2: Eàid // ^ for exponent

S’à. E$ Eà . E^E … 1 E S’àE.$ EàE . ^E 2 ^ EàE^ .E Eà.E^E 3 E EàE ^ E. EàE.^E 4 State 4: two Shift/Reduce conflicts input ^ : S Why?

slide-11
SLIDE 11

CS453 Shift-Reduce Cont' 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

S’à. S$ Sà. i S e S Sà. i S Sà .o 1 i

  • Sà i . S e S

Sà i . S … 4 S S à o. 3 Sà i S . e S Sà i S . 5 State 5: Shift/Reduce conflict input e: S Why? Is else left or right associative?

slide-12
SLIDE 12

CS453 Shift-Reduce Cont' 12

Shift reduce: ambiguous cast grammar

1: EàE+E 2: E à ( byte ) E 3: E à ( E ) 4: E à id

Eà. E+E Eà. ( byte ) E Eà. ( E ) Eà . id … Eà E . + E Eà ( byte ) E . 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

  • perator with higher precedence than the other operators.
slide-13
SLIDE 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