Closure Properties of Context Free Languages
Last Updated :
23 Jul, 2025
Context-Free Languages (CFLs) are an essential class of languages in the field of automata theory and formal languages. They are generated by context-free grammars (CFGs) and are recognized by pushdown automata (PDAs). Understanding the closure properties of CFLs helps in determining which operations preserve the context-free nature of a language.
CFLs are closed under some operations, meaning that applying these operations to CFLs will always result in another CFL. However, they are not closed under certain operations, which means that performing these operations on CFLs might result in a language that is not context-free.
Context Free Languages (CFLs) are accepted by pushdown automata. Context free languages can be generated by context free grammars, which have productions (substitution rules) of the form :
A -> ? (where A ? N and ? ? (T ? N)* and N is a non-terminal and T is a terminal)
Properties of Context Free Languages
CFLs are closed under the following operations:
1. Union
If L and M are two context-free languages, then their union L ⊠M is also a CFL.
Construction:
- Consider two context-free grammars, G and H, for L and M respectively.
- Assume that G and H have no common variables (this can be ensured by renaming variables if needed).
- Introduce a new start symbol S and add the rule: S â Sâ | Sâ Here, Sâ and Sâ are the start symbols of G and H, respectively.
- The resulting grammar generates L ⊠M, proving that CFLs are closed under union.
Example: Let Lâ = { aâŋbâŋcáĩ | m âĨ 0, n âĨ 0 } and Lâ = { aâŋbáĩcáĩ | n âĨ 0, m âĨ 0 }.
- Lâ enforces that the number of aâs equals the number of bâs.
- Lâ enforces that the number of bâs equals the number of câs.
- Their union states that either of these conditions must be satisfied, making the resulting language context-free.
Note: So CFL are closed under Union.
2. Concatenation
If L and M are CFLs, then their concatenation LM is also a CFL.
Construction:
- Let G and H be the context-free grammars for L and M, respectively.
- Assume that G and H have no common variables.
- Introduce a new start symbol S and add the production:S â Sâ SâHere, Sâ and Sâ are the start symbols of G and H, respectively.
- This ensures that any derivation from S will first generate a string in L and then a string in M, proving that CFLs are closed under concatenation.
Example
L1 = { anbn | n >= 0 } and L2 = { cmdm | m >= 0 }
L3 = L1.L2 = { anbncmdm | m >= 0 and n >= 0} is also context free.
L1 says number of aâs should be equal to number of bâs and L2 says number of câs should be equal to number of dâs. Their concatenation says first number of aâs should be equal to number of bâs, then number of câs should be equal to number of dâs. So, we can create a PDA which will first push for aâs, pop for bâs, push for câs then pop for dâs. So it can be accepted by pushdown automata, hence context free.
Note: So CFL are closed under Concatenation.
3. Kleene Closure
If L is a CFL, then its Kleene closure L* (zero or more repetitions of strings in L) is also a CFL.
Construction:
- Let G be the context-free grammar for L with start symbol Sâ.
- Introduce a new start symbol S and add the rule:S â Sâ S | ÎĩThis rule ensures that S can derive zero or more copies of Sâ, proving closure under the Kleene star.
Example
L1 = { anbn | n >= 0 }
L1* = { anbn | n >= 0 }* is also context free.
Note :So CFL are closed under Kleen Closure.
4. Intersection and complementation
If L1 and If L2 are two context free languages, their intersection L1 ? L2 need not be context free.
Example
L1 = { anbncm | n >= 0 and m >= 0 } and L2 = (ambncn | n >= 0 and m >= 0 }
L3 = L1 ? L2 = { anbncn | n >= 0 } need not be context free.
L1 says number of aâs should be equal to number of bâs and L2 says number of bâs should be equal to number of câs. Their intersection says both conditions need to be true, but push down automata can compare only two. So it cannot be accepted by pushdown automata, hence not context free.
Similarly, complementation of context free language L1 which is ?* - L1, need not be context free.
Note : So CFL are not closed under Intersection and Complementation.
5. Reversal
If L is a CFL, then its reversal L^R (where each string is reversed) is also a CFL.
Construction:
- Take the context-free grammar G for L.
- Modify each production so that the right-hand side of every rule is reversed.
- The resulting grammar generates L^R, proving that CFLs are closed under reversal.
Example:
- Original Grammar: S â 0S1 | 01
- Reversed Grammar: S â 1S0 | 10
6. Homomorphism
A homomorphism is a function that replaces each symbol in a string with another string.
If L is a CFL and h is a homomorphism, then h(L) is also a CFL.
Example:
- Let G have the production S â 0S1 | 01.
- Define a homomorphism h(0) = ab, h(1) = Îĩ.
- Then, h(L(G)) will be generated by a new grammar with productions:S â abS | ab
This shows that CFLs are closed under homomorphism.
7. Inverse Homomorphism
If L is a CFL and h is a homomorphism, then hâŧÂđ(L) is also a CFL.
Construction:
- Instead of using a grammar, we use a PDA to construct the inverse homomorphism.
- The PDA for hâŧÂđ(L) simulates the PDA for L but keeps track of the mapping of symbols.
Deterministic Context-free Languages
Deterministic CFL are subset of CFL which can be recognized by Deterministic PDA. Deterministic PDA has only one move from a given state and input symbol, i.e., it do not have choice. For a language to be DCFL it should be clear when to PUSh or POP.
For example, L1= { anbncm | m >= 0 and n >= 0} is a DCFL because for aâs, we can push on stack and for bâs we can pop. It can be recognized by Deterministic PDA. On the other hand, L3 = { anbncm ? anbmcm | n >= 0, m >= 0 } cannot be recognized by DPDA because either number of aâs and bâs can be equal or either number of bâs and câs can be equal. So, it can only be implemented by NPDA. Thus, it is CFL but not DCFL.
Note : DCFL are closed only under complementation and Inverse Homomorphism.
Multiple-choice questions (MCQs)
Question : Consider the language L1,L2,L3 as given below.
L1 = { ambn | m, n >= 0 }
L2 = { anbn | n >= 0 }
L3 = { anbncn | n >= 0 }
Which of the following statements is NOT TRUE?
A. Push Down Automata (PDA) can be used to recognize L1 and L2
B. L1 is a regular language
C. All the three languages are context free
D. Turing machine can be used to recognize all the three languages
Solution : Option (A) says PDA can be used to recognize L1 and L2. L1 contains all strings with any no. of a followed by any no. of b. So, it can be accepted by PDA. L2 contains strings with n no. of aâs followed by n no. of bâs. It can also be accepted by PDA. So, option (A) is correct.
Option (B) says that L1 is regular. It is true as regular expression for L1 is a*b*.
Option (C) says L1, L2 and L3 are context free. L3 languages contains all strings with n no. of aâs followed by n no. of bâs followed by n no. of câs. But it canât be accepted by PDA. So option ( C) is not correct.
Option (D) is correct as Turing machine can be used to recognize all the three languages.
Question : The language L = { 0i12i | i ? 0 } over the alphabet {0, 1, 2} is :
A. Not recursive
B. Is recursive and deterministic CFL
C. Is regular
D. Is CFL bot not deterministic CFL.
Solution : The above language is deterministic CFL as for 0âs, we can push 0 on stack and for 2âs we can pop corresponding 0âs. As there is no ambiguity which moves to take, it is deterministic. So, correct option is (B). As CFL is subset of recursive, it is recursive as well.
Question : Consider the following languages:
L1 = { 0n1n| n?0 }
L2 = { wcwr | w ? {a,b}* }
L3 = { wwr | w ? {a,b}* }
Which of these languages are deterministic context-free languages?
A. None of the languages
B. Only L1
C. Only L1 and L2
D. All three languages
Solution : Languages L1 contains all strings in which n 0âs are followed by n 1âs. Deterministic PDA can be constructed to accept L1. For 0âs we can push it on stack and for 1âs, we can pop from stack. Hence, it is DCFL.
L2 contains all strings of form wcwr where w is a string of aâs and bâs and wr is reverse of w. For example, aabbcbbaa. To accept this language, we can construct PDA which will push all symbols on stack before c. After c, if symbol on input string matches with symbol on stack, it is popped. So, L2 can also be accepted with deterministic PDA, hence it is also DCFL.
L3 contains all strings of form wwr where w is a string of aâs and bâs and wr is reverse of w. But we donât know where w ends and wr starts. e.g.; aabbaa is a string corresponding to L3. For first a, we will push it on stack. Next a can be either part of w or wr where w=a. So, there can be multiple moves from a state on an input symbol. So, only non-deterministic PDA can be used to accept this type of language. Hence, it is NCFL not DCFL.
So, correct option is (C). Only, L1 and L2 are DCFL.
Question : Which one of the following grammars generate the language L = { aibj | i ? j }
S -> AC | CB, C -> aCb | a | b, A -> aA | ?, B -> Bb | ?
S -> aS | Sb | a | b
S -> AC | CB, C -> aCb | ?, A -> aA | ?, B -> Bb | ?
S -> AC | CB, C -> aCb | ?, A -> aA | a, B -> Bb | b
Solution : The best way to solve these type of questions is to eliminate options which do not satisfy conditions. The conditions for language L is no. of aâs and no. of bâs should be unequal.
In option (B), S => aS => ab. It can generate strings with equal aâs and bâs. So, this option is incorrect.
In option (C), S => AC => C => ?. In ?, aâs and bâs are equal (0), so it is not correct option.
In option (A), S will be replaced by either AC or CB. C will either generate no. of aâs more than no. of bâs by 1 or no. of bâs more than no. of aâs by 1. But one more a or one more b can be compensated by B -> bB | ? or A -> aA | ? respectively. So it may give strings with equal no. of aâs and bâs. So, it is not a correct option.
In option (D), S will be replaced by either AC or CB. C will always generate equal no. of aâs and bâs. If we replace S by AC, A with add atleast one extra a. and if we replace S by CB, B will add atleast one extra b. So this grammar will never generate equal no. of aâs and bâs.
So, option (D) is correct.
Explore
Automata _ Introduction
Regular Expression and Finite Automata
CFG
PDA (Pushdown Automata)
Introduction of Pushdown Automata
5 min read
Pushdown Automata Acceptance by Final State
4 min read
Construct Pushdown Automata for given languages
4 min read
Construct Pushdown Automata for all length palindrome
6 min read
Detailed Study of PushDown Automata
3 min read
NPDA for accepting the language L = {anbm cn | m,n>=1}
2 min read
NPDA for accepting the language L = {an bn cm | m,n>=1}
2 min read
NPDA for accepting the language L = {anbn | n>=1}
2 min read
NPDA for accepting the language L = {amb2m| m>=1}
2 min read
NPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1}
2 min read
Construct Pushdown automata for L = {0n1m2m3n | m,n ⥠0}
3 min read
Construct Pushdown automata for L = {0n1m2n+m | m, n ⥠0}
2 min read
NPDA for accepting the language L = {ambncm+n | m,n ⥠1}
2 min read
NPDA for accepting the language L = {amb(m+n)cn| m,n ⥠1}
3 min read
NPDA for accepting the language L = {a2mb3m|m>=1}
2 min read
NPDA for accepting the language L = {amb2m+1 | m ⥠1}
2 min read
NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}
3 min read
Construct Pushdown automata for L = {a2mc4ndnbm | m,n ⥠0}
3 min read
NPDA for L = {0i1j2k | i==j or j==k ; i , j , k >= 1}
2 min read
NPDA for accepting the language L = {anb2n| n>=1} U {anbn| n>=1}
2 min read
NPDA for the language L ={wÐ{a,b}* | w contains equal no. of a's and b's}
3 min read
Turing Machine
Decidability
TOC Interview preparation
TOC Quiz and PYQ's in TOC