Closure Properties of Context Free Languages
Last Updated :
05 Feb, 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.
Similar Reads
Introduction to Theory of Computation Automata theory, also known as the Theory of Computation, is a field within computer science and mathematics that focuses on studying abstract machines to understand the capabilities and limitations of computation by analyzing mathematical models of how machines can perform calculations.Why we study
7 min read
TOC Basics
Regular Expressions & Finite Automata
Introduction of Finite AutomataFinite automata are abstract machines used to recognize patterns in input sequences, forming the basis for understanding regular languages in computer science. They consist of states, transitions, and input symbols, processing each symbol step-by-step. If the machine ends in an accepting state after
4 min read
Regular Expressions, Regular Grammar and Regular LanguagesTo work with formal languages and string patterns, it is essential to understand regular expressions, regular grammar, and regular languages. These concepts form the foundation of automata theory, compiler design, and text processing.Regular ExpressionsRegular expressions are symbolic notations used
7 min read
Arden's Theorem in Theory of ComputationA Regular Expression (RE) is a way to describe patterns of strings using symbols and operators like union, concatenation, and star. A Deterministic Finite Automaton (DFA) is a machine that reads input strings and decides if they match the pattern by moving through a set of defined states without any
6 min read
Conversion from NFA to DFAAn NFA can have zero, one or more than one move from a given state on a given input symbol. An NFA can also have NULL moves (moves without input symbol). On the other hand, DFA has one and only one move from a given state on a given input symbol. Steps for converting NFA to DFA:Step 1: Convert the g
5 min read
Minimization of DFADFA minimization stands for converting a given DFA to its equivalent DFA with minimum number of states. DFA minimization is also called as Optimization of DFA and uses partitioning algorithm.Minimization of DFA Suppose there is a DFA D < Q, Î, q0, Î, F > which recognizes a language L. Then the
7 min read
Reversing Deterministic Finite AutomataPrerequisite â Designing finite automata Reversal: We define the reversed language L^R \text{ of } L  to be the language L^R = \{ w^R \mid w \in L \} , where w^R := a_n a_{n-1} \dots a_1 a_0 \text{ for } w = a_0 a_1 \dots a_{n-1} a_n Steps to Reversal: Draw the states as it is.Add a new single accep
4 min read
Mealy and Moore Machines in TOCMoore and Mealy Machines are Transducers that help in producing outputs based on the input of the current state or previous state. In this article we are going to discuss Moore Machines and Mealy Machines, the difference between these two machinesas well as Conversion from Moore to Mealy and Convers
3 min read
CFG & PDA
Simplifying Context Free GrammarsA Context-Free Grammar (CFG) is a formal grammar that consists of a set of production rules used to generate strings in a language. However, many grammars contain redundant rules, unreachable symbols, or unnecessary complexities. Simplifying a CFG helps in reducing its size while preserving the gene
6 min read
Converting Context Free Grammar to Chomsky Normal FormChomsky Normal Form (CNF) is a way to simplify context-free grammars (CFGs) so that all production rules follow specific patterns. In CNF, each rule either produces two non-terminal symbols, or a single terminal symbol, or, in some cases, the empty string. Converting a CFG to CNF is an important ste
5 min read
Closure Properties of Context Free LanguagesContext-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 operation
11 min read
Pumping Lemma in Theory of ComputationThere are two Pumping Lemmas, which are defined for 1. Regular Languages, and 2. Context - Free Languages Pumping Lemma for Regular Languages For any regular language L, there exists an integer n, such that for all x ? L with |x| ? n, there exists u, v, w ? ?*, such that x = uvw, and (1) |uv| ? n (2
4 min read
Ambiguity in Context free Grammar and LanguagesContext-Free Grammars (CFGs) are essential in formal language theory and play a crucial role in programming language design, compiler construction, and automata theory. One key challenge in CFGs is ambiguity, which can lead to multiple derivations for the same string.Understanding Derivation in Cont
3 min read
Context-sensitive Grammar (CSG) and Language (CSL)Context-Sensitive Grammar - A Context-sensitive grammar is an Unrestricted grammar in which all the productions are of form - Where α and β are strings of non-terminals and terminals. Context-sensitive grammars are more powerful than context-free grammars because there are some languages that can be
2 min read
Introduction of Pushdown AutomataWe have already discussed finite automata. But finite automata can be used to accept only regular languages. Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown automata to recognize Context Free Languages. This article describes pushdown automata in detail.Pus
5 min read
Turing Machine & Decidability
Problems on Finite Automata
DFA for Strings not ending with "THE"Problem - Accept Strings that not ending with substring "THE". Check if a given string is ending with "the" or not. The different forms of "the" which are avoided in the end of the string are: "THE", "ThE", "THe", "tHE", "thE", "The", "tHe" and "the" All those strings that are ending with any of the
12 min read
DFA of a string with at least two 0âs and at least two 1âsProblem - Draw deterministic finite automata (DFA) of a string with at least two 0âs and at least two 1âs. The first thing that come to mind after reading this question us that we count the number of 1's and 0's. Thereafter if they both are at least 2 the string is accepted else not accepted. But we
3 min read
DFA for accepting the language L = { anbm | n+m =even }ProblemDesign a deterministic finite automata(DFA) for accepting the language L = {an bm | n+m = even}Examples:Input: a a b b , n = 2, m = 2 2 + 2 = 4 (even)Output: ACCEPTEDInput: a a a b b b b ,n = 3, m = 43 + 4 = 7 (odd) Output: NOT ACCEPTEDInput: a a a b b b , n = 3, m = 33 + 3 = 6 (even)Output:
14 min read
DFA machines accepting odd number of 0âs or/and even number of 1âsPrerequisite - Designing finite automata Problem - Construct a DFA machine over input alphabet \sum_= {0, 1}, that accepts: Odd number of 0âs or even number of 1âs Odd number of 0âs and even number of 1âs Either odd number of 0âs or even number of 1âs but not the both together Solution - Let first d
3 min read
DFA of a string in which 2nd symbol from RHS is 'a'Draw deterministic finite automata (DFA) of the language containing the set of all strings over {a, b} in which 2nd symbol from RHS is 'a'. The strings in which 2nd last symbol is "a" are: aa, ab, aab, aaa, aabbaa, bbbab etc Input/Output INPUT : baba OUTPUT: NOT ACCEPTED INPUT: aaab OUTPUT: ACCEPTED
10 min read
Problems on PDA
Construct Pushdown Automata for all length palindromeA Pushdown Automata (PDA) is like an epsilon Non deterministic Finite Automata (NFA) with infinite stack. PDA is a way to implement context free languages. Hence, it is important to learn, how to draw PDA. Here, take the example of odd length palindrome:Que-1: Construct a PDA for language L = {wcw'
6 min read
Construct Pushdown automata for L = {0n1m2m3n | m,n ⥠0}Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Pushdown automata (PDA) plays a significant role in compiler design. Therefore there is a need to have a good hands on PDA. Our aim is to construct a PDA for L = {0n1m2m3n | m,n ⥠0} Examples - Input : 00011112222333 Outpu
3 min read
Construct Pushdown automata for L = {a2mc4ndnbm | m,n ⥠0}Pushdown Automata plays a very important role in task of compiler designing. That is why there is a need to have a good practice on PDA. Our objective is to construct a PDA for L = {a2mc4ndn bm | m,n ⥠0} Example:Input: aaccccdbOutput: AcceptedInput: aaaaccccccccddbbOutput: AcceptedInput: acccddbOut
3 min read
NPDA for accepting the language L = {anbn | n>=1}Prerequisite: Basic knowledge of pushdown automata.Problem :Design a non deterministic PDA for accepting the language L = {an bn | n>=1}, i.e.,L = {ab, aabb, aaabbb, aaaabbbb, ......} In each of the string, the number of a's are followed by equal number of b's. ExplanationHere, we need to maintai
2 min read
NPDA for accepting the language L = {ambncm+n | m,n ⥠1}The problem below require basic knowledge of Pushdown Automata.Problem Design a non deterministic PDA for accepting the language L = {am bn cm+n | m,n ⥠1} for eg. ,L = {abcc, aabccc, abbbcccc, aaabbccccc, ......} In each of the string, the total sum of the number of 'aâ and 'b' is equal to the numb
2 min read
NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem - Design a non deterministic PDA for accepting the language L = {a^i b^j c^k d^l : i==k or j==l, i>=1, j>=1}, i.e., L = {abcd, aabccd, aaabcccd, abbcdd, aabbccdd, aabbbccddd, ......} In each string, the numbe
3 min read
NPDA for accepting the language L = {anb2n| n>=1} U {anbn| n>=1}To understand this question, you should first be familiar with pushdown automata and their final state acceptance mechanism.ProblemDesign a non deterministic PDA for accepting the language L = {an b2n : n>=1} U {an bn : n>=1}, i.e.,L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......} U {ab, aabb
2 min read
Problems on Turing Machines
Turing Machine for additionPrerequisite - Turing Machine A number is represented in binary format in different finite automata. For example, 5 is represented as 101. However, in the case of addition using a Turing machine, unary format is followed. In unary format, a number is represented by either all ones or all zeroes. For
3 min read
Turing machine for multiplicationPrerequisite - Turing Machine Problem: Draw a turing machine which multiply two numbers. Example: Steps: Step-1. First ignore 0's, C and go to right & then if B found convert it into C and go to left. Step-2. Then ignore 0's and go left & then convert C into C and go right. Step-3. Then conv
2 min read
Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}The language L = {wwres | w â {0, 1}} represents a kind of language where you use only 2 character, i.e., 0 and 1. The first part of language can be any string of 0 and 1. The second part is the reverse of the first part. Combining both these parts a string will be formed. Any such string that falls
5 min read
Construct a Turing Machine for language L = {ww | w ∈ {0,1}}Prerequisite - Turing Machine The language L = {ww | w â {0, 1}} tells that every string of 0's and 1's which is followed by itself falls under this language. The logic for solving this problem can be divided into 2 parts: Finding the mid point of the string After we have found the mid point we matc
7 min read
Construct Turing machine for L = {an bm a(n+m) | n,mâ¥1}Problem : L = { anbma(n +m) | n , m ⥠1} represents a kind of language where we use only 2 character, i.e., a and b. The first part of language can be any number of "a" (at least 1). The second part be any number of "b" (at least 1). The third part of language is a number of "a" whose count is sum o
3 min read
Construct a Turing machine for L = {aibjck | i*j = k; i, j, k ⥠1}Prerequisite â Turing Machine In a given language, L = {aibjck | i*j = k; i, j, k ⥠1}, where every string of 'a', 'b' and 'c' has a certain number of a's, then a certain number of b's and then a certain number of c's. The condition is that each of these 3 symbols should occur at least once. 'a' and
2 min read
Turing machine for 1's and 2âs complementProblem-1:Draw a Turing machine to find 1's complement of a binary number. 1âs complement of a binary number is another binary number obtained by toggling all bits in it, i.e., transforming the 0 bit to 1 and the 1 bit to 0. Example:1's ComplementApproach:Scanning input string from left to rightConv
3 min read
Practice