Overview Grammars and ambiguity CS164 3:30-5:00 TT 10 vans derivations and parse trees dferent derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 1 2 Recall: derivations and parse trees A derivation is a sequence of productions S d d Derivation xample Grammar () A derivation can be drawn as a parse tree Start symbol is the tree s root For a production X d Y 1 Y n add children Y 1,, Y n to node X String You need parse trees to build ASTs 3 4 Derivation xample (Cont.) Derivation in Detail (1) 5 6 1
Derivation in Detail (2) Derivation in Detail (3) 7 8 Derivation in Detail (4) Derivation in Detail (5) 9 10 Derivation in Detail (6) Notes on Derivations A parse tree has Terminals at the leaves Non-terminals at the erior nodes An in-order traversal of the leaves is the original input The parse tree shows the association of operations, the input string does not 11 12 2
Left-most and Right-most Derivations Right-most Derivation in Detail (1) The example is a leftmost derivation At each step, replace the left-most non-terminal There is an equivalent notion of a right-most derivation 13 14 Right-most Derivation in Detail (2) Right-most Derivation in Detail (3) 15 16 Right-most Derivation in Detail (4) Right-most Derivation in Detail (5) 17 18 3
Right-most Derivation in Detail (6) Derivations and Parse Trees Note that right-most and left-most derivations have the same parse tree The dference is only in the order in which branches are added 19 20 Ambiguity Grammar d ( ) ambiguity Strings 22 Ambiguity. xample Ambiguity. xample This string has two parse trees This string has two parse trees is left-associative has higher precedence than 23 24 4
Ambiguity (Cont.) A grammar is ambiguous it has more than one parse tree for some string quivalently, there is more than one right-most or left-most derivation for some string Ambiguity is bad Leaves meaning of some programs ill-defined Ambiguity is common in programming languages Arithmetic expressions IF-THN-LS Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite the grammar unambiguously d T T T d T ( ) nforces precedence of over nforces left-associativity of and 25 26 Ambiguity. xample Ambiguity: The Dangling lse The has ony one parse tree now T T T Conser the grammar S then S then S else S OTHR This grammar is also ambiguous 27 28 The Dangling lse: xample The Dangling lse: A Fix The expression 1 then 2 then S 3 else S 4 has two parse trees 1 S 4 2 S 3 Typically we want the second form 1 2 S 3 S 4 29 else matches the closest unmatched then We can describe this in the grammar (distinguish between matched and unmatched then ) S MIF / all then are matched / UIF / some then are unmatched / MIF then MIF else MIF OTHR UIF then S then MIF else UIF Describes the same set of strings 30 5
The Dangling lse: xample Revisited The expression 1 then 2 then S 3 else S 4 1 1 S 4 Ambiguity No general techniques for handling ambiguity Impossible to convert automatically an ambiguous grammar to an unambiguous one 2 S 3 S 4 A val parse tree (for a UIF) 2 S 3 Not val because the then expression is not a MIF Used with care, ambiguity can simply the grammar Sometimes allows more natural definitions We need disambiguation mechanisms 31 32 Precedence and Associativity Declarations Associativity Declarations Instead of rewriting the grammar Use the more natural (ambiguous) grammar Along with disambiguating declarations Conser the grammar Ambiguous: two parse trees of LR (bottom-up) parsers allow precedence and associativity declarations to disambiguate grammars xamples 33 Left-associativity declaration: %left 34 Precedence Declarations Conser the grammar And the string Precedence declarations: %left %left 35 6