In formal language theory, a context-free grammar (CFG) is a grammar subject to a special constraint: that the left-hand side (LHS) consist of a single non-terminal symbol. CFGs are capable of representing the set of context-free languages (CFLs).
Questions tagged [context-free-grammar]
1364 questions
30
votes
5 answers
How to define a grammar for a programming language
How to define a grammar (context-free) for a new programming language (imperative programming language) that you want to design from scratch.
In other words: How do you proceed when you want to create a new programming language from scratch.

Ayoub M.
- 4,690
- 10
- 42
- 52
29
votes
2 answers
What are terminal and nonterminal symbols?
I am reading Rebol Wikipedia page.
"Parse expressions are written in the parse dialect, which, like the do dialect, is an expression-oriented sublanguage of the data exchange dialect. Unlike the do dialect, the parse dialect uses keywords…

Dmitry Bubnenkov
- 9,415
- 19
- 85
- 145
29
votes
2 answers
How do Java, C++, C#, etc. get around this particular syntactic ambiguity with < and >?
I used to think C++ was the "weird" one with all the ambiguities with < and >, but after trying to implement a parser I think I found an example which breaks just about every language that uses < and > for generic types:
f(g(j));
This could…

user541686
- 205,094
- 128
- 528
- 886
28
votes
2 answers
Why does C's BNF grammar allow declarations with an empty sequence of init-declarators?
When looking through C's BNF grammar, I thought it was weird that the production rule for a declaration looked like this (according to…

rafaelfp
- 383
- 2
- 6
28
votes
1 answer
Purpose of FIRST and FOLLOW sets in LL(1) parsers?
Can anyone explain to me how FIRST and FOLLOW should be used in LL(1) grammar? I understand that they are used for syntax table construction, but I don't understand how.

Kobe-Wan Kenobi
- 3,694
- 2
- 40
- 67
27
votes
2 answers
Converting ambiguous grammar to unambiguous
I did not understand how a unambiguous grammar is derived from a ambiguous grammar? Consider the example on site: Example. How was the grammar derived is confusing to me.
Can anyone please guide me ?

name_masked
- 9,544
- 41
- 118
- 172
27
votes
2 answers
Context-free grammar for C
I'm working on a parser for C. I'm trying to find a list of all of the context-free derivations for C. Ideally it would be in BNF or similar. I'm sure such a thing is out there, but googling around hasn't given me much.
Reading the source code for…

limp_chimp
- 13,475
- 17
- 66
- 105
26
votes
2 answers
Predicate Logic in Haskell
I've been using the following data structure for the representation of propositional logic in Haskell:
data Prop
= Pred String
| Not Prop
| And Prop Prop
| Or Prop Prop
| Impl Prop Prop
| Equiv Prop Prop
…

wen
- 3,782
- 9
- 34
- 54
22
votes
4 answers
LR(1) Item DFA - Computing Lookaheads
I have trouble understanding how to compute the lookaheads for the LR(1)-items.
Lets say that I have this grammar:
S -> AB
A -> aAb | a
B -> d
A LR(1)-item is an LR(0) item with a lookahead. So we will get the following LR(0)-item for state 0:
S ->…

mrjasmin
- 1,230
- 6
- 21
- 37
19
votes
3 answers
Is JavaScript a Context Free Language?
This article on how browsers work explains how CSS is context free, while HTML is not. But what about JavaScript, is JavaScript context free?
I am learning about CFG and formal proofs, but am a long way away from understanding how to figure this…

Lance
- 75,200
- 93
- 289
- 503
18
votes
1 answer
Which grammars can be parsed using recursive descent without backtracking?
According to "Recursive descent parser" on Wikipedia, recursive descent without backtracking (a.k.a. predictive parsing) is only possible for LL(k) grammars.
Elsewhere, I have read that the implementation of Lua uses such a parser. However, the…

user200783
- 13,722
- 12
- 69
- 135
18
votes
3 answers
chomsky hierarchy and programming languages
I'm trying to learn some aspects of the Chomsky Hierarchy which are related to programming languages, and i still have to read the Dragon Book.
I've read that most programming languages can be parsed as a context free grammar (CFG). In term of…

dader
- 1,304
- 1
- 12
- 31
16
votes
1 answer
A grammar that accepts the empty set by the rule S->S
This was a homework assignment problem which I know I have incorrectly answered. I gave:
S -> ''
meaning that S yields the empty string. I know that the empty set and empty string are not the same. According to my professor, the answer is:
S ->…

Levi Morrison
- 19,116
- 7
- 65
- 85
16
votes
1 answer
What about theses grammars and the minimal parser to recognize it?
I'm trying to learn how to make a compiler. In order to do so, I read a lot about context-free language. But there are some things I cannot get by myself yet.
Since it's my first compiler there are some practices that I'm not aware of. My questions…

dader
- 1,304
- 1
- 12
- 31
16
votes
4 answers
Tips for creating "Context Free Grammar"
I am new to CFG's,
Can someone give me tips in creating CFG that generates some language
For example
L = {am bn | m >= n}
What I got is:
So -> a | aSo | aS1 | e
S1 -> b | bS1 | e
but I think this area is wrong, because there is a chance…
user1988365