Parsing: Derivations, Ambiguity, Precedence, Associativity Lecture 8 (Modified by Professor Vijay Ganesh) 1
Topics covered so far Regular languages and Finite automaton Parser overview Context-free grammars (CFG s) Derivations First, follow, predict sets 2
Outline of Today s Lecture Ambiguity in context-free grammars Left and right derivations Associativity Precedence Bottom-up Parsing 3
CFGs (Cont.) A CFG consists of A set of terminals T A set of non-terminals N A start symbol S (a non-terminal) A set of productions X YY L Y 1 2 n where { } X N and Y T N ε i 4
xamples of CFGs (cont.) Simple arithmetic expressions: + ( ) 5
Arithmetic xample Simple arithmetic expressions: + () Some elements of the language: + () () () 6
Derivation xample Grammar + () String + 7
Derivation xample (Cont.) + + + + * + + 8
Derivation in Detail (1) 9
Derivation in Detail (2) + + 10
Derivation in Detail (3) + + + * 11
Derivation in Detail (4) + + + * + 12
Derivation in Detail (5) + + + + * + 13
Derivation in Detail (6) + + + + * + + 14
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 15
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 + + + + + 16
Ambiguity Grammar + () String + 17
Ambiguity (Cont.) This string has two parse trees + * * + 18
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 19
Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite grammar unambiguously + ' ' " () " () nforces precedence of * over + ' 20
Ambiguity in Arithmetic xpressions Recall the grammar + * ( ) The string * + has two parse trees: + * * + 21
Ambiguity: The Dangling lse Conser the grammar if then if then else OTHR This grammar is also ambiguous 22
The Dangling lse: xample The expression if 1 then if 2 then 3 else 4 has two parse trees if if 1 if 4 1 if 2 3 2 3 4 Typically we want the second form 23
The Dangling lse: A Fix else matches the closest unmatched then We can describe this in the grammar MIF /* all then are matched */ UIF /* some then is unmatched */ MIF if then MIF else MIF OTHR UIF if then if then MIF else UIF Describes the same set of strings (Associativity) 24
The Dangling lse: xample Revisited The expression if 1 then if 2 then 3 else 4 if if 1 if 4 1 if 2 3 2 3 4 Not val because the then expression is not a MIF A val parse tree (for a UIF) 25
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 26
Precedence and Associativity Declarations Instead of rewriting the grammar Use the more natural (ambiguous) grammar Along with disambiguating declarations Most grammar generators allow precedence and associativity declarations to disambiguate grammars xamples 27
Associativity Declarations Conser the grammar + Ambiguous: two parse trees of + + + + + + Left associativity declaration: %left + 28
Precedence Declarations Conser the grammar + * And the string + * * + + * Precedence declarations: %left + %left * 29