80 likes | 367 Views
Context Free Grammar Ambiguity. Brandon McKune April 21, 2012. Context Free Grammar. A context-free grammar (CFG) is a 4-tuple G = (V, ∑, S, P) where V and ∑ are disjoint finite sets, S P is a finite set of formulas of the form A → , where A ∈ V and ∈ (V U ∑)*.
E N D
Context Free Grammar Ambiguity Brandon McKune April 21, 2012
Context Free Grammar A context-free grammar (CFG) is a 4-tuple G = (V, ∑, S, P) where V and ∑ are disjoint finite sets, S P is a finite set of formulas of the form A → , where A ∈ V and ∈ (V U ∑)*. • ∑ are terminal symbols, or terminals • V are variables ornonterminals • S is the start variable • P is the grammar rules or productions
Ambiguity A context-free grammar G is ambiguous for at least one x ∈ L(G), x has more than one derivation tree (or, equivalently, more than one leftmost derivation). • Theorem 4.17 contains the proof for why the leftmost derivation is the equivalent to more than one derivation tree.
The Problem Given the CFG G = {V, ∑, S, P} with the following productions: S → ASA | aB A → B | S B → b | Ʌ Show: The string aabb has two leftmost derivations.
Leftmost Derivation #1: aabb Using Production: A→B We start with S Using Production: B→b Using Production: A→B Using Production: B→Ʌ Using Production: A→B Other than the strings aband aɅ, every string derived by G will utilize the product S→ASA Using Production: B→Ʌ Using Production: A→S Using Production: S→ASA Using Production: S→aB Using Production: S→aB Using Production: B→b Using Production: B→Ʌ S A S A S A B A B Ʌ S B B a b b B Ʌ a Ʌ
Leftmost Derivation #2: aabb Using Production: B→b Using Production: A→B Using Production: B→b Using Production: A→B Using Production: B→Ʌ Using Production: S→aB Using Production: B→Ʌ Using Production: A→B Using Production: S→ASA Using Production: B→Ʌ Using Production: S→aB Using Production: A→S As shown in Derivation #1, every string derived from G starts with the production S→ASA except for the strings aband aɅ. We start with S S A S A S A S A B In Derivation # 1 the production we chose was A→B. This is the first leftmost derivation difference. As long as we can derive aabb we have shown this CFG is ambiguous. b B a a B B B Ʌ Ʌ Ʌ b
Conclusion In Derivation #1 and #2 we have two leftmost derivation trees showing how to derive the string aabb. The CFG G is ambiguous due to a single string having two different leftmost derivations as shown in Definition 4.18 of our textbook.