Ambiguity in Grammar and a Proof of Ambiguity for a Given CFG

Ambiguity in Grammar and a Proof of Ambiguity for a Given CFG

Verified Sources
Sep 12, 2026

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 ww is ambiguous with respect to the grammar if there exist at least two different parse trees whose yields (leaves, read left-to-right) form ww.

Key terms:

  • Ambiguous grammar
  • Parse tree
  • Leftmost derivation
  • Rightmost derivation

To show a grammar is ambiguous, a standard method is:

  1. Find a string ww derivable by the grammar.
  2. Exhibit two distinct parse trees for ww (or two distinct leftmost derivations).
  3. Conclude the grammar is ambiguous because ambiguity requires only one witness string.

Footnotes

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

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

SABAS \rightarrow ABA AaAεA \rightarrow aA \mid \varepsilon BbBεB \rightarrow bB \mid \varepsilon

What strings does it generate?

From AaAεA \rightarrow aA \mid \varepsilon, each AA generates a sequence of zero or more aa’s; similarly, BB generates zero or more bb’s.
Therefore, any derivation from SS has the form:

SABAaibjakS \Rightarrow A\,B\,A \Rightarrow a^{i}\, b^{j}\, a^{k}

for some i,j,k0i,j,k \ge 0.
So the language is:

L(G)={aibjaki,j,k0}.L(G)=\{\, a^{i} b^{j} a^{k} \mid i,j,k \ge 0 \,\}.

(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 AA generate aa-strings, so the same overall aa-block can be partitioned between the left AA and the right AA in more than one way.

Footnotes

  1. Wikipedia — Context-free grammar productions and language generation (rules like AaAεA \to aA \mid \varepsilon yield aa^*). https://en.wikipedia.org/wiki/Regular_grammar - Covers how recursive productions with varepsilon\\varepsilon generate repetition languages; applicable to the behavior of AA and BB here.

Construct two different parse trees for the same string

  1. 1
    Step 1

    Pick w=abaw=aba. It has the form a1b1a1a^{1} b^{1} a^{1}, so it is plausibly in the language.

  2. 2
    Step 2

    Let the left AA produce aa and the right AA produce aa: use AaεA \Rightarrow a\varepsilon twice, and BbεB \Rightarrow b\varepsilon once.

  3. 3
    Step 3

    Now change the partition of aa’s: let the left AA produce aaaa and the right AA produce ε\varepsilon while adjusting via the structure SABAS \Rightarrow ABA so the overall yield remains abaaba by using ε\varepsilon productions differently.

  4. 4
    Step 4

    Check that both trees yield abaaba as the concatenation of terminals, but their internal expansions of AA across the two AA positions differ, giving distinct parse trees.

A correct ambiguity proof: the grammar is ambiguous

We will show that there exists a string ww such that two distinct parse trees exist.

However, for this particular grammar, observe the structure of SABAS \rightarrow ABA:

  • The middle portion must be produced entirely by BB (only bb’s).
  • The left and right portions around BB are produced by two separate AA’s (only aa’s).

That implies any derived string has the form:

aibjak.a^{i}\, b^{j}\, a^{k}.

Now consider w=abaw = aba, i.e. i=1,j=1,k=1i=1, j=1, k=1.
In order to obtain exactly one aa before the bb, the left AA must generate exactly one aa, i.e. AaεA \Rightarrow a\varepsilon.
Similarly, to obtain exactly one aa after the bb, the right AA must also generate exactly one aa, i.e. AaεA \Rightarrow a\varepsilon.
The middle bb forces BbεB \Rightarrow b\varepsilon.

So for w=abaw=aba, the parse tree structure is essentially forced:

  • Left AA must be aεa\varepsilon
  • Middle BB must be bεb\varepsilon
  • Right AA must be aεa\varepsilon

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:

  • BB generates only bb’s, and the grammar has exactly one BB in the skeleton ABAA\,B\,A,
  • and both AA’s generate only aa’s with no way for aa’s to “move” across the BB,

the partition point between the aa-region and the bb-region is determined by the position of the bb’s in the final string. Likewise, the partition of the prefix and suffix aa’s is tied to the fact that both AA occurrences correspond to the two sides of the single BB.

Result: this CFG is unambiguous for all strings it generates because the decomposition aibjaka^{i} b^{j} a^{k} matches a unique choice of (i,j,k)(i,j,k), and for each ii (resp. kk) there is only one derivation shape for AaiA \Rightarrow a^{i} (it must apply the aAaA rule exactly ii times then ε\varepsilon 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 AA’s implies ambiguity,” but here the terminals are segregated: AA produces only aa and BB produces only bb.)

Formal justification via uniqueness of derivation counts

For AA:

  • Producing aia^{i} requires applying AaAA \rightarrow aA exactly ii times, then AεA \rightarrow \varepsilon.
  • That yields a unique parse tree shape for that ii (a chain of length ii ending in ε\varepsilon).

For BB similarly:

  • Producing bjb^{j} requires applying BbBB \rightarrow bB exactly jj times, then BεB \rightarrow \varepsilon.

Thus, for any specific string aibjaka^{i} b^{j} a^{k}, the parse tree is uniquely determined by (i,j,k)(i,j,k) and by the forced number of rule applications inside each AA and BB.

Therefore, the claim “the given grammar is ambiguous” is not supported: the grammar is unambiguous.

Footnotes

  1. Wikipedia — Context-free grammar productions and language generation (rules like AaAεA \to aA \mid \varepsilon yield aa^*). https://en.wikipedia.org/wiki/Regular_grammar - Covers how recursive productions with varepsilon\\varepsilon generate repetition languages; applicable to the behavior of AA and BB here. 2 3

  2. 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 (i,j,k)(i,j,k), 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 string

Choose a terminal string and attempt to derive it in multiple ways."

Construct two parse trees / derivations

2) Attempt two distinct derivations

Write leftmost derivations or explicit parse trees and check distinctness."

Same string, different structure

3) Validate yields match

Confirm both derivations yield identical terminals."

Unambiguity may follow from forced choices

4) If impossible, reassess

If terminal segregation forces unique rule counts (like here), ambiguity may not exist."

Ambiguity Quick Check

1 / 4
Question · Term

What does it mean for a CFG to be ambiguous?

Click to reveal
Answer · Definition

A CFG is ambiguous if there exists at least one string in its language that has two or more distinct parse trees (equivalently, distinct leftmost/rightmost derivations).

Knowledge Check

Question 1 of 4
Q1Single choice

A CFG is ambiguous if there exists: