THEORY OF COMPUTATION/Student Engineering Notebook
Page 05·notes
CONTEXT-FREE GRAMMARS, AMBIGUITY & PUSHDOWN AUTOMATA—~30 MIN READ

CFG Derivations, Ambiguity Proofs & Chomsky Normal Form

“Parse tree multiplicity, inherent ambiguity, CNF 2n-1 steps, and CYK parsing.”

1.Context-Free Grammars & The Ambiguity Problem

A Context-Free Grammar (CFG) is a 4-tuple G = (V, T, P, S), where production rules have the form A \to \alpha, with A \in V (a single non-terminal variable) and \alpha \in (V \cup T)^*.

Ambiguity in Grammars:

A grammar G is ambiguous if there exists at least one string w \in L(G) that has two or more distinct leftmost derivations (or equivalently, two or more distinct parse trees).

Inherent Ambiguity:

A context-free language L is inherently ambiguous if EVERY grammar that generates L is ambiguous. Classic inherently ambiguous language:

$L = { a^n b^n c^m d^m \mid n, m ≥ 1 } \cup { a^n b^m c^m d^n \mid n, m ≥ 1 }$

For strings where n = m (a^n b^n c^n d^n), every grammar is forced to generate duplicate parse trees.

Chomsky Normal Form (CNF):

A grammar is in CNF if all production rules are strictly of the form: A \to BC or A \to a (where A, B, C \in V and a \in T).

CNF Derivation Length Theorem: If a string w \in L(G) has length n ≥ 1, any derivation of w in a CNF grammar requires EXACTLY 2n - 1 steps (specifically: n - 1 rule applications of A \to BC, followed by n rule applications of A \to a).

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

2. GATE Worked Numerical: Derivation Steps in CNF Grammar

⚠️ Pitfall Warning:

The formula 2n - 1 applies ONLY when the grammar is in Chomsky Normal Form (CNF)! For general grammars, derivation lengths can vary.

Problem: A context-free grammar G is in Chomsky Normal Form. A string w of length 15 is derived in G. How many derivation steps were executed?

Step 1: Identify CNF production structure:

Binary productions (A -> BC) increase the count of non-terminals by 1.

To grow from 1 start symbol S to 15 non-terminals requires: 15 - 1 = 14 applications of A -> BC.

Step 2: Terminal productions (A -> a):

Each of the 15 non-terminals must be converted into a terminal symbol: 15 applications of A -> a.

Step 3: Total derivation steps:

Total steps = (n - 1) + n = 2n - 1 = 2(15) - 1 = 30 - 1 = 29 steps.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: Derivation length 2n − 1 is why the CYK algorithm's table is n × n.

Worked example: derivation length in Chomsky Normal Form

Problem Statement:
A grammar in Chomsky Normal Form derives a string of length 5. How many steps does the derivation take?
Step 1: CNF rules

Every rule is A → BC or A → a.

Step 2: Grow to n variables

Each A → BC step adds one symbol. Going from S to n variables takes n − 1 steps.

Step 3: Turn variables into terminals

Each A → a step turns one variable into a terminal: n steps.

Step 4: Total

(n − 1) + n = 2n − 1.

2·5 − 1
↳ Result: 9
Final Answer:9 steps
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · GATE-LEVEL

In the ambiguous grammar E → E + E | a, how many parse trees does a + a + a have?

Problem 2 · GATE-LEVEL

Same grammar, how many parse trees does a + a + a + a have?

Problem 3 · TRICKY

Is it decidable whether an arbitrary CFG is ambiguous?

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Introduction to Automata Theory, Languages, and Computation (3rd Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Gate Smashers · Theory of Computation↗Instructor: Varun Singla · Covers: DFA/NFA, minimization, CFG, PDA, Turing machines, decidability

GATE-focused with clear minimal DFA construction and language classification.

Open YouTube↗
Knowledge Gate · TOC for GATE↗Instructor: Sanchit Jain · Covers: Regular expressions, pumping lemma, closure properties, Rice's theorem

Exam-pattern oriented with closure table drilling and decidability proofs.

Open YouTube↗
Ravindrababu Ravula · TOC Lectures↗Instructor: Ravindrababu Ravula · Covers: Formal proofs, Myhill-Nerode theorem, undecidability reductions

Mathematically rigorous treatment for high-rank GATE aspirants.

Open YouTube↗