CoursifyCoursify

Ambiguity in the Grammar (S \rightarrow ABA,; A \rightarrow aA \mid \epsilon,; B \rightarrow bB \mid \epsilon)

Ambiguity in the Grammar (S \rightarrow ABA,; A \rightarrow aA \mid \epsilon,; B \rightarrow bB \mid \epsilon)

Verified Sources
May 20, 2026

A context-free grammar is called ambiguous if there exists at least one string in its language that admits two distinct parse trees; equivalently, the same string has two different leftmost derivations or two different rightmost derivations.2 Ambiguity matters because a parser may assign multiple syntactic structures to the same input, which makes interpretation non-unique.2

For the grammar

SABAS \rightarrow ABA AaAϵA \rightarrow aA \mid \epsilon BbBϵB \rightarrow bB \mid \epsilon

the nonterminal AA generates any number of aa's, including none, so L(A)={aii0}L(A)=\{a^i \mid i\ge 0\}, and BB generates any number of bb's, including none, so L(B)={bjj0}L(B)=\{b^j \mid j\ge 0\}.2 Therefore, the start symbol generates strings of the form

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

This can also be written as a\*b\*a\*a^\* b^\* a^\*.2 The key source of ambiguity is that the two occurrences of AA in SABAS \to ABA can both generate aa-strings, so when a derived string contains only aa's, or when the middle BB vanishes to ϵ\epsilon, there may be multiple ways to distribute the same aa's between the left and right AA.2

A compact structural view is:

Because both A1A_1 and A2A_2 can independently derive a\*a^\* and BB can derive ϵ\epsilon, the boundary between the left and right blocks of aa's is not unique for some strings.2

Footnotes

  1. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees. 2 3 4 5

  2. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations. 2 3

  3. Context-Free Grammars and Languages - Discusses ambiguity, parse trees, and why multiple parses create non-unique structure. 2

  4. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings. 2

Ambiguous Grammar

Core Definition

To prove a grammar ambiguous, it is enough to find one string in the language that has two distinct parse trees or two distinct leftmost derivations.2

Footnotes

  1. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees.

  2. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations.

Why this grammar is ambiguous

Let us choose the string

w=a.w = a.

This string belongs to L(G)L(G) because we may let one AA generate aa, let BϵB \Rightarrow \epsilon, and let the other AϵA \Rightarrow \epsilon. However, there is also another derivation in which the first AϵA \Rightarrow \epsilon, BϵB \Rightarrow \epsilon, and the second AA generates aa. Since these correspond to different syntactic structures, the grammar is ambiguous.2

More generally, every string ana^n with n1n\ge 1 is ambiguous in this grammar. Since BϵB \Rightarrow \epsilon, we have

SABAaiϵak=ai+k,S \Rightarrow ABA \Rightarrow a^i \epsilon a^k = a^{i+k},

and for any fixed nn, there are multiple choices of (i,k)(i,k) such that i+k=ni+k=n. For example, a2a^2 can be split as a2ϵϵa^2\epsilon\epsilon, aϵaa\epsilon a, or ϵϵa2\epsilon\epsilon a^2. At least two such splits yield distinct parse trees, which is sufficient for ambiguity.2

An abstract view of the ambiguity for ana^n is:

Left (A) contributes(B) contributesRight (A) contributesResult
(a^n)(\epsilon)(\epsilon)(a^n)
(a^{n-1})(\epsilon)(a)(a^n)
(\cdots)(\epsilon)(\cdots)(a^n)
(\epsilon)(\epsilon)(a^n)(a^n)

Thus the grammar does not assign a unique structure to such strings.2

Footnotes

  1. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings. 2

  2. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees. 2 3

  3. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations. 2

  4. Context-Free Grammars and Languages - Discusses ambiguity, parse trees, and why multiple parses create non-unique structure.

Proof That the Grammar Is Ambiguous

  1. 1
    Step 1

    From AaAϵA \rightarrow aA \mid \epsilon, the nonterminal AA generates aa^*. From BbBϵB \rightarrow bB \mid \epsilon, the nonterminal BB generates bb^*. Hence SABAS \rightarrow ABA generates abaa^*b^*a^*.2

    Footnotes

    1. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees.

    2. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings.

  2. 2
    Step 2

    Choose the shortest nontrivial string aa. It is in the language because one occurrence of AA can produce aa, while BB and the other AA can both produce ϵ\epsilon.

    Footnotes

    1. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings.

  3. 3
    Step 3

    SABAaABAaBAaAa.S \Rightarrow ABA \Rightarrow aABA \Rightarrow aBA \Rightarrow aA \Rightarrow a. Here the left AA contributes the terminal aa, while BB and the right AA both derive ϵ\epsilon.

  4. 4
    Step 4

    SABABAAaAa.S \Rightarrow ABA \Rightarrow BA \Rightarrow A \Rightarrow aA \Rightarrow a. Here the left AA derives ϵ\epsilon, BB derives ϵ\epsilon, and the right AA contributes the terminal aa.

  5. 5
    Step 5

    These are two distinct leftmost derivations of the same terminal string aa. Therefore the grammar is ambiguous.2

    Footnotes

    1. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations.

    2. Context-Free Grammars and Languages - Discusses ambiguity, parse trees, and why multiple parses create non-unique structure.

Two distinct parse trees for the string (a)

Below are two different parse trees for the same string aa.

Parse Tree 1: the left AA produces aa, while BB and the right AA produce ϵ\epsilon.

Parse Tree 2: the left AA produces ϵ\epsilon, BB produces ϵ\epsilon, and the right AA produces aa.

Since these parse trees are structurally different but yield the same terminal string aa, the grammar is ambiguous by definition.2

Footnotes

  1. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees.

  2. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations.

Common Mistake

Different derivation orders do not by themselves prove ambiguity. The important point is that the same string must have different parse trees, or equivalently different leftmost derivations.2

Footnotes

  1. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations.

  2. Context-Free Grammars and Languages - Discusses ambiguity, parse trees, and why multiple parses create non-unique structure.

The grammar generates abaa^*b^*a^*. The source of ambiguity is that both copies of AA generate the same language aa^*, and BB may disappear by deriving ϵ\epsilon. Therefore some strings, especially ana^n, can be split between the two AA symbols in multiple ways.2

Footnotes

  1. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees.

  2. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings.

Number of Possible Splits of the String (a^n)

If (B \Rightarrow \epsilon), the (n) copies of (a) can be divided between the two occurrences of (A) in (n+1) ways.

Formal interpretation of the chart

For a string ana^n, if BϵB \Rightarrow \epsilon, then the left occurrence of AA may generate aia^i and the right occurrence may generate ania^{n-i} for any integer ii with 0in0 \le i \le n. Hence the number of possible splits is

n+1.n+1.

So for n=1n=1, there are 22 splits; for n=2n=2, there are 33 splits; and in general the grammar offers multiple structural decompositions of the same terminal string. The existence of more than one valid decomposition for even one value of n1n\ge 1 already proves ambiguity.2

This also shows that the ambiguity is not accidental or isolated: it is a systematic effect caused by overlapping generative roles of the two AA nonterminals combined with the epsilon production BϵB \Rightarrow \epsilon.2

Footnotes

  1. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings. 2

  2. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees. 2

  3. CS 373: Theory of Computation - University of Illinois - Explains ambiguity of CFGs in terms of different parse trees and derivations.

Further Clarifications

Exam Strategy

When asked to prove a grammar ambiguous, first compute what each nonterminal generates, then look for overlap caused by repeated nonterminals or epsilon-productions. Short strings often expose ambiguity fastest.2

Footnotes

  1. Parse trees, ambiguity, and Chomsky normal form - Defines ambiguity via existence of a string with at least two parse trees.

  2. Unambiguous Grammar - Includes this exact grammar as an ambiguity exercise and supports the analysis of how the productions generate strings.

Knowledge Check

Question 1 of 4
Q1Single choice

When is a context-free grammar called ambiguous?

Explore Related Topics

1

Lexical Analysis and the Main Structure Used: Finite Automata

Lexical analysis relies on finite automata—typically deterministic finite automata (DFA)—to recognize token patterns defined by regular expressions.

  • Regular expressions for identifiers, numbers, etc., are converted to NFAs then to a DFA for fast scanning.
  • The DFA processes the source character by character, tracking a single current state and emitting a token at each accepting state.
  • Queues, stacks, and trees support other compiler phases (parsing, AST construction) but are not the primary model for token recognition.
  • Lexers output a stream of tokens that the parser consumes for syntax analysis.
2

Which Grammar Type Is the Most Powerful? Understanding Type-0, Type-1, Type-2, and Type-3 Grammars

The most expressive grammar in the Chomsky hierarchy is the unrestricted Type‑0 grammar, which generates all recursively enumerable languages and matches the power of a Turing machine.

  • The hierarchy is strict: L3L2L1L0L_3 \subset L_2 \subset L_1 \subset L_0, so each higher type can generate everything the lower types can, plus more.
  • Type‑3 (regular) → finite automaton; Type‑2 (context‑free) → pushdown automaton; Type‑1 (context‑sensitive) → linear‑bounded automaton; Type‑0 (unrestricted) → Turing machine.
  • Example languages: {anbnn0}\{a^n b^n \mid n \ge 0\} is context‑free but not regular; {anbncnn1}\{a^n b^n c^n \mid n \ge 1\} is context‑sensitive but not context‑free.
  • “More powerful” refers to expressive capacity (ability to generate a larger class of languages), not ease of parsing or practical use.
3

Syntax-Directed Translation: Infix to Prefix Notation

The module shows how a syntax‑directed translation scheme using only synthesized attributes can convert infix arithmetic expressions into prefix (Polish) notation while preserving operator precedence and left‑associativity.

  • Grammar: E → E + T | E - T | T; T → T * F | F; F → digit, enforcing precedence ( *  > + / - ).
  • Semantic actions compute a val string for each non‑terminal, concatenating the operator before its operand strings.
  • Example results: 9 - 5 + 2+ - 9 5 2; 9 - 5 * 2- 9 * 5 2.
  • Synthesized (S‑attributed) attributes allow immediate bottom‑up evaluation during LR‑style parsing.
  • Left‑recursive rules enable left‑associativity; to use LL parsers the grammar must be transformed and inherited attributes introduced.
Chat with Kiro