Grammars and ambiguity CS164 3:30-5:00 TT 10 vans 1
Overview derivations and parse trees different derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 2
Recall: derivations and parse trees A derivation is a sequence of productions S A derivation can be drawn as a parse tree Start symbol is the tree s root For a production X Y 1 Y n add children Y 1,, Y n to node X You need parse trees to build ASTs 3
Derivation xample Grammar () id String id id id 4
Derivation xample (Cont.) id * id id id id id id id id 5
Derivation in Detail (1) 6
Derivation in Detail (2) 7
Derivation in Detail (3) * 8
Derivation in Detail (4) * id id 9
Derivation in Detail (5) id * id id id id 10
Derivation in Detail (6) id * id id id id id id id id 11
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 12
Left-most and Right-most Derivations 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 id id id id id id id 13
Right-most Derivation in Detail (1) 14
Right-most Derivation in Detail (2) 15
Right-most Derivation in Detail (3) id id 16
Right-most Derivation in Detail (4) id * id id 17
Right-most Derivation in Detail (5) id id * id id id id 18
Right-most Derivation in Detail (6) id id * id id id id id id id id 19
Derivations and Parse Trees Note that right-most and left-most derivations have the same parse tree The difference is only in the order in which branches are added 20
ambiguity
Ambiguity Grammar * ( ) Strings * 22
Ambiguity. xample This string has two parse trees is left-associative 23
Ambiguity. xample This string has two parse trees * * * has higher precedence than 24
Ambiguity (Cont.) A grammar is ambiguous if 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 25
Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite the grammar unambiguously T T T T * ( ) nforces precedence of * over nforces left-associativity of and * 26
Ambiguity. xample The * has ony one parse tree now T * T T * 27
Ambiguity: The Dangling lse Consider the grammar S if then S if then S else S OTHR This grammar is also ambiguous 28
The Dangling lse: xample The expression if 1 then if 2 then S 3 else S 4 has two parse trees if if 1 if S 4 1 if 2 S 3 2 S 3 S 4 Typically we want the second form 29
The Dangling lse: A Fix 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 if then MIF else MIF OTHR UIF if then S if then MIF else UIF Describes the same set of strings 30
The Dangling lse: xample Revisited The expression if 1 then if 2 then S 3 else S 4 if if 1 if 1 if S 4 2 S 3 S 4 2 S 3 A valid parse tree (for a UIF) Not valid because the then expression is not a MIF 31
Ambiguity No general techniques for handling ambiguity Impossible to convert automatically an ambiguous grammar to an unambiguous one Used with care, ambiguity can simplify the grammar Sometimes allows more natural definitions We need disambiguation mechanisms 32
Precedence and Associativity Declarations Instead of rewriting the grammar Use the more natural (ambiguous) grammar Along with disambiguating declarations LR (bottom-up) parsers allow precedence and associativity declarations to disambiguate grammars xamples 33
Associativity Declarations Consider the grammar Ambiguous: two parse trees of Left-associativity declaration: %left 34
Precedence Declarations Consider the grammar * And the string * * * Precedence declarations: %left %left * 35