The Wayback Machine - https://web.archive.org/web/20241009195111/https://www.geeksforgeeks.org/union-and-intersection-of-regular-languages-with-cfl/
Open In App

Union and Intersection of Regular languages with CFL

Last Updated : 20 Nov, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite – Chomsky Hierarchy, Regular Languages

As we all are aware that languages accepted by finite automata are called regular languages and those which are accepted by push down automata are called context free languages But, when it comes to the union or intersection of these two languages some people find it difficult to analyse whether the intersection results in a regular or a context-free language.

The first thing to observe is every regular language is actually context-free,the reason is quite simple. One way to call a language regular is by designing its equivalent finite automata or in other words if we can design a finite automata for a particular language then only we can call that language – regular and same exists for context free languages, if it is possible to design a push down automata for a particular language then only it is called context free.

Now a push down automata in simple words is actually a finite automata with a memory provided to it as infinite stack. What you need to observe that is possible to design a finite automata for a particular language then it is also possible to design its equivalent push down automata what we have to do is just not to use the infinite stack available in it. Its that simple. Observe this and finally what will you get is that for every regular language it is possible to design finite automata and thus push down automata. This is the reason why every regular language can also be called context-free or in other words regular languages are subset of context-free languages.

Union of Regular language with context free language –
As all regular languages are context-free the union of both results in a context-free language. But it is always good to understand with the help of an example.
Let’s take a language L1 = {0*1*} (regular) and L2 = {0^n1^n |n>=0} (context-free)
And let L=L1 U L2 which will result in union of both these languages and that will be :

L = {0*1*} which is regular language but since every regular language is context-free. So, we can say the union of two always results in context-free language.

Intersection of Regular language with context-free language –
Because now we know that all regular languages are subset of context-free there is no problem in understanding the union of two but when we talk about intersection again the answer is context-free language. Yes, the intersection of a regular and a context-free language always result in a context-free language.

Let’s again take the above example in which L1 and L2 are same but now let
L= L1 ? L2 which will easily result in
L={0^n1^n | n>=0} which is context-free .
You can take more such examples and verify that the union and intersection of a regular language and a context-free language always results in a context-free language.

Read Next article – Closure Properties of Context Free Languages


Previous Article
Next Article

Similar Reads

Regular Expressions, Regular Grammar and Regular Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most restricted types of languages and are accepted by finite automata. Regular Expressions Regular Expressions are used to denote regular languages. An expression is regular if: ? is a regular expression for regular language ?. ? is a regular expression for regular language {?}. If a ? ?
7 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
Theory of Computation | Regular languages and finite automata | Question 2
What is the complement of the language accepted by the NFA shown below? (A) A (B) B (C) C (D) D Answer: (B) Explanation: Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 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
Star Height of Regular Expression and Regular Language
The star height relates to the field of theoretical of computation (TOC). It is used to indicate the structural complexity of regular expressions and regular languages. Here complexity, relates to the maximum nesting depth of Kleene stars present in a regular expression. It may be noted here that a regular language may be represented by regular exp
6 min read
Regular grammar (Model regular grammars )
Prerequisites: Chomsky hierarchy Type-3 grammar/regular grammar: Regular grammar generates regular language. They have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a non-terminal. The productions must be in the form: A ⇢ xB A ⇢ x A ⇢ Bx where A, B ∈ Variable(V) and
5 min read
Recursive and Recursive Enumerable Languages in TOC
Recursive Enumerable (RE) or Type -0 Language RE languages or type-0 languages are generated by type-0 grammars. An RE language can be accepted or recognized by Turing machine which means it will enter into final state for the strings of language and may or may not enter into rejecting state for the strings which are not part of the language. It me
5 min read
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
Principle of programming languages | Set 1
Following questions have been asked in GATE CS exam. 1.The most appropriate matching for the following pairs X: Indirect addressing 1: Loops Y: Immediate addressing 2: Pointers Z: Auto decrement addressing 3. Constants is (GATE CS 2000) (a) X—3 Y—2 Z—1 (b) X—1 Y—3 Z—2 (c) X—2 Y—3 Z—1 (d) X—3 Y—1 Z—2 Answer (c) Reference: http://en.wikipedia.org/wik
4 min read
Construct Pushdown Automata for given languages
Prerequisite - Pushdown Automata, Pushdown Automata Acceptance by Final State A push down automata is similar to deterministic finite automata except that it has a few more properties than a DFA.The data structure used for implementing a PDA is stack. A PDA has an output associated with every input. All the inputs are either pushed into a stack or
4 min read
Closure Properties of Context Free Languages
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 fr
9 min read
Intersection process of two DFAs
Prerequisite - Design a Finite automata Let's understand the intersection of two DFA with an example. Designing a DFA for the set of string over {0, 1} such that it ends with 01 and has even number of 1's. There two desired language will be formed:   L1= {01, 001, 101, 0101, 1001, 1101, ....} L2= {11, 011, 101, 110, 0011, 1100, .....}L = L1 and L2
1 min read
Difference between JOIN and UNION in SQL
Pre-requisites: JOIN, UNION JOIN in SQL is used to combine data from many tables based on a matched condition between them. The data combined using the JOIN statement results in new columns. Consider the two tables: Boys Girls Example: sql> SELECT Boys.Name, Boys.Age, Girls.Address, FROM Boys INNER JOIN Girls ON Boys.Rollno = Girls.Rollno; The r
2 min read
Union process in DFA
Prerequisite - Designing finite automata To perform the union operation on two deterministic finite automata (DFAs), the following steps can be taken: Create a new DFA with a new set of states, consisting of all the states from both original DFAs.Define the initial state of the new DFA to be the tuple (q1, q2), where q1 and q2 are the initial state
5 min read
∈-NFA of Regular Language L = (0+1)*(00 + 11) and L = b + ba*
Non-Deterministic Finite Automata and ∈-Non-Deterministic Finite Automata are almost the same except for their transition function and there are a few special rules for construction of ∈-NFA. ∈-NFA is defined in 5 tuple representation {Q, q0, Σ, δ, F} where - Q is the set of all states, q0 is the initial state, Σ is the set of input symbols, δ is t
3 min read
∈-NFA of Regular Language L = 0(0+1)*1 and L = (00)*1(11)*
Non-Deterministic Finite Automata and ∈-Non-Deterministic Finite Automata are almost the same except for their transition function and there are a few special rules for construction of ∈-NFA. ∈-NFA is defined in 5 tuple representation {Q, q0, Σ, δ, F} where - Q is the set of all states, q0 is the initial state, Σ is the set of input symbols, δ is t
3 min read
Right and Left linear Regular Grammars
Regular grammar is a type of grammar that describes a regular language. A regular grammar is a mathematical object, G, which consists of four components, G = (N, E, P, S), where N: non-empty, finite set of non-terminal symbols,E: a finite set of terminal symbols, or alphabet, symbols,P: a set of grammar rules, each of one having one of the formsA ⇢
2 min read
Difference between Context Free Grammar and Regular Grammar
Noam Chomsky has divided grammar into four types : Type Name 0 Unrestricted Grammar1 Context Sensitive Grammar2 Context Free Grammar3 Regular Grammar 1. Context Free Grammar : Language generated by Context Free Grammar is accepted by Pushdown AutomataIt is a subset of Type 0 and Type 1 grammar and a superset of Type 3 grammar.Also called phase stru
2 min read
Generating regular expression from Finite Automata
Prerequisite - Introduction of FA, Regular expressions, grammar and language, Designing FA from Regular Expression There are two methods to convert FA to the regular expression: 1. State Elimination Method:Step 1 - If the start state is an accepting state or has transitions in, add a new non-accepting start state and add an €-transition between the
3 min read
How to identify if a language is regular or not
Prerequisite - Regular Expressions, Regular Grammar and Regular Languages, Pumping Lemma There is a well established theorem to identify if a language is regular or not, based on Pigeon Hole Principle, called as Pumping Lemma. But pumping lemma is a negativity test, i.e. if a language doesn't satisfy pumping lemma, then we can definitely say that i
8 min read
Designing Finite Automata from Regular Expression (Set 6)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 5) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = a(a+b)* The language of the given RE is, {aaa, aba, baa, bba} Strings
3 min read
Designing Finite Automata from Regular Expression (Set 2)
Prerequisite: Finite automata, Regular expressions, grammar and language. Designing finite automata from Regular expression In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Φ (Phi). The language of the given RE is L1 = {} i.e, empty string. Its finite automata will be
3 min read
Designing Finite Automata from Regular Expression (Set 3)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 2) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression. Regular Expression 1: 'ab*' ('a' followed by any number of 'b'). The language of the given RE is, L1 = {a, ab, abb
3 min read
Designing Finite Automata from Regular Expression (Set 4)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 3) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = (a+b)(a+b) The language of the given RE is, {aa, ab, ba, bb} Length o
3 min read
Designing Finite Automata from Regular Expression (Set 5)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 4) In the below article, we shall see some Designing of Non-deterministic Finite Automata form the given Regular Expression- As NFA can be changed to corresponding DFA. Regular Expression 1: Regular language, L1 = ((a+b)(
3 min read
Regular expression to ∈-NFA
Prerequisite - Finite Automata Introduction, Designing Finite Automata from Regular Expression (Set 1) ∈-NFA is similar to the NFA but have minor difference by epsilon move. This automaton replaces the transition function with the one that allows the empty string ∈ as a possible input. The transitions without consuming an input symbol are called ∈-
2 min read
Designing Finite Automata from Regular Expression (Set 7)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 6) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = b*aa(a+b)*+b*ab*aa(a+b*) The language of the given RE is- {aaa, baa,
3 min read
Designing Finite Automata from Regular Expression (Set 8)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 7) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = {an | n≥ 1} The language of the given RE is- {a, aa, aaa, ........
2 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