# Algorithm to test whether a language is context-free

Is there an algorithm/systematic procedure to test whether a language is context-free?

In other words, given a language specified in algebraic form (think of something like $L=\{a^n b^n a^n : n \in \mathbb{N}\}$), test whether the language is context-free or not. Imagine we are writing a web service to help students with all their homeworks; you specify the language, and the web service outputs "context-free" or "not context-free". Is there any good approach to automating this?

There are of course techniques for manual proof, such as the pumping lemma, Ogden's lemma, Parikh's lemma, the Interchange lemma, and more here. However, they each require manual insight at some point, so it's not clear how to turn any of them into something algorithmic.

I see Kaveh has written elsewhere that the set of non-context-free languages is not recursively enumerable, so it seems there is no hope for any algorithm to work on all possible languages. Therefore, I suppose the web service would need to be able to output "context-free", "not context-free", or "I can't tell". Is there any algorithm that would often be able to provide an answer other than "I can't tell", on many of the languages one is likely to see in textbooks? How would you build such a web service?

To make this question well-posed, we need to decide how the user will specify the language. I'm open to suggestions, but I'm thinking something like this:

$$L = \{E : S\}$$

where $E$ is a word-expressions and $S$ is a system of linear inequalities over the length-variables, with the following definitions:

• Each of $x,y,z,\dots$ is a word-expression. (These represent variables that can hold any word in $\Sigma^*$.)

• Each of $a,b,c,\dots$ is a word-expression. (Implicitly, $\Sigma=\{a,b,c,\dots\}$, so $a,b,c,\dots$ represent a single symbol in the underlying alphabet.)

• Each of $a^\eta,b^\eta,c^\eta,\dots$ is a word-expression, if $\eta$ is a length-variable.

• The concatenation of word-expressions is a word-expression.

• Each of $m,n,p,q,\dots$ is a length-variable. (These represent variables that can hold any natural number.)

• Each of $|x|,|y|,|z|,\dots$ is a length-variable. (These represent the length of a corresponding word.)

This seems broad enough to handle many of the cases we see in textbook exercises. Of course, you can substitute any other textual method of specifying a language in algebraic form, if you like.

• Wouldn't it be easier to start with regularity of languages? – Yuval Filmus Nov 11 '13 at 20:05
• @YuvalFilmus, sure would! Now that you mention it, that's a great idea. Do you think the problem is feasible for regular languages? I'd be happy to ask a corresponding about regular languages, if you think that might be valuable. – D.W. Nov 11 '13 at 20:46
• It would certainly be easier for regular languages. By the way, the general non-decidability doesn't necessarily apply to languages of the form you mention. – Yuval Filmus Nov 11 '13 at 23:50
• I'm afraid this problem is probably open, at least a specific case is: cstheory.stackexchange.com/questions/17976. There might be a way to get undecidability for your more general problem, but I don't see it. – sdcvvc Nov 12 '13 at 14:55
• it would be helpful to give some example words in the language. suggest further research/ collaboration in Computer Science Chat – vzn Sep 18 '14 at 19:10

By Rice's theorem, to see if the language accepted by a Turing machine has any non-trivial property (here: being context free) is not decidable. So you would have to restrict the power of your recognizing machinery (or description) to make it not Turing complete to hope for an answer.

For some language descriptions the answer is trivial: If it is by regular expressions, it is regular, thus context free. If it is by context free grammars, ditto.

• I agree with all of your comments, but I'm not sure I see how this answers the question or to use this how to answer the question. I'm aware of all of those facts. I describe a particular way of specifying languages. Are you suggesting it is Turing-complete? It doesn't look likely to be Turing-complete to me. A system of linear inequalities is not Turing-complete, so I suspect/speculate I have already restricted it enough to be not Turing-complete. Also, for the method I gave for specifying languages, it's not trivial, as it's not a regular expression and not a context-free grammar. – D.W. Aug 16 '19 at 16:32

Try JFLAP software if you just want to check a CFG. You can maybe even ask JFLAP developers to give you the code or algorithm for the software. you can get JFLAP from here http://www.jflap.org/jflaptmp/ it is free however it requires JDK or JRE or something. Or maybe you can try some other similar softwares and their developers.

• I'm not sure this answers the question. JFLAP has no feature that accepts a language in mathematical notation and tells you whether it is context-free or not. – Yuval Filmus Nov 2 '18 at 16:08
• THEOREM 2.20 in Sipser book A language is context free if and only if some pushdown automaton recognizes it. And you can build PDA in JFLAP from a grammar – Haseeb Hassan Asif Nov 2 '18 at 16:39
• You maybe right about mathematical notation that can't be put in JFLAP but you can still put all rules of a grammar and it can either convert it into a PDA or says it is not a CFG or some other error – Haseeb Hassan Asif Nov 2 '18 at 16:41
• How would you represent $\{a^nb^nc^n : n \in \mathbb{N}\}$ as a grammar? Also, it is probably undecidable to tell whether an unrestricted grammar generates a context-free language, so I doubt JFLAP can do that. – Yuval Filmus Nov 2 '18 at 16:45
• I imagine that JFLAP can convert a context-free grammar to an equivalent PDA, but this is of absolutely no help here. – Yuval Filmus Nov 2 '18 at 16:45

Any language is is accepted by a Push Down Automata, is a CFL. Here is a detailed breakdown to determine whether a language is CFL or not. check if language is CFL or not

• This is not an algorithm. – xskxzr Apr 20 '19 at 18:35
• I don't see how this answers the question. I am aware that a language is context-free iff it is accepted by a PDA, but that doesn't seem to help with finding an algorithm of the form requested in the question. The geeksforgeeks article you link to does not provide a complete algorithm for this problem; it just lists non-exhaustive special cases that are easier (and it's not a great reference, as some of its statements are a bit sketchy/dubious). – D.W. Apr 20 '19 at 18:37
• AFAIK, there is no well structured algorithm for that yet. (correct me if I am wrong). The best we can do is to check for cases. – SiluPanda Apr 22 '19 at 11:40