Ambiguity of the Grammar on String aab
We study the context-free grammar (CFG)
To prove a CFG is ambiguous on a particular string , we exhibit two distinct parse trees (equivalently, two distinct leftmost or rightmost derivations) that generate the same string . In this section we prove that the grammar is ambiguous for the specific string .
Key idea: the nonterminal can either
- “wrap” itself with a leading (via ), or
- split into two ’s separated by a (via ).
Because these options can be combined in more than one structural way to produce , we can construct two different derivation structures for the same terminal string.
We will explicitly construct:
- two different leftmost derivations of , and
- the corresponding two different parse trees.
Ambiguity
Parse tree
Leftmost derivation
Rightmost derivation
Ambiguous Grammars (CFG) — Parse Trees and Derivations
Goal
Show that there exist two different derivations that both produce . Since ambiguity can be witnessed by parse trees, it suffices to show two different parse trees for .
Construct two different leftmost derivations for S ⇒* aab
- 1Step 1
Start with . Use . Then choose left and then to make the left side generate the single terminal . Choose the rightmost so it contributes nothing after the .
- 2Step 2
One leftmost derivation is:
- 3Step 3
Alternatively, begin with to produce the first . Then expand that inner using , with the left inner and the right inner so the remaining terminals become exactly after the first .
- 4Step 4
A different leftmost derivation is:
Why these derivations are different (structural witness)
Both derivations generate the same terminal string , but they do so with different top-level rule usage positions, which yields different parse trees.
- Derivation 1 uses the rule at the root, immediately creating the in the middle.
- Derivation 2 uses the rule at the root, and only later uses inside the derivation.
This difference changes the parse tree structure, even though the final yield of terminals is the same.
Parse trees for
Parse tree A (corresponds to Derivation 1)
Using the root production :
- Root:
- Root expands to:
- Left
- Right
So the frontier (leaf terminals) is .
Parse tree B (corresponds to Derivation 2)
Using at the root, then inside:
- Root:
- Inner
- Left inner
- Right inner
Frontier terminals again become .
Parse-tree ambiguity
Frontier
Production
Start symbol
Common pitfalls and how to avoid them
Where ambiguity arises in this grammar
Option 1: Use aSbS immediately
Root choiceRoot creates the middle structure right away, then the two ’s generate the and the empty tail."
Option 2: Use aS first
Root choiceRoot delays creating the by adding an initial , then uses aSbS one level down to place the ."
Both strategies generate aab
Yield checkAfter appropriate substitutions of on the remaining nonterminals, both parse trees have the same frontier aab."
Knowledge Check
Which pair of productions can be combined to generate at least one terminal 'b' in this grammar?