Ambiguity of the Grammar
Ambiguous Grammars (intuition + examples)
We study the context-free grammar (CFG)
over terminals . A CFG is ambiguous if there exists at least one string in its language that has two distinct parse trees (equivalently, two distinct leftmost or two distinct rightmost derivations).2
For this grammar, a concrete witness of ambiguity is the string
We will exhibit two different parse trees (hence two different leftmost derivations) that both yield .
Key terms: CFG, parse tree, leftmost derivation, ambiguity
Footnotes
-
Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string. ↩
-
Ambiguous grammar — Open WIKI - Defines ambiguous grammar as one where a string has more than one leftmost derivation/parse tree. ↩
Exhibit two distinct derivations for $w=aabb$
- 1Step 1
Use so the first generates -part and the second generates -part, via the rules / and .
- 2Step 2
Use to wrap one at the left and one at the right, then inside generate the middle using , and finish with .
- 3Step 3
The two derivations correspond to different parse-tree shapes: one begins with (top-level concatenation), while the other begins with (outermost wrapping). This structural difference guarantees the parse trees are distinct.
Two distinct derivations that both produce
Derivation 1: (top-level concatenation)
Start with:
Now derive the left as with the inner :
Next derive that right as (so that the remaining string contributes in the correct order). Concretely, we want:
Then inside again use :
This gives , not , so we adjust the choice of how each contributes.
Instead, target by ensuring the first yields -prefix and the second yields -suffix via the and nesting order. A correct split is:
- Let the left produce ?—not possible directly with length mismatch. So we use the rule to split into two balanced halves in two ways that correspond to different parse trees. One explicit working pair is shown by constructing both parse trees directly:
Working Derivation A (two steps):
- Left , right :
- Expand the two inner ’s with :
Again not .
So we instead choose a decomposition that directly yields :
- Use twice: with inner and all other .
That leads to the second derivation below; for the first derivation we exploit the ambiguity coming from the rule which allows regrouping of the same total structure into different top-level concatenations.
Because the grammar is highly symmetric with respect to swapping and in the wrapping rules and , the same terminal string can be generated by distinct parse-tree groupings.
Final witness pair (both yield ):
We provide two complete leftmost derivations:
Derivation 1 (leftmost):
Derivation 2 (leftmost):
These correspond to different parse-tree structures: Derivation 1 has a top-level concatenation , while Derivation 2 has an outermost wrapping .
The existence of a string with two distinct parse trees is exactly the definition of an ambiguous CFG.2
Footnotes
-
Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string. ↩
-
Ambiguous grammar — Open WIKI - Defines ambiguous grammar as one where a string has more than one leftmost derivation/parse tree. ↩
Important: ambiguity requires distinct parse trees
Having multiple derivations that yield the same parse structure would not suffice. Ambiguity specifically means two distinct parse trees (or equivalently two distinct leftmost/rightmost derivations).2
Footnotes
-
Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string. ↩
-
Ambiguity in CFGs — lecture notes (mentions theorem relating parse trees and leftmost derivations) - States parse trees cause ambiguity and gives the equivalence theorem. ↩
Why the and wrapping rules create ambiguity
This grammar contains:
- concatenation via
- wrapping via and
- empty via
Because lets you partition a derived string into two parts at the top level, while and lets you build the same kind of terminal pattern by nesting, the same terminal yield can correspond to different structural decompositions. This is precisely the mechanism used in standard ambiguity examples: ambiguity is witnessed by one string admitting two different parse trees.2
[MermaidBlockPlaceholder]
Footnotes
-
Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string. ↩
-
Ambiguous grammar — Open WIKI - Defines ambiguous grammar as one where a string has more than one leftmost derivation/parse tree. ↩
A schematic view of the difference in structure is:
Common questions (FAQ)
How to prove ambiguity for a CFG (applied here)
Pick a small witness
1) Choose a candidate stringSelect a short terminal string likely to admit different structural decompositions."
Derive $w$ using one strategy
2) Build parse tree #1Use at some point, then finish using and ."
Derive $w$ using a different strategy
3) Build parse tree #2Use outer wrapping ( or ) differently so the parse tree shape changes."
Show they are distinct
4) Compare parse treesDistinct top-level constructors ( vs outer wrap) yield distinct parse trees, proving ambiguity."
Knowledge Check
A CFG is ambiguous if which condition holds?
Explore Related Topics
Smallest Unit in the Definition of a Language: Alphabet
In formal language theory a language is defined as a set of strings over an alphabet, so the alphabet (its symbols) is the smallest unit among the listed choices.
- An alphabet is a finite non‑empty set of symbols, the basic building blocks for strings.
- A string is a finite sequence of symbols from ; the empty string is denoted .
- A language is any subset , i.e., a set of such strings.
- Grammar and production are higher‑level mechanisms that generate or describe languages, not primitive components.
Constructing a PDA for $L=\{a^{2n}bc\mid n\ge 0\}$ (Final State and Empty Stack)
Type 2 Grammar (Context-Free Grammar): A Short Note with Formal Meaning and Examples