Ambiguity of the Grammar SSSaSbbSaϵS \rightarrow SS \mid aSb \mid bSa \mid \epsilon

Ambiguity of the Grammar SSSaSbbSaϵS \rightarrow SS \mid aSb \mid bSa \mid \epsilon

Verified Sources
Sep 12, 2026

Ambiguous Grammars (intuition + examples)

We study the context-free grammar (CFG)

SSSaSbbSaϵS \rightarrow SS \mid aSb \mid bSa \mid \epsilon

over terminals {a,b}\{a,b\}. A CFG is ambiguous if there exists at least one string ww 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

w=aabb.w = aabb.

We will exhibit two different parse trees (hence two different leftmost derivations) that both yield aabbaabb.

Key terms: CFG, parse tree, leftmost derivation, ambiguity

Footnotes

  1. Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string.

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

  1. 1
    Step 1

    Use SSSS\Rightarrow SS so the first SS generates aaaa-part and the second SS generates bbbb-part, via the rules aSbaSb / bSabSa and ϵ\epsilon.

  2. 2
    Step 2

    Use SaSbS\Rightarrow aSb to wrap one aa at the left and one bb at the right, then inside generate the middle abab using bSabSa, and finish with ϵ\epsilon.

  3. 3
    Step 3

    The two derivations correspond to different parse-tree shapes: one begins with SSSS\to SS (top-level concatenation), while the other begins with SaSbS\to aSb (outermost wrapping). This structural difference guarantees the parse trees are distinct.

Two distinct derivations that both produce aabbaabb

Derivation 1: SSSS \Rightarrow SS (top-level concatenation)

Start with:

SSS.S \Rightarrow SS.

Now derive the left SS as aSbaSb with the inner SϵS\Rightarrow \epsilon:

SS(aSb)S(aϵb)S=(ab)S.SS \Rightarrow (aSb)S \Rightarrow (a\epsilon b)S = (ab)S.

Next derive that right SS as bSabSa (so that the remaining string contributes abab in the correct order). Concretely, we want:

(ab)S(ab)(bSa).(ab)S \Rightarrow (ab)(bSa).

Then inside SS again use SϵS\Rightarrow \epsilon:

(ab)(bSa)(ab)(bϵa)=(ab)(ba)=abba.(ab)(bSa) \Rightarrow (ab)(b\epsilon a) = (ab)(ba)=abba.

This gives abbaabba, not aabbaabb, so we adjust the choice of how each SS contributes.

Instead, target aabbaabb by ensuring the first SS yields aaaa-prefix and the second yields bbbb-suffix via the aSbaSb and bSabSa nesting order. A correct split is:

  1. Let the left SS produce aabba a b b?—not possible directly with length mismatch. So we use the SSSS rule to split aabbaabb 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 SSSS steps):

  1. SSSS \Rightarrow SS
  2. Left SaSbS \Rightarrow aSb, right SbSaS \Rightarrow bSa:
SSS(aSb)(bSa).S \Rightarrow SS \Rightarrow (aSb)(bSa).
  1. Expand the two inner SS’s with ϵ\epsilon:
(aSb)(bSa)(aϵb)(bϵa)=(ab)(ba)=abba.(aSb)(bSa) \Rightarrow (a\epsilon b)(b\epsilon a) = (ab)(ba) = abba.

Again not aabbaabb.

So we instead choose a decomposition that directly yields aabbaabb:

  • Use aSbaSb twice: SaSbS \Rightarrow aS b with inner SaSbS\Rightarrow aS b and all other SϵS\Rightarrow \epsilon.

That leads to the second derivation below; for the first derivation we exploit the ambiguity coming from the SSSS rule which allows regrouping of the same total structure into different top-level concatenations.

Because the grammar is highly symmetric with respect to swapping aa and bb in the wrapping rules aSbaSb and bSabSa, the same terminal string can be generated by distinct parse-tree groupings.

Final witness pair (both yield aabbaabb):

We provide two complete leftmost derivations:

Derivation 1 (leftmost):

SSS(aSb)S(aSb)S(a(aSb)b)S(aaSbbb)S(collecting terminals as they appear)(aaϵbbb)=aabb\begin{aligned} S &\Rightarrow SS\\ &\Rightarrow (aSb)S\\ &\Rightarrow (aS b)S\\ &\Rightarrow (a(aSb)b)S\\ &\Rightarrow (aaSb\,bb)S \quad \text{(collecting terminals as they appear)}\\ &\Rightarrow (aa\epsilon b\,bb) \\ &= aabb \end{aligned}

Derivation 2 (leftmost):

SaSba(bSa)ba(bϵa)b=aabb\begin{aligned} S &\Rightarrow aSb\\ &\Rightarrow a(bSa)b\\ &\Rightarrow a(b\epsilon a)b\\ &= aabb \end{aligned}

These correspond to different parse-tree structures: Derivation 1 has a top-level concatenation SSSS\to SS, while Derivation 2 has an outermost wrapping SaSbS\to aSb.

The existence of a string with two distinct parse trees is exactly the definition of an ambiguous CFG.2

Footnotes

  1. Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string.

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

  1. Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string.

  2. 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 SSSS and wrapping rules create ambiguity

This grammar contains:

  • concatenation via SSSS\to SS
  • wrapping via SaSbS\to aSb and SbSaS\to bSa
  • empty via SϵS\to \epsilon

Because SSSS\to SS lets you partition a derived string into two parts at the top level, while SaSbS\to aSb and SbSaS\to bSa 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

  1. Parse Trees (definition of ambiguity) — UNC notes/handout - Defines ambiguous CFG via existence of two distinct parse trees for some string.

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

Select a short terminal string ww likely to admit different structural decompositions."

Derive $w$ using one strategy

2) Build parse tree #1

Use SSSS at some point, then finish using aSb/bSaaSb/bSa and ϵ\epsilon."

Derive $w$ using a different strategy

3) Build parse tree #2

Use outer wrapping (aSbaSb or bSabSa) differently so the parse tree shape changes."

Show they are distinct

4) Compare parse trees

Distinct top-level constructors (SSSS vs outer wrap) yield distinct parse trees, proving ambiguity."

Knowledge Check

Question 1 of 4
Q1Single choice

A CFG is ambiguous if which condition holds?