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

Core concepts

Closure table

OperationCFLs closed?Notes
UnionyesS→S1∣S2S \to S_1 \mid S_2
ConcatenationyesS→S1S2S \to S_1 S_2
StaryesS→S1S∣εS \to S_1 S \mid \varepsilon
Reverseyesreverse every RHS
Homomorphismyesreplace terminals by strings
Intersection with regularyesproduct of PDA with DFA
Intersection (two CFLs)no{anbncm}∩{anbmcm}={anbncn}\{a^n b^n c^m\} \cap \{a^n b^m c^m\} = \{a^n b^n c^n\}
Complementnofollows from union + De Morgan, or direct
DifferencenoL‾=Σ∗∖L\overline{L} = \Sigma^* \setminus L would follow
Inverse homomorphismyesrun the PDA on h(a)h(a) for each input symbol aa
Substitution by CFLsyesreplace each terminal aa by the start symbol of a grammar for LaL_a
Difference with a regular languageyesL∖R=L∩R‾L \setminus R = L \cap \overline{R}
Right quotient with a regular languageyesPDA 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.

OperationREGDCFLCFLCSLRECRE
UnionYNYYYY
IntersectionYNNYYY
Intersection with a regular languageYYYYYY
ComplementYYNYYN
Difference L1∖L2L_1 \setminus L_2YNNYYN
ConcatenationYNYYYY
Kleene starYNYYYY
ReversalYNYYYY
HomomorphismYNYNNY
ε\varepsilon-free homomorphismYNYYYY
Inverse homomorphismYYYYYY
Right quotient with a regular languageYYYNNY

Counterexamples and reasons behind the N entries:

  • DCFL union, intersection. LA={anbncm}L_A = \{a^n b^n c^m\} and LB={ambncn}L_B = \{a^m b^n c^n\} are DCFLs. LA∪LBL_A \cup L_B is not a DCFL (it is inherently ambiguous), and LA∩LB={anbncn}L_A \cap L_B = \{a^n b^n c^n\} is not even a CFL.
  • DCFL concatenation. {ε,d}⋅(dLA∪LB)\{\varepsilon, d\} \cdot (d L_A \cup L_B) intersected with d a∗b∗c∗d\,a^* b^* c^* is d(LA∪LB)d(L_A \cup L_B), so the regular-by-DCFL product is not a DCFL although dLA∪LBd L_A \cup L_B is one (the first symbol decides).
  • DCFL reversal and homomorphism. {c anbn}∪{d anb2n}\{c\,a^n b^n\} \cup \{d\,a^n b^{2n}\} is a DCFL; its reversal is not, and the homomorphism c,d↦cc, d \mapsto c gives {c anbn}∪{c anb2n}\{c\,a^n b^n\} \cup \{c\,a^n b^{2n}\}, which is not (see Deterministic PDAs and DCFLs).
  • CFL intersection, complement, difference. The LA∩LBL_A \cap L_B witness, then De Morgan; Σ∗∖L\Sigma^* \setminus L is a difference.
  • CSL and REC under homomorphism and quotient. Use an input alphabet Σ\Sigma, a fresh certificate alphabet Γc\Gamma_c disjoint from Σ\Sigma, and a separator #\# in neither alphabet. Encode the computation entirely over Γc\Gamma_c. For a TM MM, the language {w#c∣c is an accepting computation of M on w}\{ w\#c \mid c \text{ is an accepting computation of } M \text{ on } w\} 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 #Γc∗\#\Gamma_c^*, yields L(M)L(M), which can be any RE language. So neither class is closed under these operations, while RE is.
  • RE complement and difference. ATMA_{\mathrm{TM}} is RE and its complement is not; Σ∗∖ATM\Sigma^* \setminus A_{\mathrm{TM}} 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

ProblemCFGsRegular (DFA)
Membership w∈Lw \in L?decidable (CYK, O(n3)O(n^3))decidable O(n)O(n)
Emptinessdecidabledecidable
Finitenessdecidabledecidable
Equivalence L(G1)=L(G2)L(G_1)=L(G_2)?undecidabledecidable
Is L(G)=Σ∗L(G)=\Sigma^*?undecidabledecidable
Is L(G)L(G) regular?undecidabletrivial

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

L1={anbncm∣n,m≥0}L_1 = \{a^n b^n c^m \mid n,m \ge 0\} and L2={anbmcm∣n,m≥0}L_2 = \{a^n b^m c^m \mid n,m \ge 0\} are CFLs; their intersection {anbncn}\{a^n b^n c^n\} is not CFL (pumping lemma).

CYK membership

For CNF grammar and string w=a1…anw = a_1\ldots a_n:

  1. For length 1: Xi,i={A∣A→ai}X_{i,i} = \{ A \mid A \to a_i \}.
  2. For length ℓ≥2\ell \ge 2: A∈Xi,i+ℓ−1A \in X_{i,i+\ell-1} if A→BCA \to BC with B∈Xi,kB \in X_{i,k}, C∈Xk+1,i+ℓ−1C \in X_{k+1,i+\ell-1} for some split kk.
  3. Accept iff S∈X1,nS \in X_{1,n}.
CYK membership table for a sample string
Figure 1. CYK table for a length-5 string over a CNF grammar; cell (i,j) holds variables deriving the substring from i to j; acceptance iff S appears in the top cell.

Algorithms and pseudocode

Text
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 SS 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 anbncna^n b^n c^n witness.

Decidability boundary

CFG emptiness reduces to "is SS generating?", a fixed-point computation on productions. Universality (L(G)=Σ∗L(G) = \Sigma^*) is undecidable: the set of strings that are not accepting computation histories of a TM is a CFL, and it equals Σ∗\Sigma^* iff the TM accepts nothing. Equivalence is then undecidable too, because universality is the special case "is L(G)L(G) equal to L(Gall)L(G_{\mathrm{all}})" for a fixed grammar of Σ∗\Sigma^*. 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 S→AB∣BCS \to AB \mid BC, A→BA∣aA \to BA \mid a, B→CC∣bB \to CC \mid b, C→AB∣aC \to AB \mid a. String baaba. Row ℓ\ell lists the cells for substrings of length ℓ\ell, from left to right.

ℓ\ellcells
1b: {B}\{B\}; a: {A,C}\{A,C\}; a: {A,C}\{A,C\}; b: {B}\{B\}; a: {A,C}\{A,C\}
2ba: {S,A}\{S,A\}; aa: {B}\{B\}; ab: {S,C}\{S,C\}; ba: {S,A}\{S,A\}
3baa: ∅\emptyset; aab: {B}\{B\}; aba: {B}\{B\}
4baab: ∅\emptyset; aaba: {S,A,C}\{S,A,C\}
5baaba: {S,A,C}\{S,A,C\}

Sample cell: aa gets BB from B→CCB \to CC with CC in both one-letter cells. Top cell: the split b + aaba gives B⋅{S,A,C}B \cdot \{S,A,C\}, so A→BAA \to BA adds AA and S→BCS \to BC adds SS; the split ba + aba gives {S,A}⋅{B}\{S,A\} \cdot \{B\}, and S→ABS \to AB, C→ABC \to AB add SS and CC. Since SS is in the top cell, baaba is in the language. Cost: (n+12)\binom{n+1}{2} cells, each with up to n−1n-1 splits, so O(n3⋅∣G∣)O(n^3 \cdot |G|) time and O(n2∣V∣)O(n^2\lvert V\rvert) membership-bit table space, where each cell stores one bit per variable (so O(n2)O(n^2) for a fixed grammar).

Example 2: intersection with regular

{anbn∣n≥0}∩a∗bb∗={anbn∣n≥1}\{a^n b^n \mid n \ge 0\} \cap a^* b b^* = \{a^n b^n \mid n \ge 1\} still CFL (actually the same family trimmed).

Example 3: union keeps CFL

{anbn}∪{anb2n}\{a^n b^n\} \cup \{a^n b^{2n}\} 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 A→BA \to B and A→CA \to C for each rule A→BCA \to BC. L(G)L(G) is infinite iff this graph has a cycle. For S→ABS \to AB, A→aA \to a, B→bB \to b there is no cycle and L={ab}L = \{ab\}; adding B→BBB \to BB creates the loop B→BB \to B and makes L=ab+L = a b^+ 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 ∩\cap regular = CFL.
  • CFL ∩\cap CFL may leave CFL.
  • Membership is O(n3)O(n^3) 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 anbncna^n b^n c^n. 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 nn has n(n+1)/2n(n+1)/2 cells; the top cell is the whole string.

Summary

Sources

  1. 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).
  2. 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).
  3. 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).

Next steps