G27
Self-checkedTier 1 9 min readtoc.cf-closure-properties

Closure properties of CFLs

Which operations keep a language context-free: union, concatenation, star, reversal, homomorphism, inverse homomorphism and intersection with a regular language (with the grammar or product constructions), and which do not: intersection, complement and difference (counterexample a^n b^n c^n); the complement of a^n b^n c^n is nevertheless context-free; using closure to prove that a language is not context-free.

Why it exists

Closure properties are the quickest way to answer "is this language context-free?" for combined languages, and they are a favourite GATE topic because the answers differ from the regular case: context-free languages are not closed under intersection or complement. The answer table must be known by heart, and so must the counterexamples.

Prerequisites

You need the grammar and PDA descriptions of context-free languages and their equivalence (PDA and CFG).

Precise definitions

A class of languages is closed under an operation if applying the operation to members always gives a member. For the class of context-free languages (CFLs):

operationclosed?how
union L1∪L2L_1 \cup L_2yesgrammars: S→S1∣S2S \to S_1 \mid S_2
concatenation L1L2L_1 L_2yesgrammars: S→S1S2S \to S_1 S_2
star L∗L^*yesgrammars: S→S1S∣εS \to S_1 S \mid \varepsilon
reversal LRL^Ryeswrite every right side backwards
homomorphism h(L)h(L)yesreplace each terminal by its image in the grammar
inverse homomorphism h−1(L)h^{-1}(L)yes (quoted)PDA that applies hh to the input symbol by symbol
intersection with a regular language L∩RL \cap RyesPDA ×\times DFA product
intersection L1∩L2L_1 \cap L_2no{aibick}∩{aibjcj}={anbncn}\{a^i b^i c^k\} \cap \{a^i b^j c^j\} = \{a^n b^n c^n\}
complement L‾\overline{L}noDe Morgan: closed under union and complement would give closed under intersection
difference L1∖L2L_1 \setminus L_2noL‾=Σ∗∖L\overline{L} = \Sigma^* \setminus L

Also closed (quoted): substitution of context-free languages for the symbols, quotient by a regular language, prefix closure. Not closed: infinite unions and arbitrary subsets (as for regular languages).

How it works

The grammar constructions

For G1G_1: S→aSb∣εS \to aSb \mid \varepsilon (the language anbna^n b^n) and G2G_2: S→cS∣cS \to cS \mid c (the language c+c^+) the constructions give (renaming the variables to S1,S2S_1, S_2 and adding a new start symbol SS):

Closure constructions for context-free languages: start

Computed by a script, not typed
operationresultsizeconstruction
union G1 ∪ G2
concatenation G1 G2
star G1*
reversal of G1
homomorphism h(a) = xy, h(b) = z
intersection with a regular language (PDA × DFA)

Step 0

G1 is S → aSb | ε (the language a^n b^n) and G2 is S → cS | c (the language c⁺). Each construction was run and its language compared with the set definition on all short strings.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

Each result was checked against the set definition of the operation on all strings up to length 88 (and on 150150 random pairs of grammars).

Why the constructions work. Union: any derivation starts with S→S1S \to S_1 or S→S2S \to S_2 and then continues inside one grammar; concatenation: a string of L1L2L_1 L_2 is a string of L1L_1 followed by one of L2L_2; star: S⇒S1S⇒S1S1S⇒⋯⇒S1⋯S1S \Rightarrow S_1 S \Rightarrow S_1 S_1 S \Rightarrow \cdots \Rightarrow S_1 \cdots S_1; reversal: a parse tree turned over is a parse tree of the reversed grammar; homomorphism: replacing a leaf aa by the string h(a)h(a).

Intersection with a regular language

Run a PDA PP for LL and a DFA DD for RR side by side: the states are pairs, the stack is PP's stack, the DFA component changes only when an input symbol is read, and the pair accepts when both components accept. The row at the end of the table above: a PDA for anbna^n b^n (4 states) and a DFA for "length divisible by 44" (4 states) give a product with 1616 states (13 reachable) that accepts a2kb2ka^{2k} b^{2k}. This is the only kind of intersection that is safe: with a second context-free language there are two stacks.

Intersection and complement fail

L1={aibick:i,k≥0}L_1 = \{a^i b^i c^k : i, k \ge 0\} is context-free (grammar S→XCS \to XC, X→aXb∣εX \to aXb \mid \varepsilon, C→cC∣εC \to cC \mid \varepsilon), and so is L2={aibjcj}L_2 = \{a^i b^j c^j\} (grammar S→AYS \to AY, A→aA∣εA \to aA \mid \varepsilon, Y→bYc∣εY \to bYc \mid \varepsilon). Their intersection is

L1∩L2={anbncn:n≥0},L_1 \cap L_2 = \{a^n b^n c^n : n \ge 0\},

which is not context-free (pumping lemma on the next page). Hence CFLs are not closed under intersection (checked: both languages equal their grammars on all strings up to length 1212 and the intersection is anbncna^n b^n c^n). If CFLs were closed under complement, then L1∩L2=L1‾∪L2‾‾L_1 \cap L_2 = \overline{\overline{L_1} \cup \overline{L_2}} would be context-free (closed under union): a contradiction. So they are not closed under complement either, and not under difference (take Σ∗∖L\Sigma^* \setminus L).

The complement of a non-CFL can be a CFL

The language {anbncn}\{a^n b^n c^n\} is not context-free but its complement is. A string is outside the language if (1) it is not of the form a∗b∗c∗a^* b^* c^* (it contains baba, caca or cbcb): a regular condition; or (2) it is aibjcka^i b^j c^k with i≠ji \ne j; or (3) with j≠kj \ne k. Each of the three is context-free:

S→R∣P∣Q,R→X ba X∣X ca X∣X cb X,X→aX∣bX∣cX∣ε,S \to R \mid P \mid Q, \quad R \to X\,ba\,X \mid X\,ca\,X \mid X\,cb\,X, \quad X \to aX \mid bX \mid cX \mid \varepsilon, P→T C,T→aTb∣A∣B,A→aA∣a,B→bB∣b,C→cC∣ε,P \to T\,C, \quad T \to aTb \mid A \mid B, \quad A \to aA \mid a, \quad B \to bB \mid b, \quad C \to cC \mid \varepsilon, Q→D U,D→aD∣ε,U→bUc∣E∣F,E→bE∣b,F→cF∣c.Q \to D\,U, \quad D \to aD \mid \varepsilon, \quad U \to bUc \mid E \mid F, \quad E \to bE \mid b, \quad F \to cF \mid c.

The union is the complement of {anbncn}\{a^n b^n c^n\} (compared with the definition on all strings up to length 99). So "the complement of a CFL need not be a CFL" and "the complement of a non-CFL may be a CFL" are both true statements.

Using closure to show a language is not context-free

If LL is context-free then so is L∩RL \cap R for every regular RR. To show that LL is not context-free find a regular RR so that L∩RL \cap R is known not to be.

  • L={w∈{a,b,c}∗:#a=#b=#c}L = \{w \in \{a, b, c\}^* : \#a = \#b = \#c\}. Then L∩a∗b∗c∗={anbncn}L \cap a^* b^* c^* = \{a^n b^n c^n\} is not context-free, so LL is not.
  • L={ww:w∈{a,b}∗}L = \{ww : w \in \{a, b\}^*\}: intersect with a∗b∗a∗b∗a^* b^* a^* b^*; the result is {aibjaibj}\{a^i b^j a^i b^j\}, which the pumping lemma excludes.
  • Use a homomorphism to erase symbols or rename them and reach a known language.

Worked examples

EX 1Which operations preserve context-freeness

Easy

L1∪L2L_1 \cup L_2, L1L2L_1 L_2, L1∗L_1^*, L1RL_1^R, L1∩RL_1 \cap R with RR regular: all context-free. L1∩L2L_1 \cap L_2, L1‾\overline{L_1}: not necessarily.

EX 2Count the productions

Easy

G1G_1 has 22 productions, G2G_2 has 22. The union grammar has 2+2+2=62 + 2 + 2 = 6, the concatenation 1+2+2=51 + 2 + 2 = 5, the star 2+2=42 + 2 = 4 (one of them S→S1SS \to S_1 S and one S→εS \to \varepsilon, plus the 22 of G1G_1).

EX 3Is the intersection context-free

Medium

{anbn}∩{a∗b∗}\{a^n b^n\} \cap \{a^* b^*\} = {anbn}\{a^n b^n\}: yes (regular intersection). {anbn}∩{cndn}\{a^n b^n\} \cap \{c^n d^n\} = ∅\emptyset, regular and context-free. {aibick}∩{aibjcj}\{a^i b^i c^k\} \cap \{a^i b^j c^j\} = {anbncn}\{a^n b^n c^n\}: not context-free. Closure does not say "never": a particular intersection of two CFLs can be context-free; the point is that it may fail.

EX 4Show non-context-freeness by closure

Medium

Let L={w∈{a,b,c}∗:#a(w)=#b(w)=#c(w)}L = \{w \in \{a, b, c\}^* : \#a(w) = \#b(w) = \#c(w)\} and R=a∗b∗c∗R = a^* b^* c^*, which is regular. L∩R={anbncn}L \cap R = \{a^n b^n c^n\}. If LL were context-free, so would be L∩RL \cap R; it is not; therefore LL is not context-free.

EX 5Typical questions

GATE level

(a) "CFLs are closed under: union / intersection / complement / reversal / concatenation." Union, reversal, concatenation (not intersection or complement). (b) "If L1L_1 is context-free and RR regular, L1∩RL_1 \cap R is:" context-free. (c) "If L1L_1 and L2L_2 are CFLs then L1∩L2L_1 \cap L_2 is a CFL." False in general (counterexample above). (d) "The language {anbncn}\{a^n b^n c^n\} is the intersection of two CFLs:" true. (e) "Is {ww}‾\overline{\{ww\}} context-free?" Yes (quoted textbook fact), while {ww}\{ww\} is not. (f) "Is L1∖L2L_1 \setminus L_2 context-free for CFLs?" Not necessarily; but L1∖RL_1 \setminus R is for regular RR.

Cheat box

Cheat box

  • CFL closed: union, concatenation, star, reversal, homomorphism, inverse homomorphism, intersection with a regular language.
  • CFL not closed: intersection, complement, difference.
  • Counterexample: {aibick}∩{aibjcj}={anbncn}\{a^ib^ic^k\} \cap \{a^ib^jc^j\} = \{a^nb^nc^n\}.
  • Complement of {anbncn}\{a^nb^nc^n\} IS context-free.
  • To prove "not CFL": intersect with a regular language to reach anbncna^nb^nc^n or wwww-type languages.
  • Compare: regular languages are closed under everything on the list.

Traps and GATE patterns

Connections

The corresponding table for regular languages is on closure properties of regular languages; the determinism question is on DPDA versus NPDA (DCFLs are closed under complement but not union); the non-context-freeness of anbncna^n b^n c^n is proved with the pumping lemma for CFLs.

PYQs

8 previous year questions from your book. I re-solved each one myself (1 by running or simulating, 7 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2023 · MSQQ11 · page 226 of the book
Which of the following statements is/are CORRECT?
  • (a)The intersection of two regular languages is regular.
  • (b)The intersection of two context-free languages is context-free.
  • (c)The intersection of two recursive languages is recursive.
  • (d). The intersection of two recursively enumerable languages is recursively enumerable.
GATE 2017 · Set 2 · MCQ · 1 markQ21 · page 228 of the book
Let L1, L2 be any two context-free languages and R be
any regular language.
Then which of the following is/are CORRECT?
I. L1 U L2is context free.
II. L̅1 is context-free
III. L1 – R is context – free.
IV. L1 ∩ L2 is context – free
  • (a)I, II and IV only
  • (b)I and III only
  • (c)II and IV only
  • (d)I only
GATE 2017 · Set 1 · MCQ · 2 marksQ23 · page 228 of the book
Consider the following languages over the alphabet Σ =
{a, b, c}. Let L1 = {anbncm | m, n > 0} and L2 =
{ambncn | m, n > 0}.
Which of the following are context-free languages?
I. (L1 ∪ L2)
II. (L1 ∩ L2)
  • (a)I only
  • (b)II only
  • (c)I and II
  • (d)Neither I nor II
GATE 2008 · MCQ · 2 marksQ37 · page 230 of the book
Consider the following languages.
L1={ai b j ck | i =j, k>=1}
L2={ai b j | j=2i , i >=0}
Which of the following is true?
  • (a)L1 is not a CFL but L2 is
  • (b)L1 ∩ L2 = φ and L1 is non –regular
  • (c)L1 U L2 is not a CFL but L2 is
  • (d)There is a 4- state PDA that accepts L1 ,but there is no DPDA that accepts L2.
GATE 2006 · MCQQ40 · page 230 of the book
Let L be a context-free language and M a regular
language. Then the language  L ∩ M is
  • (a)always regular
  • (b)never regular
  • (c)always a deterministic context-free language
  • (d)always a context-free language THEORY OF COMPUTATION
GATE 2005 · MCQQ48 · page 231 of the book
Consider the languages:
L1 = {anbncm | n, m > 0} and L2 = {anbmcm | n, m > 0},
Which one of the following statements is FALSE?
  • (a)L1 ∩ L2 is a context-free language
  • (b)L1 U L2 is a context-free language
  • (c)L1 and L2 are context-free languages
  • (d)L1 ∩ L2 is a context sensitive language THEORY OF COMPUTATION
GATE 2005 · MCQ · 1 markQ49 · page 232 of the book
Let L be a regular language and M be a context-free
language, both over the alphabet Σ . Let Lc and
Mc denote the complements of L and M respectively.
Which of the following statements about the language
Lc U Mc is TRUE?
  • (a)It is necessarily regular but not necessarily context- free.
  • (b)It is necessarily context-free.
  • (c)It is necessarily non-regular.
  • (d)None of the above
GATE 2016 · Set 2 · MCQ · 1 markQ11 · page 242 of the book
Consider the following types of languages:
L1: Regular
L2: Context-free
L3: Recursive
L4: Recursively enumerable.
Which of the following is/are TRUE?
I. L3' ∪ L4 is recursively enumerable .
II. L2' ∪ L3 is recursive .
III. L1* ∪ L2 is context-free .
IV. L1 ∪ L2' is context-free.
  • (a)I only
  • (b)I and III only
  • (c)I and IV only
  • (d)I, II and III only

Self-check

Question 1

Which of union, intersection, complement and concatenation preserve context-freeness?

Show answer

Union and concatenation.

Question 2

Write two CFLs whose intersection is {anbncn}\{a^n b^n c^n\}.

Show answer

{aibick}\{a^i b^i c^k\} and {aibjcj}\{a^i b^j c^j\}.

Question 3

LL is context-free and RR is regular. Is L∩RL \cap R context-free? Is L∖RL \setminus R?

Show answer

Yes to both (L∖R=L∩R‾L \setminus R = L \cap \overline{R} and R‾\overline{R} is regular).

Question 4

How many productions does the star construction add to GG with nn productions?

Show answer

Two: S→S1SS \to S_1 S and S→εS \to \varepsilon, giving n+2n + 2 productions.

Revision card

One-screen revision

  • Closed: ∪\cup, concatenation, star, reversal, homomorphism, inverse homomorphism, ∩\cap regular.
  • Not closed: ∩\cap, complement, difference.
  • {aibick}∩{aibjcj}={anbncn}\{a^ib^ic^k\} \cap \{a^ib^jc^j\} = \{a^nb^nc^n\}; the complement of anbncna^nb^nc^n is context-free.
  • Prove non-CFL by intersecting with a regular language.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 2.2 and Problems 2.15, 2.16 (closure under union, concatenation, star; intersection with a regular language)
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 7.3 (closure properties of context-free languages)

Verification

scripts/enum-checks/toc_cfl.py: on 150 random pairs of grammars the grammars built for union, concatenation, star, reversal and the homomorphism a -> x, b -> yy generate exactly the language the set definition gives (strings up to length 7; the homomorphic image exactly up to length 8); the product of a PDA with a DFA accepts exactly the strings accepted by both on 150 random pairs (strings up to length 6); {a^i b^i c^k} and {a^i b^j c^j} are generated by the grammars on this page (all strings up to length 12) and their intersection is {a^n b^n c^n}; De Morgan on the finite sets; the complement of {a^n b^n c^n} equals the language of the three-part grammar on this page (all strings up to length 9); the intersection of {w : #a = #b = #c} with a*b*c* is {a^n b^n c^n}. Closure under inverse homomorphism, substitution and quotient with a regular language, and the fact that the complement of {ww} is context-free, are standard textbook results, quoted and not machine-proved here (needs verification). That a^n b^n c^n is not context-free is shown on the next page. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never