The Wayback Machine - https://web.archive.org/web/20261126065147/https://www.geeksforgeeks.org/compiler-design/ambiguous-grammar/

Ambiguous and Unambiguous Grammar in Compiler Design

Last Updated : 17 Nov, 2026

A Context-Free Grammar (CFG) describes the structure of a language using production rules. Based on the number of parse trees generated for a string, CFGs are classified as:

  • Ambiguous Grammar
  • Unambiguous Grammar

Ambiguous grammar 

A CFG is ambiguous if at least one string has multiple parse trees, or equivalently, multiple leftmost derivations or multiple rightmost derivations.

A CFG G = (V, T, P, S) is ambiguous if a string in T* can be derived in more than one way.

A CFG G = (V, T, P, S) is said to be ambiguous if there exists at least one string w ∈ T* that can be generated in more than one way (i.e., it has two or more distinct parse trees, leftmost derivations, or rightmost derivations).

Where:

  • V: Finite set of non-terminal symbols (variables).
  • T: Finite set of terminal symbols, where V ∩ T = ∅ .
  • P: Finite set of production rules of the form A -> α, where A ∈ V and α ∈ (V ∪ T)*.
  • S: The start symbol, where S ∈ V.
ambiguous_grammar

Both the above parse trees are derived from the same grammar rules but both parse trees are different. Hence the grammar is ambiguous.    

Example 2: Consider the following grammar:  

Set of symbols Σ = {0, …, 9, +, *, (, )}

E → I
E → E + E
E → E * E
E → (E)
I → ε | 0 | 1 | ... | 9
 

Derivation 1: 3*(2+5)

E ⇒ E * E
  ⇒ I * E
  ⇒ 3 * E
  ⇒ 3 * E + E
  ⇒ 3 * I + E
  ⇒ 3 * 2 + E
  ⇒ 3 * 2 + I
  ⇒ 3 * 2 + 5

Derivation 2: (3*2)+5

E ⇒ E + E
  ⇒ E * E + E
  ⇒ I * E + E
  ⇒ 3 * E + E
  ⇒ 3 * I + E
  ⇒ 3 * 2 + E
  ⇒ 3 * 2 + I
  ⇒ 3 * 2 + 5

Both derivations generate the same string but represent different groupings. Therefore, the grammar is ambiguous.

Unambiguous Grammar

A CFG is unambiguous if every string has only one parse tree (one leftmost and one rightmost derivation).

Example:

E → E + T | T
T → T * F | F
F → (E) | id

This grammar defines operator precedence (multiplication before addition), so each string has only one valid parse tree.

Removal of Ambiguity in Grammar

To remove ambiguity from a grammar, follow these simple steps:

  1. Simplify production rules: Break down complex rules into smaller and simpler ones. This way, the grammar won't allow multiple interpretations for the same string.
  2. Set precedence and associativity: For things like math operations, make sure the order in which they are applied is clear. For example, define that multiplication happens before addition, or how to group operations (left-to-right or right-to-left).
  3. Restructure Production Rules: Rewrite ambiguous production rules to ensure that each string has a unique parse tree.
  4. Factor out common parts: If two rules start the same way, combine the common part. For example, instead of having rules like A → αβ and A → αγ, make it A → αA' and A' → β | γ.

Inherent Ambiguity

A Context-Free Language (CFL) is said to be inherently ambiguous if every possible grammar for the language is ambiguous. This means no matter how you write the grammar for the language, there will always be strings in that language that can be parsed in more than one way.

Example:

Consider the language L defined as:

L = { aⁿbⁿcᵐdᵐ : n ≥ 1, m ≥ 1 } ∪ { aⁿbᵐcᵐdⁿ : n ≥ 1, m ≥ 1 }

A grammar for this language could be:

  • S → AB | C
  • A → aAb | ab
  • B → cBd | cd
  • C → aCd | aDd
  • D → bDc | bc

Let’s see how the string aabbccdd can be parsed. This string has two possible leftmost derivations:

Using S → AB:

  • S ⇒ AB
  • A → aAb ⇒ aAbB
  • A → ab ⇒ aabbB
  • B → cBd ⇒ aabbcBd
  • B → cd ⇒ aabbccdd

Using S → C:

  • S ⇒ C
  • C → aCd ⇒ aCd
  • C → aDd ⇒ aaDdd
  • D → bDc ⇒ aabDcdd
  • D → bc ⇒ aabbccdd
ambiguous_grammar_2

Both derivations result in the same string aabbccdd, but they come from different rules and paths, showing that there is more than one way to parse the string.

Since there are multiple ways to parse the string aabbccdd and this pattern holds for all grammars of L, we can conclude that L is inherently ambiguous. This means no grammar for L can avoid ambiguity for all strings in the language.

Important Points on Ambiguous Grammar

1. A grammar is ambiguous if a string has more than one parse tree or derivation. This combination can lead to multiple ways of deriving the same string, creating more than one parse tree.

Example:

S → SaS | ε

The grammar is ambiguous because the string aa can be generated using multiple leftmost derivations, resulting in different parse trees.

ambiguous_grammar_4

2. Even if a grammar does not have both left and right recursion, it can still be ambiguous. The absence of recursion does not guarantee that the grammar is unambiguous.

Example:

S → aB | ab
B → b

The string ab can be derived in two ways:

  • S ⇒ ab
  • S ⇒ aB ⇒ ab

Therefore, the grammar is ambiguous even though it has no recursive productions.

ambiguous_grammar_3
Comment

Explore