The Wayback Machine - https://web.archive.org/web/20240607080327/https://www.geeksforgeeks.org/check-if-the-language-is-context-free-or-not/
Open In App

Check if the language is Context Free or Not

Last Updated : 10 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report
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 test, i.e. if a language doesn’t satisfy pumping lemma, then we can definitely say that it is not context free, but if it satisfies, then the language may or may not be context free. Pumping Lemma is more of a mathematical proof, takes more time and to apply it on context free languages is a tedious task and finding out counter example for complex language expressions is not much handful. We can address this problem very quickly, based on common observations and analysis:
  1. Every regular language is context free. Example – { a^m b^l c^k d^n | m, l, k, n >= 1 } is context free, as it is regular too.
  2. Given an expression such that it is possible to obtain a center or mid point in the strings, so we can carry out comparison of left and right sub-parts using stack. Example 1 – L = {a^n b^n | n >= 1} is context free, as we can push a’s and then we can pop a’s for each occurrence of b. Example 2 – L = { a^m b^n c^{(m+n)}} is context free. We can rewrite it as { a^m b^n c^n c^m }. Example 3 – L = { a^n b^{(2n)} } is context free, as we can push two a’s and pop an a for each occurrence of b. Hence, we get a mid-point here as well. Example 4 – L = { a^n b^n c^n } is not context free.
  3. Given expression is a combination of multiple expressions with mid-points in them, such that each sub-expression is independent of other sub-expressions, then it is context free. Example 1 – L = { a^m b^m c^n d^n } is context free. It contains multiple expressions with a mid-point in each of them. Example 2 – L = { a^m b^n c^m d^n } is not context free.
  4. Given expression consists of an operation where mid-point could be found along with some independent regular expressions in between, results into context free language. Example – L = { a^m b^i c^m d^k } is a context free language. Here, we have b^i and d^k as independent regular expressions in between, which doesn’t affect the stack.
  5. An expression that doesn’t form a pattern on which linear comparison could be carried out using stack is not context free language. Example 1 – L = { a^m b^n^2 } is not context free. Example 2 – L = { a^n b^2^n } is not context free. Example 3 – L = { a^n^2 } is not context free. Example 4 – L = { a^m | m is prime } is not context free.
  6. An expression that involves counting and comparison of three or more variables independently is not context free language, as stack allows comparison of only two variables at a time. Example 1 – L = { a^n b^n c^n } is not context free. Example 2 – L = { w | na(w) = nb(w) = nc(w) } is not context free. Example 3 – L = { a^i b^j c^k | i > j > k } is not context free.
  7. A point to remember is counting and comparison could only be done with the top of stack and not with bottom of stack in Push Down Automata, hence a language exhibiting a characteristic that involves comparison with bottom of stack is not a context free language. Example 1 – L = { a^m b^n c^m d^n } is not context free. Pushing a’s first then b’s. Now, we will not be able to compare c’s with a’s as the top of the stack has b’s. Example 2 – L = { WW | W belongs to {a, b}* } is not context free. One might think to draw a non-deterministic push down automaton, but it will not help as first symbol will be at bottom of the stack and when the second W starts, we will not be able to compare it with the bottom of stack.
  8. If we can find mid-point in the expression even in a non-deterministic way, then it is context free language. Example 1 – L = { W W^r | W belongs to {a, b}* } is context free language. Example 2 – L = { a^i b^j c^k d^l | i=k or j=l } is a context free language.


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
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 described by CSG but not by context-free grammars
2 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
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
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
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
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[caption width="800"]Chomsky Hierarchy[/caption] 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 s
2 min read
What is Context-Free Grammar?
Context Free Grammar is formal grammar, the syntax or structure of a formal language can be described using context-free grammar (CFG), a type of formal grammar. The grammar has four tuples: (V,T,P,S). V - It is the collection of variables or nonterminal symbols.T - It is a set of terminals. P - It is the production rules that consist of both termi
2 min read
Article Tags :