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):
| operation | closed? | how |
|---|---|---|
| union | yes | grammars: |
| concatenation | yes | grammars: |
| star | yes | grammars: |
| reversal | yes | write every right side backwards |
| homomorphism | yes | replace each terminal by its image in the grammar |
| inverse homomorphism | yes (quoted) | PDA that applies to the input symbol by symbol |
| intersection with a regular language | yes | PDA DFA product |
| intersection | no | |
| complement | no | De Morgan: closed under union and complement would give closed under intersection |
| difference | no |
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 : (the language ) and : (the language ) the constructions give (renaming the variables to and adding a new start symbol ):
Closure constructions for context-free languages: start
Computed by a script, not typed| operation | result | size | construction |
|---|---|---|---|
| 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.
Each result was checked against the set definition of the operation on all strings up to length (and on random pairs of grammars).
Why the constructions work. Union: any derivation starts with or and then continues inside one grammar; concatenation: a string of is a string of followed by one of ; star: ; reversal: a parse tree turned over is a parse tree of the reversed grammar; homomorphism: replacing a leaf by the string .
Intersection with a regular language
Run a PDA for and a DFA for side by side: the states are pairs, the stack is '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 (4 states) and a DFA for "length divisible by " (4 states) give a product with states (13 reachable) that accepts . This is the only kind of intersection that is safe: with a second context-free language there are two stacks.
Intersection and complement fail
is context-free (grammar , , ), and so is (grammar , , ). Their intersection is
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 and the intersection is ). If CFLs were closed under complement, then would be context-free (closed under union): a contradiction. So they are not closed under complement either, and not under difference (take ).
The complement of a non-CFL can be a CFL
The language is not context-free but its complement is. A string is outside the language if (1) it is not of the form (it contains , or ): a regular condition; or (2) it is with ; or (3) with . Each of the three is context-free:
The union is the complement of (compared with the definition on all strings up to length ). 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 is context-free then so is for every regular . To show that is not context-free find a regular so that is known not to be.
- . Then is not context-free, so is not.
- : intersect with ; the result is , 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, , , , with regular: all context-free. , : not necessarily.
EX 2Count the productions
Easyhas productions, has . The union grammar has , the concatenation , the star (one of them and one , plus the of ).
EX 3Is the intersection context-free
Medium= : yes (regular intersection). = , regular and context-free. = : 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
MediumLet and , which is regular. . If were context-free, so would be ; it is not; therefore 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 is context-free and regular, is:" context-free. (c) "If and are CFLs then is a CFL." False in general (counterexample above). (d) "The language is the intersection of two CFLs:" true. (e) "Is context-free?" Yes (quoted textbook fact), while is not. (f) "Is context-free for CFLs?" Not necessarily; but is for regular .
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: .
- Complement of IS context-free.
- To prove "not CFL": intersect with a regular language to reach or -type languages.
- Compare: regular languages are closed under everything on the list.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
No cross-subject links yet.
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 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
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.
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
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
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.
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
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
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
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 answerHide
Union and concatenation.
Question 2
Write two CFLs whose intersection is .
Show answerHide
and .
Question 3
is context-free and is regular. Is context-free? Is ?
Show answerHide
Yes to both ( and is regular).
Question 4
How many productions does the star construction add to with productions?
Show answerHide
Two: and , giving productions.
Revision card
One-screen revision
- Closed: , concatenation, star, reversal, homomorphism, inverse homomorphism, regular.
- Not closed: , complement, difference.
- ; the complement of 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
My notes (saved in this browser)