1 / 7

Context Free Grammar Ambiguity

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 ∑)*.

aurora
Télécharger la présentation

Context Free Grammar Ambiguity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Context Free Grammar Ambiguity Brandon McKune April 21, 2012

  2. 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

  3. 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.

  4. 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.

  5. 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 Ʌ

  6. 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

  7. 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.

More Related