Recall warm-up
- Safe order of grammar simplification?
Stuck? Peek at 3.2.1 Removing useless symbols, ε- and unit productions
- Useful = generating + reachable. Remove non-generating first, then unreachable.
- Nullable: A ⇒* ε. Add all versions dropping nullable occurrences; delete ε-rules; add S₀ → S | ε if needed.
- Unit pairs by reachability over unit rules; copy non-unit bodies; delete unit rules.
- Order: ε → unit → useless.
- Do parse trees correspond one-to-one with leftmost derivations?
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).
- What are the two parts of an induction proof?
Stuck? Peek at 0.2.3 Proof techniques
- P ⇒ Q false only when P true, Q false. Contrapositive ¬Q ⇒ ¬P ≡ original. Converse/inverse are NOT.
- Contradiction: assume ¬S, derive the impossible. Used for “no machine can…” proofs.
- Induction: base S(n₀) + step S(k) ⇒ S(k+1). Strong induction assumes all S(≤k).
- One counterexample kills a “for all”. Examples never prove it.
The Hook
Algorithms love uniform input. If every rule had exactly the same shape, we could predict how long derivations are, bound the height of parse trees, and fill a table to test membership. Two standard shapes exist: Chomsky Normal Form (binary trees) and Greibach Normal Form (one terminal per step). GATE asks for their derivation lengths almost every time this topic appears.
Predict First
In a grammar where every rule is either A → BC (two variables) or A → a (one terminal), how many derivation steps produce a string of length 4?
Core Concept
Prediction check
7. You need 4 terminal steps (A → a), and to have 4 variables you need 3 splitting steps (each A → BC adds one variable). 3 + 4 = 7 = 2·4 − 1.
Chomsky Normal Form
Every CFL (without ε) has a CNF grammar.
Converting to CNF
- Simplify (ε, unit, useless).
- For each terminal a in a body of length ≥ 2, add Xₐ → a and replace a by Xₐ.
- Break long bodies: A → B₁B₂…Bₖ becomes A → B₁C₁, C₁ → B₂C₂, …, Cₖ₋₂ → Bₖ₋₁Bₖ.
Example: S → aSb | ab ⇒ Xa → a, Xb → b, S → XaSXb | XaXb ⇒ S → XaY | XaXb, Y → SXb.
Derivation length and tree height in CNF
For a string of length n ≥ 1: exactly 2n − 1 derivation steps (n − 1 binary steps + n terminal steps). The parse tree is a binary tree above the leaves, so a string of length n needs height at least ⌈log₂ n⌉ + 1; conversely a tree of height h yields a string of length at most 2^(h−1). That bound powers the CFL pumping lemma (Chapter 3.4).
Greibach Normal Form
Each step produces exactly one terminal, so a string of length n takes exactly n steps. GNF has no left recursion (A ⇒⁺ A…), and it is the shape a stack machine can simulate one input symbol per move — a preview of the next chapter.
Putting it together
- CNF: binary trees, 2n − 1 steps, height bounds yield length.
- GNF: one terminal per step, n steps.
- Every CFL (minus ε) has grammars in both forms.
Visual
CNF tree for length n: n leaves (terminal steps)
n − 1 binary nodes (A → BC)
total 2n − 1 rule applications
height h ⇒ |w| ≤ 2^(h−1) ; |w| = n ⇒ height ≥ ⌈log₂ n⌉ + 1Worked Examples
A CNF grammar derives a string of length 10. Number of steps?
Show the full solution
2·10 − 1 = 19.
Convert S → ASB | ab, A → a, B → b to CNF.
Show the full solution
Terminals in long bodies: Xa → a, Xb → b; S → ASB | XaXb. Break ASB: S → AY, Y → SB. Result: S → AY | XaXb, Y → SB, A → a, B → b, Xa → a, Xb → b. (A, B could replace Xa, Xb.)
A CNF grammar has a parse tree of height 5 (longest root-to-leaf path has 5 edges). What is the maximum length of its yield? A GNF grammar derives a string in 12 steps: its length?
Show the full solution
Height counts edges; the last edge is A → a. The binary part has height 4, so at most 2⁴ = 16 leaves. GNF: one terminal per step ⇒ length 12.
GATE Traps
- CNF steps = 2n − 1, not 2n and not n.
- GNF steps = n.
- CNF cannot have unit rules or ε-rules (except S → ε with S never on a right side).
- Watch how a question measures height (edges vs levels).
Memory Hook
CNF = Chomsky Cuts in two (binary): 2n − 1. GNF = Greibach Gives one terminal per step: n.
Connect the Dots
Grammar side complete. Now the machine side: a finite automaton plus a stack — the push-down automaton — which accepts exactly the context-free languages. Its natural ancestor is the GNF derivation you just saw.
Self-Test
- NAT
Steps in a CNF derivation of a string of length 15?
Write your answer before opening the key.
- NAT
Steps in a GNF derivation of a string of length 15?
Write your answer before opening the key.
- MSQ
Which rules are allowed in CNF?
- A → BC
- A → a
- A → aB
- A → B
- MCQ
A CNF parse tree has 9 leaves (all terminals). Number of internal nodes using A → BC rules?
- 8
- 9
- 17
- 4
Rule Card
- CNF: A → BC | a (+ S → ε). Length n ⇒ 2n − 1 steps; n − 1 binary nodes. Height h ⇒ |w| ≤ 2^(h−1).
- Conversion: simplify, replace terminals in long bodies, split long bodies.
- GNF: A → aα. Length n ⇒ n steps. No left recursion.
Concept Ledger
- Chomsky Normal Form (CNF)
- Greibach Normal Form (GNF)
- 2n − 1 derivation steps
- binary parse tree height bound
- left recursion
Answer Vault
Reveal answers when you’re ready
Warm-up answers
- ε-productions, unit productions, useless symbols.
- Yes.
- Base case and inductive step.
Self-test answers
- 29 — 2·15 − 1.
- 15 — One terminal per step.
- AB — Only these two shapes.
- C: GNF shape, not CNF.
- D: Unit rule.
- A — n − 1 binary nodes.
- B: That counts terminal rules.
- C: That is the total 2n − 1.
- D: Too small.
Flashcards
- CNF rule shapes?
Reveal answer
A → BC or A → a (plus S → ε if needed). - Derivation steps in CNF for length n?
Reveal answer
2n − 1 - Derivation steps in GNF for length n?
Reveal answer
n - GNF rule shape?
Reveal answer
A → aα (a terminal then variables).