# Context free grammar for language with even number of $0$'s and $1$'s

I want to create a Context-Free grammar that generates the language

$$L = \{ w \in \{0, 1\}^* |\ \text{number of 0's is even, and number of 1's is also even} \}.$$

I came up with

$$S \rightarrow 0S0S\ |\ 1S1S\ |\ ABABS\ |\ BABAS\ |\ \epsilon \\ A \rightarrow 0A0A\ |\ 1A1A\ |\ 0\\ B \rightarrow 0B0B\ |\ 1B1B\ |\ 1\\$$

Looks like it does the job, but how can I be sure that it does indeed generate L.

Any help would be appreciated.

• Have you considered accepting an answer? A question without an accepted answer indicates that the asker does not consider any answer works and the asker is still waiting for another better answer. The question is viewed as unresolved. (This comment will be deleted upon feedback.) Oct 12, 2022 at 17:41

how can I be sure that it does indeed generate $$L$$?

You should show both of the following. There are a variety of methods to choose. Mathematical induction or structural induction is usually involved.

• It generate every string in $$L$$.
• It does not generate any string that is not in $$L$$.

It turns out

• Your grammar does not generate 010001, a string in $$L$$. This non-generation is not hard to prove by hand, although it can take some time. Basically, you can make lots of small observations on the generation rules such as $$A$$ becomes either 0 or at least 4 symbol long. There are also efficient algorithms to compute whether a string can be generated by a given context-free grammar.
• Your grammar generates 1010000, which is not in $$L$$.
• $$A\Rightarrow 0A0A\Rightarrow^*0000$$.
• $$S\Rightarrow BABAS \Rightarrow^* 101A\Rightarrow^*1010000$$

It looks like that some of the common/basic intuition to create context-free grammars (CFG) or regular grammars might be helpful for you.

• Each of the non-terminals in a CFG stands for a context-free language. Try to formulate each of them in plain English (or in your favorite model). Try to describe each rule in plain English as well.
• There are some basic tools you would like to know.
• If the conditions on the strings can be computed in bounded memory, then the language is regular. In the current case, $$L$$ is a regular language since there are only finitely many combinations of the number of 0's in a string modulo 2 and the number of 1's in it modulo 2. In fact, there are only $$2\times2=4$$ combination.

Let $$E_s$$ be the language of words that are equivalent to the string $$s$$ where a string $$w$$ is equivalent to $$s$$ if the number of 0's or 1's in $$w$$ is of the same parity as the number of 0's or 1's in $$s$$, respectively. For example, since there is an odd number of 0's and an even number of 1's in string $$0$$, $$E_0$$ is the language of words with an odd number of 0's and an even number of 1's.

It is easy to construct the following grammar that can be proved easily to generate the given language, where $$E_s$$ is abused as a non-terminal for $$s=\epsilon, 0,1,$$ and $$01$$ with $$E_\epsilon$$ viewed as the starting symbol.

$$\begin{array}{lllllll} E_\epsilon &\to &0E_0 &\mid &1E_1 &\mid &\epsilon\\ E_1 &\to &0E_{01} &\mid &1E_\epsilon &\\ E_0 &\to &0E_\epsilon &\mid &1E_{01} &\\ E_{01} &\to &0E_1 &\mid &1E_0 &\\ \end{array}$$

Exercise. Write a grammar for $$\{ w \in \{0, 1\}^*$$ | the difference of the number of 0's in $$w$$ and the number of 1's in $$w$$ is odd $$\}$$.

• Thanks for your answer! The grammar for your exercise I think is the same as the one you presented, except that $\epsilon$ is generated by $F_{0,1}$ and $F_{1,0}$ instead of $F_{0,0}$. Jun 26, 2019 at 13:20

The most common approach to show that two sets are the same is to show that they are subsets of each other.

In this case, that means (1) every string with an even number of $$0$$s and $$1$$s has a derivation in the grammar, and (2) every derivation of the grammar has an even number of $$0$$s and $$1$$s.

In attempting (2), a reasonable approach is to prove inductively, from smaller strings to bigger strings.

For example, considering $$S \to 0S0S$$, since the number of $$0$$s increases by 2 and the number of $$1$$s stays the same, the resulting string has the desired property when the smaller $$S$$ strings in the right hand side do.

However, I think you'll find the $$S \to BABAS$$ case hard to prove correct, which suggests that's where you need to look for a counterexample. Indeed, here's one:

0101010 = (B:0)(A:1)(B:0)(A:1010)(S:) = (B:0)(A:1)(B:0)(A:(1(A:0)1(A:0)))(S:)


The non-terminals we'll write $$[xy]$$ with $$x = 0$$ for an even number of $$0$$, $$x = 1$$ for odd number of $$0$$, similarly $$y$$ for $$1$$s. Set up so that $$[xy]$$ generates strings with the required parities: You get an odd number of $$0$$ by an even number of $$0$$ and an additional $$0$$, and so on. The grammar is then:

\begin{align*} [00] &\to [10] 0 \mid [01] 1 \mid \epsilon \\ [01] &\to [11] 0 \mid [00] 1 \mid 1 \\ [10] &\to [00] 0 \mid [11] 1 \mid 0 \\ [11] &\to [01] 0 \mid [10] 1 \end{align*}

Rename the non-terminals as letters, and you are done.

Note that this grammar is regular, you can get a finite automaton for this (regular) language.

Don't overthink!