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
1
vote
1 answer

How do you insure that from a CFG you get the same number of as and bs?

I can make a CFG that makes sure we can produce any string that has the same number of as and bs, but I can't insure that those strings are the only ones that are produced. S => aS | bS | E The problem is that you can get something like this:…
1
vote
1 answer

Is it possible, that intersection of regular language and context-free unambiguous language is inherently ambiguous?

Is it possible, that intersection of regular language and context-free unambiguous language is inherently ambiguous? If it is possible, give me any example please. Otherwise, I am looking for proof.
Xpast
  • 199
1
vote
2 answers

Is a divisibility language context free?

I am working to see if this language would be context free: L = { 0n1k : n/k is an integer } After playing with it for a little while, I believe that the language is not context free. Now I am looking to use the pumping lemma to prove that it is…
Tesla
  • 187
1
vote
1 answer

Removing epsilon productions from a Context-free Grammar

There is just one part of the grammar I am having trouble with, it reads: $$C\to CBA\mid\epsilon$$ After removing the epsilon production, I get: $$C\to CBA\mid BA\mid CB\mid B$$ I'm confused as to whether this is correct or not. Does the latter…
1
vote
2 answers

Construct context-free grammar for $\{a^ib^jc^k : i\le j+k\}$

I'm looking through several of old exam sets in order to prepare for the exam and now I'm stuck on this exercise, where we have to construct a context-free grammar for the language: $$L = \{a^ib^jc^k: i \le j+k \}$$ The best solution I've obtained…
John
  • 139
1
vote
0 answers

$\sum=\{0, 1, \#\}$ $C=\{x\#x^R\#x\# | x \in \{0, 1\}^*\}$ Show C̄ is CFL

From my Automata class $\sum=\{0, 1, \#\}$ $C=\{x\#x^R\#x\# | x \in \{0, 1\}^*\}$ Show C̄ is CFL I want to use pumping lemma for CFL but I can’t understand which type of language is the complement of C. Some of my friend proved by: {… demonstration…
1
vote
0 answers

Find CFG for Lisp-like expressions

All Lisp-like expressions. A Lisp expression may be an unsigned integer or a list. A list, enclosed by left and right parentheses, consists of one operator ($+$, $-$, $*$, $/$, $\max$, $\min$) followed by a sequence of at least one Lisp…
Herious
  • 157
1
vote
2 answers

Need help creating a Context-Free Grammar

I'm trying to generate a CFG for the following $L$, but I'm stuck on how to do this. $$L = \{0^i1^j2^k\mid i
Jose
  • 253
1
vote
0 answers

Is this correctly CFG for this language?

$$ L = \{ a^{2n}w \mid |w| = n\},\ \Sigma=\{ a,b \} $$ My CFG: $$ S \to \epsilon\mid aaST \\ T\to a \mid b $$ My lecturer wrote this is wrong because $w$ but I don't understand why.
benzi
  • 13
1
vote
1 answer

Creating a context free grammar for this language. (Having difficulties keeping track of $3$ parts).

create a context free grammar that creates this language: $\{w_1bw_2bw_3 : w_1,w_2,w_3\in \{a,c\}^* \space\text{and}\space |w_2|+|w_3|<2|w_1|\}$ Usually when I solve context free grammar questions, I try to find where the $2$ parts that I need to…
Pwaol
  • 2,113
1
vote
1 answer

Why is {$0^ 1^ : ≥0$} context free?

This Language {$0^ 1^ : ≥0$} is context-free as per another answer I read on this site. Link: What does it mean to say a language is context-free? For this example take S = $0^p 1^p$ However, if I take a split of: u = ε v = $0^p$ x = ε y = ε z =…
1
vote
1 answer

Is my grammar for the language $L=\{w \in \{a,b\}^+ \mid w\text{ contains }aa\text{ followed by }bb\}$ correct?

Consider the language $L=\{w \in \{a,b\}^+ \mid w\text{ contains }aa \text{ followed by }bb\}$. I wanted to know if this grammar is correct or not. This is my solution: $S\to IaabbI.$ $I\to aI|bI|e.$
1
vote
1 answer

Context Free Grammar Equal Lengths

Consider the language L = {x#y is in {0,1}* where |x| = |y|} Would this CFG be a sufficient definition of the language L? S->0S0 | 0S1 | 1S0 | 1S1 | # Thanks.
1
vote
1 answer

Is there a context free grammar that is both in Chomsky Normal Form and ambiguous?

I know that converting an ambiguous context free grammar (CFG) to be in Chomsky Normal Form (CNF) might make it unambiguous, but is it a method that necessarily makes any CFG unambiguous? My knowledge tells me that the only way to prove a CFG to be…
1
vote
1 answer

Which one of these language Context free?

Which one of the following languages is CFL and what is the grammar for it? Can we use pumping lemma for the not CFL? \begin{align*} L_1 &= \{a^mb^ma^n \mid m,n \geq 0\}\\ L_2 &= \{a^mb^ma^n \mid m \geq n \geq 0\} \end{align*}
Jesun
  • 11