CS 342 Lecture -*- Outline -*- * Ambiguity ** Motivation: Expression grammars ------------ ::= | + | * | ( ) ::= a | b | c ------------- What does "a+b*c" mean? a+(b*c) or (a+b)*c? *** How parse trees are built | | ________|_________ _______|_______ | | | | | | | | + _____|______ ______|____ * | | | | | | | | | | | | | * + | a b c a b c *** precedence: higher precedence operators bind more tightly * has higher precedence than + ** Ambiguous grammar a grammar is ambiguous iff it has a sentence with two (different) parse trees example: above expression grammar ** Parse trees (formal def) T is a parse tree for G iff 1. Every node of T is labeled with a (terminal or nonterminal) symbol of G 2. The root of T is labeled with the start symbol of G 2. Every interior node is labeled with a nonterminal of G 4. If A | ---------------- B C D E F is a subtree of T, then A ::= B C D E F is a production of G. 4. Some require that the fringe of parse tree be terminal symbols. Graphical abstraction of derivation ** Derivations: Consider the expression grammar ::= | + | * ::= a + b There are many ways to generate (derive) a + b: => + => + => a+ => a+ => a+b => + => +b => +b => a+b Order doesn't really matter for context free grammars choose standard order *** Leftmost: replace leftmost nonterminal at each step (top line above) *** Rightmost: replace rightmost nonterminal at each step ** Common causes of ambiguity *** operator precedence *** associativity (e.g, ::= - ) *** example left-associative, expression grammar with + lower precedence ::= + | ::= * | ::= switching precedence, associtivity? is it unambiguous? ** Techniques for proving that a grammar is unambiguous restrict grammar to some class which is known to have only unambiguous grammars e.g., if L is regular it is unambiguous (proof: use a left-linear grammar) if L1 and L2 are disjoint unambiguous langs, then L1 union L2 is unambiguous If grammar can be deterministically parsed, then it's unambiguous idea: (LR(k)) make sure each prefix of terminals leads to a "state" of a parser (pda) in which the next step (reduce, shift) is determined, by looking ahead k tokens. (LR(k) grammars can be parsed with k symbols of lookahead) ** Undecideability of ambiguity Undecideable whether an arbitrary context-free grammar is ambiguous. (no algorithm that always terminates with decision) But can check for membership in smaller classes ** Quiz ------------------ Quiz (on grammars and ambiguity): Consider the following grammar ::= | ( ) | * | () | [] ::= b | c 1. Draw a parse tree for the string "*(c[])". (omitted) 2. Is the grammar ambiguous? (yes) 3. Can more than one identifier appear in a sentence? (no) 4. write an umambiguous grammar for the same language so that () and [] have higher precedence than * -------------------- answers 1. (omitted) 2. yes 3. no 4. below ::= * | ::= | ( ) | () | []