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
0
votes
0 answers

Context-Free Grammars(Clarification on answer)

I'm trying to make sure if i did a) correct. I believe it makes sense, just trying to see if anyone has any suggestions. The grammar G= for the calculator language is defined by: V={calculation, expression, value, number,…
geforce
  • 217
0
votes
1 answer

Context-free grammar for an expression

how to find context-free grammar for generating language $$J={\{ba^mc^na^ma^nb}\;| n\ge1, m\ge1\} $$ I have already solved problems with constructions like $a^mc^mc^na^n$, but how to appropach the problem if the values in superscripts are…
TheMP
  • 127
  • 1
  • 6
0
votes
1 answer

Obtaining a grammar CFL

Let b(n) denote the binary representation of n >= 1, leading zeros omitted. For example, b(5) = 101 and b(12) = 1100. Let $ be another symbol not in {0,1}. Suppose we reverse the first numeral; that is, consider the set {revb(n)$b(n+1) | n >= 1}.…
0
votes
0 answers

How do I know how many times to repeat a replacement when generating a grammar?

My textbook discusses Context Free Grammars, and provides the following rules: A -> 0A1 A -> B B -> # The resulting string is 000#111. Shouldn’t it just be 0#1? My steps: A 0A1 0B1 0#1 I’m missing something obvious, what it is? How do I know how…
Moshe
  • 265
0
votes
1 answer

context free grammar production rules

I am working with context free grammars and have a question concerning the production rules. I have read that the rules are formalized as pairs (α,β) ∈ R. The natural language rules that I am working with are of the form: S → A B, B → C D E, D →…
beoliver
  • 178
0
votes
1 answer

How to Determine which language is guaranteed to be a deterministic Context-Free Language

I'm struggling with figuring out which one of these languages is guaranteed to be a DCFL, i have two languages to choose from and the word guaranteed is throwing me off. Here are the two languages: Let f1(L) = {w : wa ∈ L for some a ∈ Σ} f2(L)…
-1
votes
2 answers

Obtain a grammar for the language (i) L = {a ^m b ^n ; m ≠ n ; m , n > 0 }

Obtain a grammar for the language $L = \{a^m b^n \mid m ≠ n , m > 0 , n > 0 \}$. Please help me with the solution.
Shruti
  • 21
-1
votes
1 answer

Language to CFG ( l + m = 2n )

I'm trying to find the CFG of language L = {$0^l 1^m 0^n | l+m = 2n$}, I don't know where to start
1 2 3
12
13