Recall warm-up
- 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.
- 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).
- 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.
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.
Definition
Two different derivations alone prove nothing: one leftmost and one rightmost derivation of the same tree are normal. Compare like with like.
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).
| grammar | witness string | why two trees |
|---|---|---|
| S → SS | (S) | ε | ()()() | split as (()())() or ()(()()) |
| S → aS | Sa | a | aa | a then S→a, or S→a then a |
| S → if c then S | if c then S else S | a | if c then if c then a else a | else belongs to the inner or the outer if |
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.
Inherent ambiguity
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.
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
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. ∎
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.
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
- MCQ
A grammar is ambiguous iff some string has two distinct:
- derivations
- parse trees
- rightmost and leftmost derivations
- sentential forms
- MSQ
Which grammars are ambiguous?
- S → SS | a
- S → aS | a
- S → aSb | ε
- S → AB | aB, A → a, B → b
- MCQ
Which language is inherently ambiguous?
- {aⁿbⁿ}
- {aⁱbʲcᵏ : i = j or j = k}
- (a+b)*
- palindromes
- 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
- S → XY, X → aXb | ε, Y → cYd | ε.
- Leftmost derivations (and rightmost derivations).
- Yes, up to renaming.
Self-test answers
- B — Trees (or two leftmost / two rightmost derivations).
- A: Different orders of one tree are normal.
- C: One of each is normal.
- D: Irrelevant.
- AD — A: aaa groups two ways. D: ab via AB or aB.
- B: One way to build aⁿ.
- C: One way per n.
- B — The standard example.
- A: S → aSb | ε is unambiguous.
- C: Regular ⇒ unambiguous grammar exists.
- D: S → aSa | bSb | a | b | ε is unambiguous.
- 2 — (aa)a and a(aa).
Flashcards
- Definition of an ambiguous grammar?
Reveal answer
Some string has two or more parse trees (two leftmost derivations). - Standard inherently ambiguous language?
Reveal answer
{ aⁱbʲcᵏ : i = j or j = k } - Is S → SS | a ambiguous? Witness?
Reveal answer
Yes: aaa. - Can an ambiguous grammar generate a regular language?
Reveal answer
Yes, e.g. S → aS | Sa | a.