Discussion #4 Ambiguity & Precedence Grammars Discussion #4 1/17 Topics • Ambiguity • Precedence Grammars • Grammar Reductions Discussion #4 2/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 Discussion #4 3/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. Discussion #4 4/17 An Ambiguous Grammar ={ED|(E)|E+E|E–E|E*E|E/E, D0|1|…|9 } E E E E + E * E D E * E E + E D 1 D D D D 3 2 2 * 3 3 1 1 2 + 2 * 3 1 + Discussion #4 5/17 Ambiguity & Multiple Meanings • • • • Precedence: (1+2)*3 1+(2*3) Associativity: (32)1 3(21) Different if-then-else nestings … Discussion #4 6/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. Discussion #4 7/17 Example: Fixing Precedence Ambiguity ET|E+T|E–T ={ED|(E)|E+E| E–E|E*E|E/E, D0|1|…|9 } TF|T*F|T/F FD|(E) D0|1|…|9 E E E E E + E * E E D E * E E + E D T 1 D 2 D 3 D 1 D 2 T * F 3 F D Discussion #4 T + 1 F D D 3 2 8/17 Fixing Associative Ambiguity • Left recursion yields left associativity. • Right recursion yields right associativity. Discussion #4 9/17 Left Associativity ~ Left Recursion ED|DE 3 21 E 321 E D E E D 3 D E 2 Discussion #4 ED|ED E D D D 1 3 1 2 10/17 Right Associativity ~ Right Recursion ED|DE 2 3 2 ED|ED = 232 E 232 E D E E D 2 D E 3 Discussion #4 E D D D 2 2 2 3 11/17 Adding the Power Operator E T | E+T | ET E T | E+T | ET T F | T*F | T/F T P | T*P | T/P F D | (E) P F | FP F D | (E) Discussion #4 12/17 Grammar Reductions: “Silly” Rules Some grammars have “silly” constructions ={AA|a} AAa AAAAa Always ambiguous; rewrite as = { A a }. Discussion #4 13/17 Grammar Reductions: Unreachable Rules Some grammars have unreachable rules. = {S aABb , A a | aA , B b | bBD , C cD , De} Discussion #4 can’t be reached 14/17 Least Fixed Point Algorithm Finds Unreachable Rules 1. 2. 3. 4. 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 nonterminals in the rhs to R. Loop on #2 until there are no changes to R. Rules whose lhs’s are non-terminals in VN minus the non-terminals in R constitute the set of unreachable rules. = { S aABb , A a | aA , B b | bBD , C cD , De} Discussion #4 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} 15/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 … enC A also because it always produces C A bC beC beeC … benC S also because it always produce A S aABb aAbb abCbb … Discussion #4 16/17 Least Fixed Point Algorithm Finds 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 } Discussion #4 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} 17/17