# Is my proof for a context free language correct? Same number of a's as b's

I have the following grammar G: \begin{align*} &S \to aB|bA \\ &A \to a|aS|bAA \\ &B \to b|bS|aBB \end{align*}

I am going to prove that this language L(G) consists of words with the same number of a's and b's by strong induction.

Inductive hypothesis: $S$ produces $m$ $a$'s and $m$ $b$'s, $A$ produces $m+1$ $a$'s and $m$ $b$'s, $B$ produces $m$ $a$'s and $m+1$ $b$'s for some integer $m$.

Base: The smallest string produced by $S$ is $ab$ or $ba$. The smallest string produced by $A$ and $B$ is $a$ and $b$ respectively. All three satisfy the IH.

Induction: Assume the inductive hypothesis is satisfied for all strings of length $k$ or less.

For strings of length $k+1$:

(1). $A\to aS$; then $S$ inductively derived a string of length $k-1$ with same number of $a$'s as $b$'s. Since $A$ is initiated by $S\to bA$, we concatenate that $b$ plus the $a$ in $aS$ and we get a string of length $k+1$ with same number of $a$'s as $b$'s.

(2). $A\to bAA$; then each $A$ on the right hand side has one more $a$ than $b$. Since $A$ is initiated by $S\to bA$, we concatenate that $b$ plus the $b$ in $bAA$ and we get a string of length $k+1$ with the same number of $a$'s as $b$'s.

Can I continue for $B\to bS$ and $B\to aBB$? Am I totally wrong here?

First of all, you've never said what exactly you are trying to prove. From the title, one might guess that you're trying to prove that the given grammar generates the language $L = \{ w \in (a+b)^* : \#_a(w) = \#_b(w) \}$. However, it seems that you are only trying to prove that every word generated by the grammar is in $L$. (The other direction is even harder.)

In order to prove your claim, you strengthen the claim, trying to prove the following:

1. If $S$ generates $w$ then for some $m \in \mathbb{N}$, $\#_a(w) = \#_b(w) = m$.
2. If $A$ generates $w$ then for some $m \in \mathbb{N}$, $\#_a(w) = m + 1$ and $\#_b(w) = m$.
3. If $A$ generates $w$ then for some $m \in \mathbb{N}$, $\#_a(w) = m$ and $\#_b(w) = m + 1$.

In order to prove the strengthened claim, you need to prove by induction some claim $P(m)$. However, you never state what $P(m)$ is. Your "inductive hypothesis" is actually the statement that you're trying to prove by induction. One way to interpret your inductive hypothesis is the following: $P(m)$ states that

1. If $S$ generates $w$ then $\#_a(w) = \#_b(w) = m$.
2. If $A$ generates $w$ then $\#_a(w) = m + 1$ and $\#_b(w) = m$.
3. If $A$ generates $w$ then $\#_a(w) = m$ and $\#_b(w) = m + 1$.

However, this is simply false. You need to come up with a predicate $P(m)$ that you could prove by induction. Once you've done that, you can check whether your proof works.

The rest of your proof contains several other problems, but let's start with the more major problems mentioned above.