Language to Context Free Grammar

89 Views Asked by At

Trying to study some context free grammar but I am currently very confused on how to solve the following question.

Which grammer generates the following language: $$\{w0^{n+1}1^n|w\in\{1,0\}^* \text{ and }n\ge0\}$$

A beginner friendly explanation on a solution would be very much appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

I would break the words into three parts: the $w\in\{0,1\}^*$, a single $0$, and a word of the form $0^n1^n$ for $n\ge 0$. Then if $S$ is your initial symbol, you can begin with a production $S\to A0B$, where the plan is to make $A$ produce $w$ and $B$ produce $0^n1^n$.

The language $\{0,1\}^*$ is regular, and you shouldn’t have too much trouble figuring out a grammar with $A$ as initial symbol that generates it; I’ll leave that to you for now. For the $0^n1^n$ part you use a standard trick: build from the middle out. Two productions take care of the whole thing, in fact: $B\to\lambda$ and $B\to 0B1$. (I use $\lambda$ for the empty word; many people use $\varepsilon$, and you may be more familiar with that usage.

A final comment: you can get rid of the $\lambda$ productions by adding three extra productions from $S$. One of them is $S\to A0$, and the others, which I’ll leave to you for now, are similar.