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
11
votes
2 answers
Drop-in, portable parsing
I see umpteen posts a day about "how to do X with regexen". And the best response to most of them seems like it would honestly be, "Why are you trying to drive a screw with a hammer?" But regexen are everywhere, and the syntax is mostly portable,…

Jeremy W. Sherman
- 35,901
- 5
- 77
- 111
10
votes
3 answers
Is "regex" in modern programming languages really "context sensitive grammar"?
Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now…

notnot
- 4,472
- 12
- 46
- 57
10
votes
4 answers
Any tools can randomly generate the source code according to a language grammar?
A C program source code can be parsed according to the C grammar(described in CFG) and eventually turned into many ASTs. I am considering if such tool exists: it can do the reverse thing by firstly randomly generating many ASTs, which include tokens…

W.Sun
- 868
- 2
- 11
- 19
10
votes
5 answers
NLTK Context Free Grammar Genaration
I'm working on a non-English parser with Unicode characters. For that, I decided to use NLTK.
But it requires a predefined context-free grammar as below:
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP ->…

ChamingaD
- 2,908
- 8
- 35
- 58
9
votes
3 answers
Identity expression, factor, and term?
I am learning context-free grammar, and I don't understand how to identity expression, factor and term in a programming language like C or C++.
Suppose we have an assignment statement, id := E, where E is any arithmetic expression.
What is a term?…

CppLearner
- 16,273
- 32
- 108
- 163
9
votes
1 answer
Parsing a context-free grammar in Python
What tools are available in Python to assist in parsing a context-free grammar?
Of course it is possible to roll my own, but I am looking for a generic tool that can generate a parser for a given CFG.

Yuval Adam
- 161,610
- 92
- 305
- 395
9
votes
3 answers
self-taught compiler courses / good introductory compiler books?
Does anyone know of online course / university lectures that comprise a typical compiler course? I've had theory of computing but unfortunately my school didn't offer a course in compiler construction.
I know there are lectures out there; I was…

larryq
- 15,713
- 38
- 121
- 190
9
votes
1 answer
If we know a CFG only generates regular language, can we get the corresponding regular expression?
As we know, given a regular grammar, we have algorithm to get its regular expression.
But if the given grammar is context-free grammar (but it only generates regular language), like
S->aAb
A->bB
B->cB|d
Is there any existing algorithm that…

JackWM
- 10,085
- 22
- 65
- 92
8
votes
2 answers
Using Parsec to parse regular expressions
I'm trying to learn Parsec by implementing a small regular expression parser. In BNF, my grammar looks something like:
EXP : EXP *
| LIT EXP
| LIT
I've tried to implement this in Haskell as:
expr = try star
<|> try litE
<|>…

Xodarap
- 11,581
- 11
- 56
- 94
8
votes
1 answer
Genetic algorithm grammar induction program/code?
Does anyone know of a program that uses a GA to perform grammar induction/inference, I've read tonnes of research papers and articles on this stuff like Lankhorst and De Pauw but I can't find any implementations or programmes that use this technique…

Matt Robinson
- 305
- 1
- 4
- 14
8
votes
5 answers
Context free grammar for non-palindrome
I need a CFG which will generate strings other than palindromes. The solution has been provided and is as below.(Introduction to theory of computation - Sipser)
R -> XRX | S
S -> aTb | bTa
T -> XTX | X |
X -> a | b
I get the general idea…

Abhijith Madhav
- 2,748
- 5
- 33
- 44
8
votes
3 answers
Pumping Lemma with Context Free Languages
I have the language {a^i b^j c^k | i,j,k>=0 & i>j & j>k}
I began by assuming some m is picked for me, such that a string
z = a^m b^(m-1) c^(m-2)
Then the string is split up into (z =) uvwxy so that vx are not empty and #(vwx)<=m
Then when I get…

Alex
- 5,364
- 9
- 54
- 69
8
votes
1 answer
How does one implement wildcards, character classes, negated character classes, etc. in a model for a regular grammar?
TL;DR:
How does one computationally model a grammar's productions such that an indefinite number of products exist for the same left hand side?
I'm working on a project regarding formal language theory and am trying to write a class for building…

Tyler Crompton
- 12,284
- 14
- 65
- 94
8
votes
1 answer
What is the difference between Lexical grammar and Syntactic grammar?
I am reading The Java Language Specification 8.
I am trying to understand Chapter 2. Grammars.
Here's what I have already learned:
Semantics:
Semantics is the study of meaning.
Meaning:
Meaning, in semantics, is defined as being Extension: The…

Rounak
- 613
- 3
- 8
- 22
8
votes
1 answer
First and follow of the non-terminals in two grammars
Given the following grammar:
S -> L=L
s -> L
L -> *L
L -> id
What are the first and follow for the non-terminals?
If the grammar is changed into:
S -> L=R
S -> R
L -> *R
L -> id
R -> L
What will be the first and follow ?

petromax
- 91
- 1
- 3