Skip to content

Normal Forms for Context-Free Grammars ​

A CFG can be transformed into equivalent but structurally simpler forms. These normal forms are essential for parsing algorithms (like CYK) and theoretical proofs.


Eliminating Useless Variables ​

A symbol is useful if it appears in some derivation S⇒∗αXβ⇒∗w (from start to terminal string). Otherwise it is useless.

Two Kinds of Uselessness ​

1. Variables That Derive Nothing ​

Example: S→AB,A→aA∣a,B→AB

B can never derive a terminal string (every production for B contains B itself). S also derives nothing because it depends on B.

Discovery Algorithm:

  • Basis: If A→w where w∈Σ∗, then A derives something
  • Induction: If A→X1X2…Xk and all Xi either are terminals or already discovered variables, then A derives something
  • Elimination: Remove all undiscovered variables and all productions containing them

Proof: Induction on parse tree height — if A⇒∗w, then A will be discovered in at most h rounds where h is the tree height.

2. Unreachable Symbols ​

A symbol is reachable if it appears in some sentential form derived from S.

Discovery Algorithm:

  • Basis: S is reachable
  • Induction: If A is reachable and A→α, then all symbols in α are reachable
  • Elimination: Remove unreachable symbols and their productions

Correct Two-Step Elimination ​

Important: Eliminate non-deriving variables FIRST, then unreachable symbols. Doing it in reverse order can leave useless symbols.

Why: Eliminating non-deriving variables may make previously reachable symbols unreachable.


Eliminating ε-Productions ​

A production of the form A→ε is called an ε-production.

Goal: Eliminate all ε-productions (except possibly S→ε if ε∈L(G)).

Nullable Symbols ​

A is nullable if A⇒∗ε.

Discovery Algorithm:

  • Basis: If A→ε, A is nullable
  • Induction: If A→X1X2…Xk and ALL Xi are nullable, then A is nullable

Elimination Method ​

Key idea: Turn each production A→X1X2…Xk into a family of productions — for each subset of nullable Xi, create a version where those symbols are omitted.

Example: S→AB,A→aA∣ε,B→bB∣A

  • Nullable: A (because of A→ε), then B (because B→A), then S (because S→AB)
  • New productions for S: S→AB∣A∣B (omit A, omit B, omit both — but S→ε removed)
  • A: A→aA∣a (omit nullable A in body)
  • B: B→bB∣b∣A

Handling ε∈L(G) ​

If ε is in the language, we introduce a new start symbol S0 with productions S0→S∣ε. The rest of the grammar has no ε-productions.


Eliminating Unit Productions ​

A unit production is one whose body consists of a single variable: A→B.

Finding Unit Pairs ​

Compute all pairs (A,B) such that A⇒∗B using only unit productions.

Algorithm: Find all such pairs by transitive closure.

  • Basis: (A,A) is a unit pair for all A
  • Induction: If (A,B) is a unit pair and B→C, then (A,C) is a unit pair

Elimination ​

For each unit pair (A,B), add all non-unit productions of B to A:

  • If B→α (where α is not a single variable), add A→α

Remove all unit productions.


Chomsky Normal Form (CNF) ​

A CFG is in Chomsky Normal Form if every production is of the form:

  1. A→BC (two variables)
  2. A→a (single terminal)

(Plus S→ε allowed if ε∈L(G))

CNF Theorem ​

Every CFL (without ε) has a grammar in CNF.

Proof (Construction) ​

Step 0 (ε handling): If ε is in the language, handle with S0→S∣ε.

Step 1 (Clean grammar): Eliminate useless variables, ε-productions, and unit productions. Now every production body is either a single terminal or a string of ≥2 symbols.

Step 2: For every terminal a that appears in a body of length >1, create a new variable A with production A→a, and replace a by A in all longer bodies.

Example: A→BcDe → create C→c, E→e, then A→BCDE

Step 3: Break right sides longer than 2 into a chain of productions, each with two variables.

Example: A→BCDE → A→BC1,C1→CD1,D1→DE

Significance of CNF ​

  • Every derivation of a string of length n uses exactly 2n−1 steps
  • Parse trees are binary — every interior node has exactly 2 children
  • Enables the CYK parsing algorithm (O(n3))