Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

Similar presentations


Presentation on theme: "COP4620 – Programming Language Translators Dr. Manuel E. Bermudez"— Presentation transcript:

1 COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
SLR(1) parsing COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

2 topics Conflicts in LR(0) Automata Using Follow sets for lookahead
SLR(1) examples

3 LR Parsing Operation of an LR parser Two moves: shift and reduce.
Shift: Advance from current state on Next_Token, push new state on stack. Reduce: (on A → ω). Pop |ω| states from stack. Advance from new top state on A. So far, we haven’t seen both apply in the same state.

4 SLR(1) parsing Grammar: S → E  T → P * T E → E + T → P → T P →(E) → i
2 S → . E  E → .E + T E → .T T → .P*T T → .P P →.i P →.( E ) P → (.E) E → .E + T E → .T T → .P*T T → .P P →.i P →.( E ) 10 1 6 E NOT LR(0)! T 3 T 3 T T → P * .T T → .P*T T → .P P →.i P →.( E ) 12 P 4 P 4 9 P 4 i i 5 5 i 5 ( ( 6 6 ( 6 S → E . 7 7 S → E .  E → E. + T 2 + ) T P → (E.) E → E. + T 8 11 13 E → E + .T T → .P*T T → .P P →.i P →.( E ) 10 8 P + 8 4 3 E → T. 11 E → E + T. i * 5 4 T →P.*T T →P. 9 ( 12 T → P * T. 6 5 P → i. P → (E). Shift/reduce conflict. 13

5 SLR(1) parsing Conflicts appear in the ACTION table, as multiple actions. State + * i ( ) 1 S/5 S/6 2 S/8 S/7 3 R/E→T 4 R/T→P S/9 5 R/P→i 6 7 Accept 8 9 10 S/13 11 R/E→E+T 12 R/T→P*T 13 R/P→(E)

6 SLR(1) parsing Conflict Solution: Use lookahead!
Grammar: S → E  E → E + T → T T → P * T → P P →(E) → i SLR(1) parsing Conflict Solution: Use lookahead! SLR(1): For each “conflict” state s, Compute Follow(A) for each conflict production A → ω. Place “R/A → ω” in ACTION[s,t], only if tFollow(A). Need Follow(T)  Follow(E) = {+, ),  }. So, change row 4 to State + * i ( ) 4 R/T→P S/9 State + * i ( ) 4 R/T→P S/9 Shift/reduce conflict solved! Grammar is SLR(1).

7 SLR(1) parsing ┴ Grammar: S’ → S  S → aSb → 1 2 4 3 5 6 2 S’ → .S 
ACTION GOTO State a b S 1 R/S→ S/3 2 S/4 3 5 4 Accept 6 R/S→aSb S 5 3 S → a.Sb S → .aSb S → . a 3 4 S’ → S  . b 5 6 S →aS.b 6 S →aSb. Two shift/reduce conflicts: need Follow(S)

8 SLR(1) parsing Grammar: S’ → S  S → aSb → SLR(1) Analysis:
State 1: Follow(S) = {b, }. Place ’S →’ only in ACTION[1,b] and ACTION[1, ]. Since a ∉ Follow(S), the shift/reduce conflict is resolved. State 3: Same story. Table (rows 1,3) changes ACTION State a b 1 R/S→ S/3 3 ACTION State a b 1 S/3 R/S→ 3 from to Conflicts solved. Grammar is SLR(1).

9 summary Conflicts in LR(0) Automata Using Follow sets for lookahead
SLR(1) examples


Download ppt "COP4620 – Programming Language Translators Dr. Manuel E. Bermudez"

Similar presentations


Ads by Google