Introduction
Context-free languages are closed under union, concatenation, star, reverse and homomorphism, and under intersection with regular languages. They are not closed under intersection or complement. Membership, emptiness and finiteness are decidable for CFGs; equivalence and regularity are not. The two key tools are the contrast table against regular languages and a CYK membership trace.
Prerequisites
- Pushdown Automata: machine model.
- CFG Simplification and Normal Forms: CNF for CYK.
Core concepts
Closure table
| Operation | CFLs closed? | Notes |
|---|---|---|
| Union | yes | |
| Concatenation | yes | |
| Star | yes | |
| Reverse | yes | reverse every RHS |
| Homomorphism | yes | replace terminals by strings |
| Intersection with regular | yes | product of PDA with DFA |
| Intersection (two CFLs) | no | |
| Complement | no | follows from union + De Morgan, or direct |
| Difference | no | would follow |
| Inverse homomorphism | yes | run the PDA on for each input symbol |
| Substitution by CFLs | yes | replace each terminal by the start symbol of a grammar for |
| Difference with a regular language | yes | |
| Right quotient with a regular language | yes | PDA guesses the rest of the input from the DFA |
Closure across all classes
Y = closed, N = not closed. "Homomorphism" means arbitrary homomorphisms, which may erase symbols.
| Operation | REG | DCFL | CFL | CSL | REC | RE |
|---|---|---|---|---|---|---|
| Union | Y | N | Y | Y | Y | Y |
| Intersection | Y | N | N | Y | Y | Y |
| Intersection with a regular language | Y | Y | Y | Y | Y | Y |
| Complement | Y | Y | N | Y | Y | N |
| Difference | Y | N | N | Y | Y | N |
| Concatenation | Y | N | Y | Y | Y | Y |
| Kleene star | Y | N | Y | Y | Y | Y |
| Reversal | Y | N | Y | Y | Y | Y |
| Homomorphism | Y | N | Y | N | N | Y |
| -free homomorphism | Y | N | Y | Y | Y | Y |
| Inverse homomorphism | Y | Y | Y | Y | Y | Y |
| Right quotient with a regular language | Y | Y | Y | N | N | Y |
Counterexamples and reasons behind the N entries:
- DCFL union, intersection. and are DCFLs. is not a DCFL (it is inherently ambiguous), and is not even a CFL.
- DCFL concatenation. intersected with is , so the regular-by-DCFL product is not a DCFL although is one (the first symbol decides).
- DCFL reversal and homomorphism. is a DCFL; its reversal is not, and the homomorphism gives , which is not (see Deterministic PDAs and DCFLs).
- CFL intersection, complement, difference. The witness, then De Morgan; is a difference.
- CSL and REC under homomorphism and quotient. Use an input alphabet , a fresh certificate alphabet disjoint from , and a separator in neither alphabet. Encode the computation entirely over . For a TM , the language is context-sensitive and recursive (a computation can be checked in linear space). The letterwise homomorphism fixing each input symbol and erasing every certificate symbol and , or right quotient by the regular language , yields , which can be any RE language. So neither class is closed under these operations, while RE is.
- RE complement and difference. is RE and its complement is not; is a difference of RE languages.
- CSL complement is closed (the Immerman-Szelepcsenyi theorem), a fact that is easy to mis-mark as N.
Decision table
| Problem | CFGs | Regular (DFA) |
|---|---|---|
| Membership ? | decidable (CYK, ) | decidable |
| Emptiness | decidable | decidable |
| Finiteness | decidable | decidable |
| Equivalence ? | undecidable | decidable |
| Is ? | undecidable | decidable |
| Is regular? | undecidable | trivial |
How it works
Intersection with a regular language
Take a PDA for the CFL and a DFA for the regular language; run them as a product on the finite control while sharing the stack. Accept when both accept. This is the workhorse construction for proofs.
Non-closure under intersection
and are CFLs; their intersection is not CFL (pumping lemma).
CYK membership
For CNF grammar and string :
- For length 1: .
- For length : if with , for some split .
- Accept iff .

Algorithms and pseudocode
CYK(G_cnf, w)
0 if |w| = 0: return whether S -> eps is present (the stated CNF convention)
1 n <- |w|; T[i][j] <- empty set for all i,j
2 for i <- 1 to n
3 T[i][i] <- { A | A -> w[i] }
4 for len <- 2 to n
5 for i <- 1 to n-len+1
6 j <- i+len-1
7 for k <- i to j-1
8 for each A -> BC
9 if B in T[i][k] and C in T[k+1][j]: add A to T[i][j]
10 return S in T[1][n]Emptiness: compute generating symbols; test whether is generating.
Mathematical explanation
Why complement fails
If CFLs were closed under complement, then with closure under union they would be closed under intersection, contradicting the witness.
Decidability boundary
CFG emptiness reduces to "is generating?", a fixed-point computation on productions. Universality () is undecidable: the set of strings that are not accepting computation histories of a TM is a CFL, and it equals iff the TM accepts nothing. Equivalence is then undecidable too, because universality is the special case "is equal to " for a fixed grammar of . Intersection-emptiness of two CFGs is undecidable by a reduction from Post's correspondence problem.
Worked examples
Example 1: CYK on a five-letter string
Grammar in CNF with , , , . String baaba. Row lists the cells for substrings of length , from left to right.
| cells | |
|---|---|
| 1 | b: ; a: ; a: ; b: ; a: |
| 2 | ba: ; aa: ; ab: ; ba: |
| 3 | baa: ; aab: ; aba: |
| 4 | baab: ; aaba: |
| 5 | baaba: |
Sample cell: aa gets from with in both one-letter cells. Top cell: the split b + aaba gives , so adds and adds ; the split ba + aba gives , and , add and . Since is in the top cell, baaba is in the language. Cost: cells, each with up to splits, so time and membership-bit table space, where each cell stores one bit per variable (so for a fixed grammar).
Example 2: intersection with regular
still CFL (actually the same family trimmed).
Example 3: union keeps CFL
is CFL by grammar union; not regular.
Example 4: finiteness decision sketch
Remove useless symbols and convert to CNF. Draw a graph with an edge and for each rule . is infinite iff this graph has a cycle. For , , there is no cycle and ; adding creates the loop and makes infinite.
Example 5: equivalence undecidable
There is no algorithm that, given two CFGs, always correctly answers whether they define the same language. Contrast: minimize two DFAs and compare.
Important properties and observations
- CFL regular = CFL.
- CFL CFL may leave CFL.
- Membership is via CYK for CNF (faster practical parsers exist).
- DCFLs have different closure (complement yes, union no).
Common misconceptions.
- Believing CFL equivalence is decidable like DFA equivalence.
- Thinking intersection of CFLs is CFL because union is.
- Running CYK on a non-CNF grammar without conversion.
GATE examination perspective
- MEMORIZE BOTH TABLES. Closure and decision side by side with regular languages.
- WITNESS . Intersection of two CFLs.
- CYK TRACE. Fill the table bottom-up; do not skip splits.
- COMPLEMENT ARGUMENT. Non-closure under complement via De Morgan + intersection.
- CROSS-CLASS TABLE. The N entries to remember: CFL intersection and complement, DCFL union, RE complement, CSL and REC under arbitrary homomorphism.
- "CLOSED UNDER INTERSECTION WITH REGULAR". True for every class in the table; it is the standard way to filter a language before pumping.
- THE COMPLEMENT OF A CFL. Can still be a CFL (for example when the CFL is regular or a DCFL); non-closure means "not always".
- CYK CELL COUNT. A string of length has cells; the top cell is the whole string.
Summary
Sources
- J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley, Section 7.3 (closure properties: substitutions, reversal, intersection with a regular language, inverse homomorphism), Section 7.4 (decision properties: testing emptiness, testing membership, preview of undecidable CFL problems).
- M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 2.1 (Chomsky normal form), Section 4.1 (decidable problems concerning context-free languages), Section 5.1 (reductions via computation histories).
- P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning, Section 8.2 (closure properties and decision algorithms for context-free languages), Section 6.3 (a membership algorithm for context-free grammars), Section 11.3 (context-sensitive languages and linear bounded automata).