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$$

980 questions
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
  • 373
4
votes
1 answer

Chomsky normal form complexity

How to prove that the complexity of transforming any context-free grammar without epsilon productions to chomsky normal form is $ O(N^2) $ , because I found this in 2 articles, but without proof
user156981
  • 41
  • 2
4
votes
1 answer

What if I replace a stack with a queue?

Suppose I have a PDA $P$. What if I replace its First-In-Last-Out stack with a First-In-First-Out queue? Is this new machine with a queue just as powerful as a PDA? Can it represent the same languages? I feel that it can because we can still count…
John Hoffman
  • 2,734
3
votes
3 answers

Can't write context free grammar for language $L=\{a^n\#a^{n+2m}, n,m \geq 1\}$

Spent some time on this problem and seems like I am not able write context free grammar for language $L=\{a^n\#a^{n+2m}, n\geq 1\wedge m \geq 1, n\in \mathbb{N} \wedge m\in\mathbb{N}\}$ I am sure I am missing something obvious, but can't figure out…
Mr. L
  • 133
3
votes
0 answers

Are there general guidelines to make a grammar unambiguous?

I realized that we can't have more than one uppercase letter of the same type. For instance, we can have S -> AB, but not S -> BB. Is there any other principles we must abide to when writing an unambiguous grammar?
francis
  • 41
3
votes
1 answer

Does the grammar generate the words?

Given the following grammar: $$ E \to S$$ $$ S \to T \mid S+T $$ $$ T \to P$$ $$ P \to F\mid P*F $$ $$ F \to V\mid(E)$$ $$ V \to a\mid b\mid c$$ Does this grammar generate the words $(b*a)+c$ and $b*(a+c)$ ?
Mary Star
  • 13,956
3
votes
2 answers

CFG for this language

I have this language $L=\{a^nb^m|n\not = m; n,m \in \mathbb{N}\}$ I have to make CFG for this language but I'm clueless.
James
  • 49
3
votes
1 answer

Non-ambiguous CFG for an expression

I was practicing for an exam of CFG and I'm struggling with this CFG: $L =\{a^ib^jc^k | j \le i+k\}$ I tried this CFG but is ambiguous: $$S\to AXC$$ $$A\to aA|\lambda$$ $$C\to cC|\lambda$$ $$X\to YZ$$ $$Y\to aYb|\lambda$$ $$Z\to bZc|\lambda$$ If…
3
votes
2 answers

Is this language $L$ context free?

$L$ is a language combined with the symbols $\texttt{a}$, $\texttt{b}$ and $\texttt{c}$ given by: $$ L = \{ v\texttt{c}w \mid v, w \in \{ \texttt{a, b} \}^*\text{ and } v \neq w \}.$$ I tried to prove that it is not context-free using the pumping…
3
votes
2 answers

$w$ such that it contains at least 3 ones, is my approach to the CFG right?

So I was trying to solve the CFG, $$\{w \in (0,1)^* \mid w \text{ contains at least three 1's}\}$$ My approach: I decided that a string can begin with a $0$, end with a $0$, it may begin with a $1$, ended with a $1$, begin with a 0 end with a $1$,…
Gaak
  • 221
3
votes
2 answers

Is my context free grammar here correct?

So in preparation for an interview, I have been revising and studying by solving some CFGs, and here is a question, which I solved, but I feel like I haven't solved it right, given its boundary requirements. Give a CFG for the set of all strings in…
Gaak
  • 221
3
votes
0 answers

If there is an infinite loop in a context-free grammar, is the resulting language context-free?

Suppose I have the following context-free grammar: $ S = A \\ A = aA $ Will the resulting language be context-free? If it is, this would also mean a Turing machine should be able to produce it, since it's higher in the hierarchy. But how? Wouldn't…
3
votes
2 answers

prove (by grammar or closures) that the prefix language is context free

I have a language $L$, which is context-free, and I have $Pref(L)$, which is the language of all the prefixes of the words in $L$. I need to prove that $pref(L)$ is context free only with closures or grammar. I can use some help please...thanks in…
DanielY
  • 979
3
votes
3 answers

Is this language context free or not?

I am studying for my next midterm for formal languages and finite state machines. I am stuck with this problem. Determine whether this language is context free or not. The language is : $$ L = \{ a^n b^j | n \leq j^2 \} $$ I tried designing a PDA…
Jiyda Moussa
  • 239
  • 2
  • 11
3
votes
2 answers

Grammar for $0^a1^b2^c3^d$ with $a+b = c+d$

I'm currently attempting to construct a grammar for the language $L = \{0^a1^b2^c3^d | a,b,c,d \in \mathbb{N} \land a+b = c+d\}$ However I'm getting stuck on constructing the rules in a way that the $2$s and $3$s always appear in the correct…
ntldr
  • 47
1
2 3
12 13