Talk:Context-free grammar

message: :It is not possible to construct a general algorithm which takes as input two context-free grammars and decides whether the two grammars generate the same language; nor is it decidable whether their languages have a single string in common. It is however possible to decide whether a given context-free grammar generates a non-empty language or not, and it is also possible to decide algorithmically whether a given context-free grammar generates an infinite language or not. 
Is it possible to decide whether given context-free grammar is equivalent to given
regular grammar ? In particular is it possible to decide whethere there is single string
that doesn't belong to context-free language (that is, whether or not is it equivalent to .*) ? Taw 14:41 Apr 6, 2003 (UTC)
:The answer for your first question is Pumping lemma. -- Sundar 05:08, Feb 15, 2005 (UTC)
:And the answer to your second question is yes. See Rice's Theorem. -- JBolla
::It seems both answers are wrong. It is undecidable whether a given context-free grammar generates all words over its alphabet. Jochgem 23:45, 11 March 2007 (UTC)
replies: text: The first answer is indeed wrong as well: it is undecidable whether any given context-free language is regular (I was surprised to learn this, too.  Google for references).  The pumping lemma isn't good enough: some non-regular languages can be pumped. Rp (talk) 23:29, 11 April 2008 (UTC)
replies: text: I have tried to search for results about "whether any given CFL is regular" without success; the terms I have come up with appear together too often in other contexts. Can you reproduce the search that led to your earlier discovery? 128.105.181.52 (talk) 19:41, 2 February 2012 (UTC)
replies: text: I added a source, found by searching for the terms "context-free", "is regular", and "undecidable" in Google books. —David Eppstein (talk) 20:10, 2 February 2012 (UTC)

text: What's unclear? The general problem is hard. Some special cases of it are not hard. That seems to be what we say. —David Eppstein (talk) 21:21, 7 December 2014 (UTC)
replies: text: My guess: the sloppy way such problems are traditionally formulated. The theorem as usually stated: it is undecidable whether a context-free language is regular. This is a very sloppy formulation: it doesn't state how we know that the language in question is context-free. Properly stated, the theorem says: it is undecidable whether, given an arbitrary context-free grammar (or, equivalently, an arbitrary pushdown automaton), the language it generates (or recognizes) is regular.  In other words: whether some left-linear or right-linear grammar describes the exact same language.  Strictly speaking, this is a stronger theorem. Now, it is immediately clear that if the given CFG happens to be left-linear or right-regular already, the affirmative answer follows immediately.  All the more surprising that it there are so many other ways to describe regular languages with CFGs that it is impossible to devise an algorithm that decides for any given CFG whether it describes a regular language. Rp (talk) 23:20, 7 December 2014 (UTC)
thread_title: Decidability

message: The article states that  "BNF (Backus-Naur Form) is the most common notation used to express context-free grammars.", but still the article make use of production rules like V→w instead of V::=w, is that because production rules and BNF are interchangeable ?
--Robsy 11:32, 2 May 2006 (UTC)
replies: text: No, it is because theoretical articles about context-free grammars use the arrow, while the BNF notation is pretty much restricted to manuals or textbooks that describe the syntax of a concrete language.  The reason for the difference, by the way, is that BNF was designed for ASCII-only text. Rp 09:28, 25 July 2007 (UTC)
thread_title:  BNF and Production rules 

message: A starting point would be to add an explanation of the used symbol S to the first example. It represents a grammar? In that case, would G be more appropriate? No? Why? Also, the use of the arrow symbol (product?) could be linked to an article describing that symbol etc. The use of symbols instead of human language warrants for an easy reference material to symbols used. Otherwise the use of symbols serve only an obfuscation purpose, being informative only to those already adept in the topic, which doubtfully is the purpose of Wikipedia.
: The link is right there; click on grammar. Rp (talk) 15:15, 28 September 2009 (UTC)
thread_title:  Clarify clarify and clarify more 

message: I stumbled upon a tool that produces images from context free grammars. Here is the link
http://www.contextfreeart.org/index.html
140.203.154.11 (talk) 18:37, 11 November 2008 (UTC)
replies: text: Nice; this kind of stuff is typically associated with L-systems, which reminds me that L-systems should definitely be mentioned in the article.  Please do ... Rp (talk) 22:52, 9 January 2009 (UTC)
thread_title:  link proposal 

message: I have read this article a few times over the months, and never noticed that it mentioned (in the Normal Form section) that the membership problem is decidable (i.e. determining whether a string is generated by a given grammar). I think this should get more prominent mention in the article. Maybe in the summary? It at least shouldn't only be buried at the end of the normal form section. --pdf23ds 24.174.103.112 (talk) 18:17, 17 May 2009 (UTC)
replies: text: This is implied by the remarks about parsing algorithms.  Incidentally, I believe the statement while the widely used LR and LL parsers are more efficient algorithms that deal only with more restrictive subsets of context-free grammars is incorrect: they aren't more efficient, only simpler. Rp (talk) 15:19, 28 September 2009 (UTC)

text: The text now reads: Deterministic grammars (...) These classes are important in parsing: they allow string recognition to proceed deterministically, e.g. without backtracking.'  This is still wrong: arbitrary grammars can be parsed without backtracking, using essentially matrix multiplication, see Cocke-Younger-Kasami algorithm and Earley parser. Rp (talk) 11:42, 9 December 2009 (UTC)
replies: text: I have rewritten these paragraphs - please review. Rp (talk) 20:09, 31 March 2011 (UTC)
thread_title: Decidability again

message: I like Likebox's edits a lot in general, but I'm not so happy about the references to Noam Chomsky's beliefs.  The article should provide facts about cfgs, not beliefs.  And while they are at the core of Chomsky's frameworks for describing syntax, they were always complemented with transformations in some form, so the reader shouldn't be left with the impression that syntax == context free grammar in Chomsky's eyes. Rp (talk) 19:32, 11 January 2010 (UTC)
replies: text: Yes, I suppose that might belong elsewhere. The only reason I placed it there is because I think that the only observation Chomsky made that is not controversial is that context free grammars are very special types of grammars, and that context free grammars are suited to describe all of the complex grammatical clause-nesting/adjective-listing in natural language. Everything else he says is controversial, but I thought that this part is univerally accepted.Likebox (talk) 03:42, 12 January 2010 (UTC)
replies: text:  Thank you-- I am sorry for not being completely accurate about this, I always mean to, but I never read enough of Chomsky's work.Likebox (talk) 17:53, 1 February 2010 (UTC)
thread_title:  Chomsky's beliefs 

message: The article in question points out that Swiss German has a non context free construction, which involves the following:
Jan says that (we-1) (Mary-1) (the house-2) helped-1 paint-2
The numbers say which nouns go with which verb. is is not stack based, otherwise it would be:
Jan says that (we-1) (Mary-1) (the house-2) (painting-2) (helped-1)
So agreed, it's not context free. But you need to go several levels deep to make this work. The claim that they make is that you can go on in this pattern in Swiss German with more nouns and more verbs
Jan says that we-1 Mary-1 Martha-2 Sally-3 the house-4 told-1 tell-2 help-3 paint-4
If you find something like this, that would be the clincher. But I have not been able to verify this schema as grammatical in Dutch German, and the gloss provided by the authors for their analogous sentence was insufficient to determine whether it was like the above.
But the construction in question is peculiar to Swiss German, and Dutch, while Chomsky was working with English. It is manifestly obvious that English grammar is context free. I don't know how this article can be used to support the statement that "Chomsky's observations about the non-context freeness of natural language has held up", especially that I don't see any examples analogous to the above in Chomsky, and I don't know of any in English.Likebox (talk) 13:00, 9 February 2010 (UTC)
replies: text: Chomsky has always explicitly worked on a universal theory of language, i.e. one that would not be tied to any particular natural language such as English. Rp (talk) 17:02, 22 April 2010 (UTC)
thread_title:  Non context-freeness of Swiss German 

message: Article currently states:  "In later work (e.g. Chomsky 1981), the idea of formulating a grammar consisting of explicit rewrite rules was abandoned."
Could someone (who is more familiar than myself with the subject matter) please rewrite this sentence to eliminate the ambiguity and the passive voice.  "Was abandoned" ... by who?  Chomsky?  
Karl gregory jones (talk) 19:02, 9 February 2011 (UTC)
thread_title:  Ambiguity, Passive Voice:  needs improvement 

message: In the last paragraph of subsection "Proper CFGs" we find the following:
This makes it clear that 
L(G) = \{ww^R:w\in\{a,b\}^*\}. The language is context-free, but as shown in Example 4.8, it is not regular.''
Should we remove this hard-coded reference to "Example 4.8"? Perhaps we might add an actual reference for this result.
(Sorry for the minor and possibly irrelevant points, I'm a newbie contributor.)  — Preceding unsigned comment added by Marcelkcs (talk • contribs) 16:39, 29 May 2011 (UTC)
thread_title:  Minor question about an example 

message: I don't think it makes sense to require that there is at least one production
rule for the non-terminal S. All that counts is that the transitive closure
of the production rules, filtered to terminals is taken, in defining the
accepted language L(G). If there is no production rule for S, then we have
obviously L(G) = {}, i.e. the accepted language is empty. This is not to
be confused with L(G) = {eps}.
But there are also other context free grammars that also lead to empty
languages. And there are such that have a rule for the start symbol S.
For example the following grammar, has also an empty accepted language:
   S --> S
So the requirement that thre is at least one rule for S, does not imply
that the accepted language L(G) is non empty, so it doesn't make any sense.
Janburse (talk) 20:09, 20 June 2011 (UTC)
replies: text: I agree.  Where do you see this requirement?  It may be too early in the morning, but I can't find it in the article. Rp (talk) 07:29, 22 June 2011 (UTC)
replies: text: I found it and removed the requirement. --Zahnradzacken (talk) 13:53, 23 June 2011 (UTC)
thread_title:  Formal Definition of a Context Free Language and the Empty Language 

message: I'm currently studying CFGs in school; I got rather confused reading this article, when presented with this notation:
    S → SS
    S → (S)
    S → () 
I hadn't seen a CFG presented that way before and I didn't know what it meant. Then later in the article I arrived at 
    S → bSbb | A A → aA | ε
which is what I'm used to seeing. 
After hunting through the article numerous times I finally searched the page for pipes and found "It is common to list all right-hand sides for the same left-hand side on the same line, using | (the pipe symbol) to separate them. Hence the grammar above can be described more tersely as follows:" arbitrarily hidden under the "A regular grammar" section.
There absolutely needs to be a "Notation" section before the "Examples" section that clearly explains both styles of notation to the reader. Not explaining your first notation and switching to another notation midway through with an easily-missed warning is a recipe for confusion for those not fully familiar with the subject. I would write a notation section myself, but I don't feel I grasp the concept and terminology enough to write accurately about it. Some guy (talk) 05:52, 3 March 2013 (UTC)
replies: text: Is there anything else you would like to read in a separate notation section? I moved the sentence you quoted up to the section Production rule notation, maybe that's enough for now? --Zahnradzacken (talk) 10:03, 23 March 2013 (UTC)
thread_title: Notation

message: The lead (or lede in Wiki-speak?) is terrible, IMO.  The Background section does a better job of explaining what this is all about.  I am a SW engineer, and remember a little about this from college many years ago, but that lead section did nothing in helping me remember the overall purpose and meaning of a CFG.  I can't imagine how it could be expected to help a lay-person. Nerfer (talk) 17:18, 23 March 2020 (UTC)
replies: text: I made an attempt to improve the lead, by replacing the former example by a C example. The problem is that there are two different audiences: a user knowing formal grammars in general should be told why these ones here are called context-free, while a user who is new to formal grammars should be told how a grammar works in general. - Jochen Burghardt (talk) 21:32, 23 March 2020 (UTC)
thread_title:  lead paragraph 

message: Until the section section "Well-formed parentheses", commas are used to separate production rules. But from there on, no commas are used! 
I think this should be done in an uniform way.  — Preceding unsigned comment added by Doubletimer (talk • contribs) 17:55, 27 November 2020 (UTC)
thread_title:  Misleading commas in examples 

message: I don't have editing experience, so I thought I'd bring this up here and let someone else address it. I buy that the language in this section (balanced but not properly nested strings of parentheses and brackets) is not context-free, but this cannot be shown through the Pumping Lemma. Indeed, any nonempty string in this language will contain either the pair '(' and ')' or the pair '[' and ']' - either of these pairs of substrings will satisfy the conclusion of the Pumping Lemma. The observation that this language contains the sublanguage (^n [^n )^n ]^n is irrelevant. The complexity of a language puts no bounds on the complexity of its subsets (e.g. the regular language (a)* of all strings of a's contains the subset {a^n | n is in your favorite non-recursively-enumerable set of natural numbers}). Mersenne44 (talk) 21:07, 26 September 2022 (UTC)
replies: text: I think the standard proof is that L intersect (^* [^* )^* ]^* equals (^n [^m )^n ]^m. By closure under intersection with regular languages, if L were context free (^n [^m )^n ]^m would be context free. But it is not, by pumping. The bigger problem here, though, is that this section needs a published source. If we find one, we can follow whatever proof is used there. —David Eppstein (talk) 22:24, 26 September 2022 (UTC)
replies: text: Oh I see - if you use (^n [^m )^n ]^m instead then it does make sense to just say L 'contains' this subfamily, since it witnesses unbounded distance between pumpable infixes. Thanks for clearing that up for me! I don't think there's a good proof that passes through (^n [^n )^n ]^n though, so that bit really should be changed. As far as citations go, might it be better to forego this example and just use L = a^n b^n c^n in this section? It's a pretty canonical example and appears in most published introductions to context-free languages that I've seen, plus the application of the pumping lemma is maybe a little more immediately obvious. Mersenne44 (talk) 00:01, 27 September 2022 (UTC)
replies: text: Oh nevermind - I'm just tired. Same thing works with (^n [^n )^n ]^n - it witnesses the same thing as (^n [^m )^n ]^m. Ignore me - sorry! Mersenne44 (talk) 00:14, 27 September 2022 (UTC)
thread_title:  Examples of languages that are not context free: proof is incorrect 

message: Minor nitpick here.
The article says:
: It is common to list all right-hand sides for the same left-hand side on the same line, using | (the pipe symbol) to separate them. Rules \alpha\rightarrow \beta_1 and \alpha\rightarrow\beta_2 can hence be written as \alpha\rightarrow\beta_1\mid\beta_2. In this case, \beta_1 and \beta_2 are called the first and second alternative, respectively.
My suggestion is to avoid calling | the "pipe symbol", which might cause confusion to people interpreting it as the Bash pipe (https://en.wikipedia.org/wiki/Vertical_bar#Pipe) and think that \beta_1 is somehow fed into \beta_2.
Since the vertical bar (https://en.wikipedia.org/wiki/Vertical_bar) goes by a lot of other names, I think it would me more appropriate to refer to it as either:
* the "vertical bar",
* or the "disjunction operator"/"alternative symbol" linking to https://en.wikipedia.org/wiki/Vertical_bar#Disjunction.
130.192.232.228 (talk) 12:21, 30 September 2022 (UTC)
replies: text:  I agree that "pipe symbol" can be understood by Linux users only, and is misleading even to them. I changed both occurrences to "vertical bar". - Jochen Burghardt (talk) 14:27, 30 September 2022 (UTC)
thread_title:  Avoid calling | the "pipe symbol" 

message: In the "Examples" section, the aforementioned sentence is confusing to me (I understand a lot of the rest of the page). The ε-production itself is defined in the context of the article earlier (further up) and no mention is made alluding to why there'd be [later] some improper something with it. It is only described what an ε-production is. I can't find any information that should somehow hint at or explain why "it is not proper". Transientdifficulties (talk) 20:10, 6 April 2023 (UTC)
replies: text: I agree. Some definitions of CFGs (including Chomsky's) do not allow empty productions, and this should be explained, but it doesn't appear to be standard practice to call $\epsilon$-less grammars "proper". So the right approach is to add a paragraph explaining this and refer to it here. Rp (talk) 16:06, 11 April 2023 (UTC)
thread_title:  Should "it is not proper since it includes an ε-production." be _explained_?