CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 3 · CFG, PDA & Context-Free Languages

Lesson 3.1.3 · Item 49 of 91

Ambiguity

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. CFG for aⁿbⁿcᵐdᵐ?
    Stuck? Peek at 3.1.2 Writing CFGs for standard languages
    • Wrap: S → aSb | ε (aⁿbⁿ); ratios S → aSbb.
    • Nested: outer wrap → inner wrap. Concatenation: S → XY. Union: S → S₁ | S₂. Star: S → S₁S | ε.
    • Palindromes S → aSa | bSb | a | b | ε. Balanced S → SS | (S) | ε. #a = #b: S → aSbS | bSaS | ε.
    • i ≠ j: S → AC | CB, C → aCb | ε, A → aA | a, B → bB | b.

    Read lesson 3.1.2

  2. What corresponds one-to-one with parse trees?
    Stuck? Peek at 3.1.1 Context-free grammars, derivations and parse trees
    • CFG: A → α, single variable on the left. CFL = L(G) for some CFG. Regular ⊂ CFL.
    • Leftmost / rightmost derivations; parse tree with yield = leaves left to right.
    • Trees ↔ leftmost derivations ↔ rightmost derivations (one-to-one per string).

    Read lesson 3.1.1

  3. Is the minimal DFA unique?
    Stuck? Peek at 1.3.1 Unreachable, equivalent and distinguishable states
    • Step 0: delete unreachable states (BFS from q₀).
    • p ≡ q ⇔ ∀w: δ̂(p,w) ∈ F ⇔ δ̂(q,w) ∈ F. Distinguishable ⇔ some w separates.
    • ≡₀ = {F, Q−F}; ≡ₖ₊₁ splits by where symbols lead w.r.t. ≡ₖ; stop when stable.
    • Merge classes → minimal DFA, unique up to renaming.

    Read lesson 1.3.1

The Hook

A calculator reads 2 + 3 * 4. Is it (2 + 3) * 4 = 20 or 2 + (3 * 4) = 14? If the grammar allows both parse trees, the meaning is undefined — a real bug class in compilers. A grammar with two trees for one string is ambiguous. GATE asks you to spot ambiguity, prove it, and know that some languages cannot avoid it.

Predict First

Grammar E → E + E | E * E | id. How many different parse trees does id + id * id have?

Core Concept

Prediction check

Two: one with + at the root (multiplication done first), one with * at the root (addition first). So this grammar is ambiguous.

steps illustration
Two parse trees for one string = ambiguity.

Definition

A CFG G is ambiguous if some string w ∈ L(G) has two or more parse trees — equivalently two leftmost derivations, or two rightmost derivations.

Two different derivations alone prove nothing: one leftmost and one rightmost derivation of the same tree are normal. Compare like with like.

two parse trees
ambiguous
two leftmost derivations
ambiguous
one leftmost + one rightmost
proves nothing
What counts as evidence.

Proving a grammar ambiguous

Exhibit one string and two distinct parse trees (or two leftmost derivations). Classic witnesses:

  • E → E+E | E*E | id: id+id*id.
  • S → SS | (S) | ε: ()()() (group left or right), and even ε (S ⇒ ε or S ⇒ SS ⇒ S ⇒ ε).
  • Dangling else: S → if c then S | if c then S else S | a — the else can attach to either if.
  • S → aS | Sa | a: aa (left or right growth).
grammarwitness stringwhy two trees
S → SS | (S) | ε()()()split as (()())() or ()(()())
S → aS | Sa | aaaa then S→a, or S→a then a
S → if c then S | if c then S else S | aif c then if c then a else aelse belongs to the inner or the outer if
Standard ambiguity witnesses.

Removing ambiguity (when possible)

Encode precedence and associativity with layered variables: E → E + T | T, T → T * F | F, F → (E) | id. Now * is always deeper than + and both group to the left; every string has one tree.

E handles +, lowest precedence
T handles *
F ( E ) or id — highest
Layered grammar: lower layers bind tighter, so each string has one tree.

Inherent ambiguity

A CFL is inherently ambiguous if every grammar for it is ambiguous.

Standard example: L = { aⁱbʲcᵏ : i = j or j = k }. Strings aⁿbⁿcⁿ satisfy both conditions and get one tree from each half of any grammar. Ambiguity is usually a property of a grammar; inherent ambiguity is a property of a language.

Facts to know: there is no general algorithm that decides whether a CFG is ambiguous (proved in Phase 5); every regular language has an unambiguous grammar.

Why { aⁱbʲcᵏ : i = j or j = k } is inherently ambiguous.
Why { aⁱbʲcᵏ : i = j or j = k } is inherently ambiguous.

Putting it together

  • Ambiguity = two parse trees for one string; prove it with one witness.
  • Precedence layers remove ambiguity in expression grammars.
  • Some CFLs are inherently ambiguous; no algorithm decides ambiguity in general.

Visual

dangling else:   if c then  if c then a  else a
                 └──────── outer ────────────────┘
                            └─ inner ─┘            else → inner?  (tree 1)
                            └──────── inner ───┘   else → outer?  (tree 2)

Worked Examples

Easy

Show S → aS | Sa | a is ambiguous.

Show the full solution

w = aa. Tree 1: S → aS, S → a. Tree 2: S → Sa, S → a. Two leftmost derivations: S ⇒ aS ⇒ aa and S ⇒ Sa ⇒ aa. ∎

Medium

Is S → aSb | ab ambiguous?

Show the full solution

No. Each string aⁿbⁿ (n ≥ 1) has exactly one derivation: n − 1 uses of aSb then ab. One tree per string ⇒ unambiguous.

GATE-level

Which are true? (i) An ambiguous grammar can generate a regular language. (ii) If a CFL has one ambiguous grammar, it is inherently ambiguous. (iii) {aⁿbⁿcᵐ} ∪ {aⁿbᵐcᵐ} is inherently ambiguous. (iv) Every DFA-recognizable language has an unambiguous CFG.

Show the full solution

(i) true: S → aS | Sa | a generates a⁺. (ii) false: the language may have another unambiguous grammar. (iii) true: it is the i = j or j = k language. (iv) true: the right-linear grammar from a DFA has one derivation per string. Answer: (i), (iii), (iv).

GATE Traps

  • Ambiguity is about parse trees (or leftmost derivations), not about any two derivations.
  • Ambiguous grammar ≠ inherently ambiguous language.
  • A grammar can be ambiguous on just one string (even ε) — that is enough.
  • There is no general algorithm for deciding whether a CFG is ambiguous.

Memory Hook

Ambiguity = one sentence, two family trees. Inherent ambiguity = a sentence so symmetric that every family-tree drawer must draw it twice.

Connect the Dots

Grammars can be messy: useless variables, ε-rules, chains like A → B. Next chapter cleans them up and brings every CFG into a standard shape — Chomsky Normal Form — which makes derivation lengths predictable and powers the membership algorithm.

Self-Test

  1. MCQ

    A grammar is ambiguous iff some string has two distinct:

    1. derivations
    2. parse trees
    3. rightmost and leftmost derivations
    4. sentential forms
  2. MSQ

    Which grammars are ambiguous?

    1. S → SS | a
    2. S → aS | a
    3. S → aSb | ε
    4. S → AB | aB, A → a, B → b
  3. MCQ

    Which language is inherently ambiguous?

    1. {aⁿbⁿ}
    2. {aⁱbʲcᵏ : i = j or j = k}
    3. (a+b)*
    4. palindromes
  4. NAT

    How many parse trees does S → SS | a give for aaa?

    Write your answer before opening the key.

Rule Card

  • Ambiguous: some w has ≥ 2 parse trees (≡ ≥ 2 leftmost derivations).
  • Witnesses: E+E/E*E with id+id*id; SS with three pieces; aS|Sa with aa; dangling else.
  • Fix with precedence layers E → E+T | T, T → T*F | F, F → (E) | id.
  • Inherently ambiguous language: every grammar ambiguous, e.g. aⁱbʲcᵏ with i=j or j=k. No algorithm decides ambiguity in general.

Concept Ledger

  • ambiguous grammar
  • unambiguous grammar
  • inherently ambiguous language
  • precedence / associativity layering
  • dangling else

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. S → XY, X → aXb | ε, Y → cYd | ε.
  2. Leftmost derivations (and rightmost derivations).
  3. Yes, up to renaming.

Self-test answers

  1. B — Trees (or two leftmost / two rightmost derivations).
    • A: Different orders of one tree are normal.
    • C: One of each is normal.
    • D: Irrelevant.
  2. AD — A: aaa groups two ways. D: ab via AB or aB.
    • B: One way to build aⁿ.
    • C: One way per n.
  3. B — The standard example.
    • A: S → aSb | ε is unambiguous.
    • C: Regular ⇒ unambiguous grammar exists.
    • D: S → aSa | bSb | a | b | ε is unambiguous.
  4. 2 — (aa)a and a(aa).

Flashcards

  1. Definition of an ambiguous grammar?
    Reveal answerSome string has two or more parse trees (two leftmost derivations).
  2. Standard inherently ambiguous language?
    Reveal answer{ aⁱbʲcᵏ : i = j or j = k }
  3. Is S → SS | a ambiguous? Witness?
    Reveal answerYes: aaa.
  4. Can an ambiguous grammar generate a regular language?
    Reveal answerYes, e.g. S → aS | Sa | a.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.