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 $\}$.