1 / 17

Discussion #4 Ambiguity & Precedence Grammars

Discussion #4 Ambiguity & Precedence Grammars. Topics. Ambiguity Precedence Grammars Grammar Reductions. 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!

zander
Download Presentation

Discussion #4 Ambiguity & Precedence Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discussion #4Ambiguity &Precedence Grammars

  2. Topics • Ambiguity • Precedence Grammars • Grammar Reductions

  3. 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. 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. E E E E E * E + D E * E E + E D 3 1 D D D D 2 3 1 2 1 + 2 * 3 1 + 2 * 3 An Ambiguous Grammar • = { E  D | ( E ) | E + E| E – E | E * E| E / E , • D  0 | 1 | … | 9 }

  6. Ambiguity & Multiple Meanings • Precedence: (1+2)*3  1+(2*3) • Associativity: (32)1  3(21) • Different if-then-else nestings • …

  7. 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. = { E  D | ( E ) | E + E| E – E | E * E| E / E , D  0 | 1 | … | 9 } Example: Fixing Precedence Ambiguity E  T | E + T | E – T T  F | T * F | T / F F  D | ( E ) D  0 | 1 | … | 9 E E E E E E * E + E T + D E * E E + E D T T * F 3 D D 1 D D D F F 1 2 2 3 D 3 D 1 2

  9. Fixing Associative Ambiguity • Left recursion yields left associativity. • Right recursion yields right associativity.

  10. E  D | E  D Left Associativity ~ Left Recursion E  D | D  E 3  2  1 3  2  1 E E D  E E  D 3 D  E E  D 1 2 D 2 D 1 3

  11. E  D | E  D Right Associativity ~ Right Recursion E  D | D  E 2 2 3 = 2  3  2 2  3  2 E E D  E E  D 2 D  E E  D 2 3 D 3 D 2 2

  12. 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. 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. 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. Least Fixed Point AlgorithmFinds Unreachable Rules • Initialize the set of reachable non-terminals R with the start symbol. • For each round, if R includes the lhs of a production rule, add the non-terminals in the rhs to R. • Loop on #2 until there are no changes to R. • Rules whose lhs’s are non-terminals in VNminusthe non-terminals in R constitute the set of unreachable rules. • = { S  aABb , A a | aA , B  b | bBD , C cD , D  e } 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: VN – {S, A, B, D} = {C}

  16. 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  … enC A also because it always produces C A  bC  beC  beeC  … benC S also because it always produce A S  aABb  aAbb  abCbb  …

  17. Least Fixed Point AlgorithmFinds Unproductive Non-terminals 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.  = { S  aABb , A bC , B  d | dB , C eC } 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}

More Related