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

Context free grammar of $L = \{w \in \{a, b, c\}^* : |w|_{c} = 3k +1 \}$

So, this is my first homework in conext free grammar, I want the grammar that generates all possible words in L, I have come out with the following rules $$ S \rightarrow aS | bS| cX\\ X \rightarrow aX| bX| cY\\ Y \rightarrow aY|bY|cZ|cS\\ Z…
1
vote
1 answer

Determining the lookahead set of an $\mathrm{LL}(1)$-grammar

I would like to determine the lookahead set $\newcommand{\LA}[1]{\mathrm{LookAhead}_{#1}}\LA{1}(S)$ for the productions $P$ \begin{align*} \newcommand{\rewrite}{\longrightarrow} S &\rewrite Ab & S &\rewrite Cd & A &\rewrite aA \\ A &\rewrite…
sesodesa
  • 701
1
vote
1 answer

Help generating a grammar with {a,b,c} where the number of as and bs are different or bs and cs, or both

w ∈ C* "Generate a Grammar such that w contains strings of as followed by bs followed by cs such that there are either a different number of as and bs or a different number of bs and cs or both" C= {a,b,c} I am trying to do both. I'm having…
koby
  • 47
1
vote
0 answers

Unambiguous Context free grammar for strings in {a,b}* containing at least as many a's as b's

Taken from Sipser, 2.28. I came up with the following: $$S \rightarrow \epsilon|aS|bAs$$ $$A \rightarrow a|bAA$$ I believe this grammar generates the mentioned language, but I am not sure how to check that it does so unambiguously. Any hints?
mmmmo
  • 590
1
vote
1 answer

Find grammar for given language

I want to practice finding grammars for given languages. Unfortunately after solving some easy examples I found out that I completely don't know how to approach these more ambitious. For example when the task is to find grammar for $\left\{ a^i…
xan
  • 2,053
1
vote
1 answer

How to transition from context-free grammar $G$ to to context-free grammar which starts and ends with specific letters?

Given context-free grammar $G$ whose terminal letters are $\{a,b,c,d\}$ how can we transition to context-free grammar which contains the words from the language that $G$ creates but which start with $a$ and end with $d$? I guess we could create…
Yos
  • 2,663
1
vote
0 answers

Can redundant variables be removes in transition to Chomsky normal form grammar?

I have the following context-free grammar: $$ S\to aAb|aaBb|ab\\ A\to S|B\\ B\to C|S\\ C\to aC $$ and I need to convert the grammar to Chomsky normal form (CNF). After removing unit rules I get: $$ S\to aAb|aaBb|ab\\ A\to aAb|aaBb|ab|aC\\ B\to…
Yos
  • 2,663
1
vote
1 answer

CFG for $L=\big\{a^ib^jc^k\mid k\neq i+j\text{ and }i,j,k \ge0\big\}$

I tried to solve it by this:$$\big\{a^ib^jc^k\mid k> i+j\text{ and }i,j,k \ge0\big\}\cup\big\{a^ib^jc^k\mid k< i+j\text{ and }i,j,k \ge0\big\}$$ So, $$S_0\to S_1|S_4$$ $$\\ S_1\to S_2c \\S_2 \to S_2c|aS_2c|S_3 \\S_3 \to S_3c|bS_3c|\varepsilon$$ $$\\…
Asaf
  • 131
1
vote
1 answer

Showing that UPDOWNUP is not a context-free language

Define the formal language UPDOWNUP as $L = \{ s(reverse(s))s,s \in (a+b)^* \} = \{aaa,bbb,aaaaaa,abbaab, baabba, bbbbbb, ... \}$ I want to show using the pumping-lemma for context free languages that this language is not context free. My…
1
vote
1 answer

Eliminating Epsilon Productions from CFG

I am having a hard time figuring out how to remove the epsilon production from this context free grammar. Any help would be appreciated. CFG: (below) $S\to SAS|SBS|SaS|a|ba$ $A\to SAS|AaBB|Aba$ $B\to AAbB|BB$ $C\to a|bCa|Saaa$ $D\to…
Bryce
  • 11
1
vote
2 answers

Is this language and its complement is Context free Language?

The language $\mathcal{L}=\{a^n b^n c^n\;|\;n={1,2,3,4}\}$ is not context free from my point of view. Because no. of a's pushed in to stack is totally compensated by no of b's. popped. Please help me. Also, please tell how to deduce its complement?
Sudhir
  • 11
1
vote
1 answer

What is the grammar for the following context-free language $L=\{w|w=a^ib^ja^jb^{i}\}$?

I want to find the grammar for the following context-free language: $L=\{w|w=a^ib^ja^jb^{i}\}$ I tried \begin{align*} S&\rightarrow \varepsilon|aKb |aSb\\ K&\rightarrow\varepsilon |bKa \end{align*} I want to know if it works for any cas. It works…
1
vote
3 answers

Finding a Context Free Grammar for this language

(Paraphrasal of problem 2.48a in Sipser - Introduction to the Theory of Computation, 3rd ed.) The language $L$ consists of all binary strings with a $1$ somewhere in the middle third of the string, and I'm trying to prove $L$ is context-free by…
1
vote
1 answer

Is $\{a^m!a^n : n > 0, m > 0, n > m\}$ a context free language?

I'm trying to construct a context-free grammar for the language $L = \{a^m!a^n : n > 0, m > 0, n > m\}$, but I'm not sure how to ensure that the right trail of $a$s is longer than the left one. Is there a way I could include the left side in the…
John Hoffman
  • 2,734
1
vote
2 answers

Why is $T \Rightarrow aba$ false, but $T \stackrel{*}{\Rightarrow}$ true?

I am reading Sipser's book and encountered a question with this regular grammer: $$ R \Rightarrow XRX~|~S \\ S \Rightarrow aTa~|~bTa \\ T \Rightarrow XTX~|~X~|~\varepsilon \\ X \Rightarrow a~|~b $$ Apparently, the solutions in the back say that $T…
John Hoffman
  • 2,734