Questions tagged [context-free-grammar]

Context-free grammars give a set of rules for generating formal languages. The formal languages generated by a context-free grammar are known as context-free languages.

Context-free grammars (CFGs) were introduced by Noam Chomsky in an attempt to classify formal languages. CFGs are type-2 grammars in the Chomsky hierarchy and generate what is known as context-free languages.

Formally, CFGs consists of a set $V$ of variables, an alphabet $\Sigma$, a set of production rules $R$ and a start symbol $S \in V$.

As an example, the following CFG generates the language of all strings starting with $1$ over the alphabet $\{1,0\}$. The start symbol is $S$.

$$S \rightarrow 1B$$

$$B \rightarrow 1B \; | \; 0B \; | \; 1 \; | \; 0$$

Here the arrow means that the variable on the left-hand side can be replaced by any of the expressions on the right-hand side, where different expressions are separated by a $|$.

To derive the string $1101$ from the previous CFG, we can give the following derivation, starting with the start symbol $S$.

$$S \Rightarrow 1B \Rightarrow 11B \Rightarrow 110B \Rightarrow 1101$$

991 questions
28
votes
3 answers

An efficient way to determine if two context free grammars are equivalent?

I'm wondering if there's an efficient way of checking to see if two context free grammars are equivalent, besides working out "test cases" by hand (ie, just trying to see if both grammars can generate the same things, and only the same things, by…
pauliwago
  • 655
23
votes
4 answers

Determining Ambiguity in Context Free Grammars

What are some common ways to determine if a grammar is ambiguous or not? What are some common attributes that ambiguous grammars have? For example, consider the following Grammar G: $S \rightarrow S(E)|E$ $E \rightarrow (S)E|0|1|\epsilon$ My guess…
20
votes
2 answers

Eilenberg's rational hierarchy of nonrational automata & languages

In the preface to his very influential books Automata, Languages and Machines (Volumes A, B), Samuel Eilenberg tantalizingly promised a Volume C dealing with "a hierarchy (called the rational hierarchy) of the nonrational phenomena... using rational…
17
votes
1 answer

Generating functions for context-free languages

I have a question about context free grammars and their relationship with generating functions. It is well-know how to associate a generating function $\mathsf{gf}{(R)}$ with a non-ambiguous regular expression $R$ over the alphabet…
16
votes
4 answers

Can a regular grammar be ambiguous?

An ambiguous grammar is a context-free grammar for which there exists a string that has more than one leftmost derivation, while an unambiguous grammar is a context-free grammar for which every valid string has a unique leftmost derivation. A…
12
votes
4 answers

How to create a grammar for complement of $a^nb^n$?

I've got a language L: $$ \Sigma = \{a,b\} , L = \{a^nb^n | n \ge 0 \} $$ And I'm trying to create a context-free grammar for co-L. I've created grammar of L: P = { S -> aSb S -> ab | epsilon } In co-L, I don't know how to ensure, that there…
12
votes
3 answers

Context free grammar for language { {a,b}*: where the number of a's = the number of b's}

My professor said that converting a language to a CFG is more of an art than anything else. I looked at this problem and didn't even know how to get started, or how I would reason my way to the solution (which I understand how it is correct). Are…
pajkatt
  • 383
11
votes
6 answers

A computer's memory is finite, so how can there be languages more powerful than regular?

A computer has a finite memory. There are no computers with infinite memory. Therefore the only languages that a computer can process are those whose member strings are finite. As I recall, the computational power required for any finite language is…
11
votes
2 answers

Show that minimal CFG is undecidable (Sipser 5.36)

Question: Say that a CFG (context-free grammar) is minimal if none of its rules can be removed without changing the language generated. Let $MIN_{\text{CFG}}$ = $\{\, \langle G \rangle$ | $G$ is a minimal CFG$\}$. a) Show that $MIN_{\text{CFG}}$ is…
10
votes
1 answer

Languages with context-free grammar having only one non-terminal symbol

As seen in this question, the class of languages that can be generated by a context-free grammar having only one non-terminal symbol (i.e. the start symbol) is a proper subclass of the class of context-free languages (in particular, it doesn't…
9
votes
1 answer

Converting to Chomsky Normal Form

I am trying to learn how to convert any context free grammar to Chomsky Normal Form. In the example below, I tried to apply Chomsky Normal Form logic, to result in a grammar, where every symbol either produces two symbols, or every symbol produces a…
8
votes
1 answer

How do I show language of prime number of xs not context-free?

I have a hunch that the language $L = \{ x^n : n \text{ is prime.} \}$ is not context-free. I am trying to show that by contradiction with the Pumping Lemma: First assume that $L$ is context-free. That means for any string in $L$ of a certain…
David Faux
  • 3,515
  • 10
  • 40
  • 55
8
votes
1 answer

Why is this Parsing Expression Grammar left recursive?

I'm trying to get my parser generator to accept this specification. I know that's kind of a programming question, but I figured this was the best place to ask. It's specified as a Parsing Expression Grammar, I think. start = Expr Expr = [0-9]+…
Jack M
  • 28,518
  • 7
  • 71
  • 136
7
votes
0 answers

Unambiguous formal grammars for a specific class of languages

Suppose that $w \in \{0; 1\}^*$ is a binary word. Let's denote the number of $0$-s in $w$ as $\#_0(w)$ and the number of $1$-s in $w$ as $\#_1(w)$. Now suppose that $q \in \mathbb{Q}$ is a positive rational number. Consider the language $L_q \subset…
7
votes
1 answer

Is First Order Logic Syntax Context Free?

If I were to build a first order logic parser could I do it using a context free parser generator (like bison)? I found some articles where First Order Logic Syntax is described with EBNF notation, usually like this: :=
1
2 3
65 66