Ambiguity in Grammar and a Proof of Ambiguity for a Given CFG
Ambiguity in grammar (especially in context-free grammars) means that one string in the language can be generated in more than one distinct way—typically formalized as producing two different parse trees (or equivalently, two different leftmost/rightmost derivations).
For a context-free grammar (CFG), a string is ambiguous with respect to the grammar if there exist at least two different parse trees whose yields (leaves, read left-to-right) form .
Key terms:
- Ambiguous grammar
- Parse tree
- Leftmost derivation
- Rightmost derivation
To show a grammar is ambiguous, a standard method is:
- Find a string derivable by the grammar.
- Exhibit two distinct parse trees for (or two distinct leftmost derivations).
- Conclude the grammar is ambiguous because ambiguity requires only one witness string.
Footnotes
-
Wikipedia — Ambiguity in context-free grammars (general definition). https://en.wikipedia.org/wiki/Context-free_grammar#Ambiguity - Explains ambiguity as multiple parse trees/derivations for the same string. ↩
-
Wikipedia — Parse tree and derivations concepts. https://en.wikipedia.org/wiki/Parse_tree - Background on parse trees (tree structure reflecting derivations). ↩ ↩2
Ambiguous vs Unambiguous Context-Free Grammars (Parse Trees & Derivations)
We are given the CFG:
What strings does it generate?
From , each generates a sequence of zero or more ’s; similarly, generates zero or more ’s.
Therefore, any derivation from has the form:
for some .
So the language is:
(We will not need the full characterization to prove ambiguity—only one witness string.)
Key idea
Here, ambiguity will come from the fact that both occurrences of generate -strings, so the same overall -block can be partitioned between the left and the right in more than one way.
Footnotes
-
Wikipedia — Context-free grammar productions and language generation (rules like yield ). https://en.wikipedia.org/wiki/Regular_grammar - Covers how recursive productions with generate repetition languages; applicable to the behavior of and here. ↩
Construct two different parse trees for the same string
- 1Step 1
Pick . It has the form , so it is plausibly in the language.
- 2Step 2
Let the left produce and the right produce : use twice, and once.
- 3Step 3
Now change the partition of ’s: let the left produce and the right produce while adjusting via the structure so the overall yield remains by using productions differently.
- 4Step 4
Check that both trees yield as the concatenation of terminals, but their internal expansions of across the two positions differ, giving distinct parse trees.
A correct ambiguity proof: the grammar is ambiguous
We will show that there exists a string such that two distinct parse trees exist.
However, for this particular grammar, observe the structure of :
- The middle portion must be produced entirely by (only ’s).
- The left and right portions around are produced by two separate ’s (only ’s).
That implies any derived string has the form:
Now consider , i.e. .
In order to obtain exactly one before the , the left must generate exactly one , i.e. .
Similarly, to obtain exactly one after the , the right must also generate exactly one , i.e. .
The middle forces .
So for , the parse tree structure is essentially forced:
- Left must be
- Middle must be
- Right must be
That suggests no ambiguity for that string.
The subtlety
To prove ambiguity, we need a string for which there are two different ways to assign productions to nonterminals that still yield the same terminal string. Here, because:
- generates only ’s, and the grammar has exactly one in the skeleton ,
- and both ’s generate only ’s with no way for ’s to “move” across the ,
the partition point between the -region and the -region is determined by the position of the ’s in the final string. Likewise, the partition of the prefix and suffix ’s is tied to the fact that both occurrences correspond to the two sides of the single .
Result: this CFG is unambiguous for all strings it generates because the decomposition matches a unique choice of , and for each (resp. ) there is only one derivation shape for (it must apply the rule exactly times then once).
[Warning]{type="warning"}
If you were expecting a straightforward “two parse trees for the same string” example, note that this grammar’s structure can make such a witness difficult or impossible—so ambiguity must be checked carefully with explicit derivations. (A common mistake is to assume “multiple ’s implies ambiguity,” but here the terminals are segregated: produces only and produces only .)
Formal justification via uniqueness of derivation counts
For :
- Producing requires applying exactly times, then .
- That yields a unique parse tree shape for that (a chain of length ending in ).
For similarly:
Thus, for any specific string , the parse tree is uniquely determined by and by the forced number of rule applications inside each and .
Therefore, the claim “the given grammar is ambiguous” is not supported: the grammar is unambiguous.
Footnotes
-
Wikipedia — Context-free grammar productions and language generation (rules like yield ). https://en.wikipedia.org/wiki/Regular_grammar - Covers how recursive productions with generate repetition languages; applicable to the behavior of and here. ↩ ↩2 ↩3
-
Wikipedia — Parse tree and derivations concepts. https://en.wikipedia.org/wiki/Parse_tree - Background on parse trees (tree structure reflecting derivations). ↩
FAQs and common confusion points
Rule-choice determinism for a fixed target string a^i b^j a^k
For each fixed , the number of times rules must be applied is forced.
How to prove ambiguity (or discover it’s not ambiguous)
Start with a witness candidate
1) Pick a candidate stringChoose a terminal string and attempt to derive it in multiple ways."
Construct two parse trees / derivations
2) Attempt two distinct derivationsWrite leftmost derivations or explicit parse trees and check distinctness."
Same string, different structure
3) Validate yields matchConfirm both derivations yield identical terminals."
Unambiguity may follow from forced choices
4) If impossible, reassessIf terminal segregation forces unique rule counts (like here), ambiguity may not exist."
Ambiguity Quick Check
Knowledge Check
A CFG is ambiguous if there exists: