The Wayback Machine - https://web.archive.org/web/20240607080327/https://www.geeksforgeeks.org/closure-properties-of-context-free-languages/
Open In App

Closure Properties of Context Free Languages

Last Updated : 28 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 
Union : If L1 and L2 are two context free languages, their union L1 ? L2 will also be context free. For example, 
L1 = { anbncm | m >= 0 and n >= 0 } and L2 = { anbmcm | n >= 0 and m >= 0 } 
L3 = L1 ? L2 = { anbncm ? anbmcm | n >= 0, m >= 0 } is also 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 union says either of two conditions to be true. So it is also context free language. 

Note: So CFL are closed under Union. 

  
Concatenation : If L1 and If L2 are two context free languages, their concatenation L1.L2 will also be context free. For 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. 

  
Kleene Closure : If L1 is context free, its Kleene closure L1* will also be context free. For example, 
L1 = { anbn | n >= 0 } 
L1* = { anbn | n >= 0 }* is also context free. 

Note :So CFL are closed under Kleen Closure. 

  
Intersection and complementation : If L1 and If L2 are two context free languages, their intersection L1 ? L2 need not be context free. For 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. 

  
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. 
  
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. 
  
This article has been contributed by Sonal Tuteja. 
  

 



Previous Article
Next Article

Similar Reads

Ambiguity in Context free Grammar and Context free Languages
Before reading this article, we recommend you to first read about Pushdown Automata and Context Free Languages. Suppose we have a context free grammar G with production rules : S -> aSb | bSa | SS | e Left Most Derivation (LMD) and Derivation Tree : Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal
5 min read
Ambiguity in Context free Grammar and Context free Languages
Prerequisite - Pushdown Automata and Context Free Languages . Suppose we have a context free grammar G with production rules: S->aSb|bSa|SS|? Left most derivation (LMD) and Derivation Tree: Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal symbol by RHS of corresponding production rule. For example
4 min read
Various Properties of context free languages (CFL)
Context-Free Language (CFL) is a language which is generated by a context-free grammar or Type 2 grammar(according to Chomsky classification) and gets accepted by a Pushdown Automata. Some very much important properties of a context-free language is: Regularity- context-free languages are Non-Regular PDA language. Closure properties : The context-f
2 min read
Closure properties of Regular languages
Closure properties on regular languages are defined as certain operations on regular language that are guaranteed to produce regular language. Closure refers to some operation on a language, resulting in a new language that is of the same "type" as originally operated on i.e., regular. Regular languages are closed under the following operations: Co
4 min read
Closure properties Table in TOC
The Below Table shows the Closure Properties of Formal Languages : REG = Regular LanguageDCFL = deterministic context-free languages, CFL = context-free languages,CSL = context-sensitive languages,RC = Recursive.RE = Recursive Enumerable Consider L and M are regular languages : The Kleene star - ∑*, is a unary operator on a set of symbols or string
3 min read
Converting Context Free Grammar to Chomsky Normal Form
Prerequisite - Simplifying Context Free Grammars A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules satisfy one of the following conditions: A non-terminal generating a terminal (e.g.; X->x) A non-terminal generating two non-terminals (e.g.; X->YZ) Start symbol generating Îĩ. (e.g.; S-> Îĩ) Consider the following gra
4 min read
Converting Context Free Grammar to Greibach Normal Form
Prerequisite - Context Free Grammars, Simplifying Context Free Grammars A context free grammar (CFG) is in Greibach Normal Form (GNF) if all production rules satisfy one of the following conditions: A non-terminal generating a terminal (e.g.; X->x)A non-terminal generating a terminal followed by any number of non-terminals (e.g.; X->xX1X2â€ĶXN)
4 min read
Check if the language is Context Free or Not
Prerequisite - Pumping Lemma, How to identify if a language is regular or not We typically face questions to identify which of the given languages are context free. In case of regular languages, it is comparatively easy to answer this, but for Context Free languages, it is tricky sometimes. Pumping Lemma provides us with ability to perform negative
4 min read
Regular Expression Vs Context Free Grammar
Regular Expressions are capable of describing the syntax of Tokens. Any syntactic construct that can be described by Regular Expression can also be described by the Context free grammar. Regular Expression: (a|b)(a|b|01) Context-free grammar: S --> aA|bA A --> aA|bA|0A|1A|e *e denotes epsilon. The Context-free grammar form NFA for the Regular
2 min read
CYK Algorithm for Context Free Grammar
Prerequisite - Converting Context Free Grammar to Chomsky Normal Form CYK algorithm is a parsing algorithm for context free grammar. In order to apply CYK algorithm to a grammar, it must be in Chomsky Normal Form. It uses a dynamic programming algorithm to tell whether a string is in the language of a grammar. Algorithm : Let w be the n length stri
2 min read
Article Tags :