Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion #41/17 Discussion #4 Ambiguity & Precedence Grammars.

Similar presentations


Presentation on theme: "Discussion #41/17 Discussion #4 Ambiguity & Precedence Grammars."— Presentation transcript:

1 Discussion #41/17 Discussion #4 Ambiguity & Precedence Grammars

2 Discussion #42/17 Topics Ambiguity Precedence Grammars Grammar Reductions

3 Discussion #43/17 Problems with Grammars We are heading towards grammars that are reasonable, get the job done, and for which we can find efficient parsing algorithms. Not all grammars are usable! –Ambiguous –“Silly” –Have unproductive non-terminals –Have unreachable rules

4 Discussion #44/17 Ambiguous Grammars A grammar for which there are two different parse trees for the same terminal string is said to be ambiguous. We can show that a grammar is ambiguous by giving two parse trees for the same terminal string.

5 Discussion #45/17  = { E  D | ( E ) | E + E | E – E | E * E | E / E, D  0 | 1 | … | 9 } E EE + EE*D DD1 23 1 + 2 * 3 E EE* EE+D DD 12 3 An Ambiguous Grammar

6 Discussion #46/17 Ambiguity & Multiple Meanings Precedence: (1+2)*3  1+(2*3) Associativity: (3  2)  1  3  (2  1) Different if-then-else nestings …

7 Discussion #47/17 Fixing Precedence Ambiguity Observe: Operators lower in the parse tree are executed first in an expression tree. Observe: Operators executed first have higher precedence. Fix: We make our grammar correctly represent precedence levels by introducing a new non-terminal symbol for each precedence level.

8 Discussion #48/17 Example: Fixing Precedence Ambiguity  = { E  D | ( E ) | E + E | E – E | E * E | E / E, D  0 | 1 | … | 9 } E  T | E + T | E – T T  F | T * F | T / F F  D | ( E ) D  0 | 1 | … | 9 E EE + EE*D DD1 23 E EE* EE+D DD 12 3 E ET + TF*T F D 12 3 F D D

9 Discussion #49/17 Fixing Associative Ambiguity Left recursion yields left associativity. Right recursion yields right associativity.

10 Discussion #410/17 Left Associativity ~ Left Recursion E  D | D  EE  D | E  D 3  2  1 E DE 3DE 2 1   E ED ED D 3 2   1 D

11 Discussion #411/17 Right Associativity ~ Right Recursion E  D | D  EE  D | E  D 2  3  2 E DE 2DE 3 2   E ED ED D 2 3   2 D 2 3 2 =

12 Discussion #412/17 Adding the Power Operator E  T | E+T | E  T T  F | T*F | T/F F  D | (E) E  T | E+T | E  T T  P | T*P | T/P P  F | F  P F  D | (E)

13 Discussion #413/17 Grammar Reductions: “Silly” Rules Some grammars have “silly” constructions  = { A  A | a } A  A  a A  A  A  A  a Always ambiguous; rewrite as  = { A  a }.

14 Discussion #414/17 Grammar Reductions: Unreachable Rules Some grammars have unreachable rules.  = {S  aABb, A  a | aA, B  b | bBD, C  cD, D  e } can’t be reached

15 Discussion #415/17 Least Fixed Point Algorithm Finds Unreachable Rules  = { S  aABb, A  a | aA, B  b | bBD, C  cD, D  e } 1.Initialize the set of reachable non-terminals R with the start symbol. 2.For each round, if R includes the lhs of a production rule, add the non- terminals in the rhs to R. 3.Loop on #2 until there are no changes to R. 4.Rules whose lhs’s are non-terminals in V N minus the non-terminals in R constitute the set of unreachable rules. Initialize: R = {S} Round 1: R = {S, A, B} Round 2: R = {S, A, B, D} Round 3: R = {S, A, B, D} Done: no change: V N – {S, A, B, D} = {C}

16 Discussion #416/17 Grammar Reductions: Unproductive Non-terminals Some grammars have non-terminals that can never become all terminals (unproductive, inactive).  = { S  aABb, A  bC, B  d | dB, C  eC } C never produces all terminals. C  eC  eeC  eeeC  … e n C A also because it always produces C A  bC  beC  beeC  … be n C S also because it always produce A S  aABb  aAbb  abCbb  …

17 Discussion #417/17 Least Fixed Point Algorithm Finds Unproductive Non-terminals  = { S  aABb, A  bC, B  d | dB, C  eC } 1. Start with the set of terminals T. 2. For each round, if T covers a rhs of a production rule, add the lhs to T. 3. Loop on #2 until there are no changes to T. 4. The alphabet of terminals and non-terminals, V, minus T is the set of unproductive non-terminals. Initialize: T = {a, b, d, e} Round 1: T = {a, b, d, e, B} Round 2: T = {a, b, d, e, B} Done: no change: {a, b, d, e, A, B, C, S} – T = {A, C, S}


Download ppt "Discussion #41/17 Discussion #4 Ambiguity & Precedence Grammars."

Similar presentations


Ads by Google