Ambiguity of the Grammar on String aab

Ambiguity of the Grammar on String aab

Verified Sources
Nov 4, 2026

We study the context-free grammar (CFG)

S→aS∣aSbS∣εS \rightarrow aS \mid aSbS \mid \varepsilon

To prove a CFG is ambiguous on a particular string ww, we exhibit two distinct parse trees (equivalently, two distinct leftmost or rightmost derivations) that generate the same string ww. In this section we prove that the grammar is ambiguous for the specific string w=aabw = \texttt{aab}.

Key idea: the nonterminal SS can either

  • “wrap” itself with a leading aa (via S⇒aSS \Rightarrow aS), or
  • split into two SS’s separated by a bb (via S⇒aSbSS \Rightarrow aSbS).

Because these options can be combined in more than one structural way to produce aab\texttt{aab}, we can construct two different derivation structures for the same terminal string.

We will explicitly construct:

  1. two different leftmost derivations of aab\texttt{aab}, and
  2. 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 aab\texttt{aab}. Since ambiguity can be witnessed by parse trees, it suffices to show two different parse trees for aab\texttt{aab}.

Construct two different leftmost derivations for S ⇒* aab

  1. 1
    Step 1

    Start with SS. Use S⇒aSbSS \Rightarrow aSbS. Then choose left S⇒aSS \Rightarrow aS and then S⇒εS \Rightarrow \varepsilon to make the left side generate the single terminal aa. Choose the rightmost S⇒εS \Rightarrow \varepsilon so it contributes nothing after the bb.

  2. 2
    Step 2

    One leftmost derivation is:

    S⇒aSbS⇒a(aS)bS(expand the leftmost S using S⇒aS)⇒aabS(expand that leftmost S⇒ε)⇒aab(expand the remaining S⇒ε)\begin{aligned} S &\Rightarrow aSbS\\ &\Rightarrow a(aS)bS \quad (\text{expand the leftmost } S\text{ using } S\Rightarrow aS)\\ &\Rightarrow aa bS \quad (\text{expand that leftmost } S\Rightarrow \varepsilon)\\ &\Rightarrow aab \quad (\text{expand the remaining } S\Rightarrow \varepsilon) \end{aligned}

  3. 3
    Step 3

    Alternatively, begin with S⇒aSS \Rightarrow aS to produce the first aa. Then expand that inner SS using S⇒aSbSS \Rightarrow aSbS, with the left inner S⇒εS \Rightarrow \varepsilon and the right inner S⇒εS \Rightarrow \varepsilon so the remaining terminals become exactly abab after the first aa.

  4. 4
    Step 4

    A different leftmost derivation is:

    S⇒aS⇒aaSbS(expand the leftmost S using S⇒aSbS)⇒aabS(expand leftmost S⇒ε)⇒aab(expand remaining S⇒ε)\begin{aligned} S &\Rightarrow aS\\ &\Rightarrow aaSbS \quad (\text{expand the leftmost } S\text{ using } S\Rightarrow aSbS)\\ &\Rightarrow aa bS \quad (\text{expand leftmost } S\Rightarrow \varepsilon)\\ &\Rightarrow aab \quad (\text{expand remaining } S\Rightarrow \varepsilon) \end{aligned}

Why these derivations are different (structural witness)

Both derivations generate the same terminal string aab\texttt{aab}, but they do so with different top-level rule usage positions, which yields different parse trees.

  • Derivation 1 uses the rule S⇒aSbSS \Rightarrow aSbS at the root, immediately creating the bb in the middle.
  • Derivation 2 uses the rule S⇒aSS \Rightarrow aS at the root, and only later uses S⇒aSbSS \Rightarrow aSbS inside the derivation.

This difference changes the parse tree structure, even though the final yield of terminals is the same.

Parse trees for w=aabw=\texttt{aab}

Parse tree A (corresponds to Derivation 1)

Using the root production S⇒aSbSS \Rightarrow aSbS:

  • Root: SS
  • Root expands to: a  S  b  Sa\; S\; b\; S
  • Left S⇒aS⇒aεS \Rightarrow aS \Rightarrow a\varepsilon
  • Right S⇒εS \Rightarrow \varepsilon

So the frontier (leaf terminals) is a  a  ba\;a\;b.

Parse tree B (corresponds to Derivation 2)

Using S⇒aSS \Rightarrow aS at the root, then S⇒aSbSS \Rightarrow aSbS inside:

  • Root: S⇒aSS \Rightarrow aS
  • Inner S⇒aSbSS \Rightarrow aSbS
  • Left inner S⇒εS \Rightarrow \varepsilon
  • Right inner S⇒εS \Rightarrow \varepsilon

Frontier terminals again become a  a  ba\;a\;b.

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 choice

Root creates the middle bb structure right away, then the two SS’s generate the aa and the empty tail."

Option 2: Use aS first

Root choice

Root delays creating the bb by adding an initial aa, then uses aSbS one level down to place the bb."

Both strategies generate aab

Yield check

After appropriate substitutions of S⇒εS \Rightarrow \varepsilon on the remaining nonterminals, both parse trees have the same frontier aab."

Knowledge Check

Question 1 of 3
Q1Single choice

Which pair of productions can be combined to generate at least one terminal 'b' in this grammar?