CFG for all strings of a’s and b’s that contain a different number of a’s and b’s

I am trying to write CFG for all strings on {a,b} that contains different numbers of a’s and b’s? After two hours of brainstorming, I came up with this:

S→A|B
A→aE|aA|EA
B→bE|bB|EB
E→aEbE|bEaE|Λ

I tried running few strings on this, it worked, But then I run string "abbab" and it failed.
Kindly someone point out at my mistake or tell me the correct CFG for this as I don't know how to correct this.

In fact, your grammar is correct and clean!

Here is how your grammar generates $$abbab$$.

$$S\Rightarrow B\Rightarrow EB\Rightarrow^* abB \Rightarrow abbE\Rightarrow^* abbab$$

Exercise. Show your grammar is correct.

• Oh wow, thanks, didn't think of it. By the way, I have one more question, Do I have to delete this post now or what? Because it is not a question anymore, as my CFG was already correct. – Tom Jun 2 '19 at 16:59
• That's actually wrong. You shouldn't delete a question which has been answered. If you delete it, it will get undeleted if anybody notices. – Yuval Filmus Jun 2 '19 at 18:48
• @YuvalFilmus Thanks, didn't know this. – Tom Jun 2 '19 at 19:06
• What's important about a question is that it is an interesting problem that is likely to help others (when combined with answers), and your question is that. Anyone wanting to know how to construct the CFG will be helped by this question. – gnasher729 Jun 2 '19 at 21:52

Here is a (hopefully) more principled approach. Let us start with a grammar for the language of all strings containing the same number of $$a$$'s and $$b$$'s ("balanced"). We can identify such strings as walks in which $$a$$ gets translated to $$\nearrow$$ and $$b$$ gets translated to $$\searrow$$. We concatenate these arrows horizontally. For example, $$aabbab$$ is the walk $$\begin{array}{cccccc} & \nearrow & \searrow \\ \nearrow & & & \searrow & \nearrow & \searrow \end{array}$$

A walk is balanced if it ends up in the Y axis. For every such walk, one of the following cases must hold:

• The walk is empty.
• The walk reaches the Y axis at some point in the middle.
• The walk doesn't reach the Y axis at any point in the middle.

A walk of the second type is a concatenation of two balanced walks, and conversely, the concatenation of two balanced walks is balanced. A walk of the third type is of the form $$\nearrow W \searrow$$ or $$\searrow W \nearrow$$, where $$W$$ is a balanced walk, and conversely, walks of these forms are balanced. This shows that the following grammar generates all balanced walks: $$W \to \epsilon \mid WW \mid aWb \mid bWa$$

What about unbalanced walks? We will consider walks with more $$\nearrow$$s than $$\searrow$$s. There are at least two ways of approaching this problem:

1. For each such walk, we can consider the last time that the walk reaches the origin. That part of the walk is balanced, and then we must have $$\nearrow$$, followed either by another balanced walk, or by another walk with more $$\nearrow$$s than $$\searrow$$s; and conversely, any walk of these two types is "$$\nearrow$$-unbalanced".

2. For each such walk, we can remove $$\nearrow$$s until we get a balanced walk. Conversely, starting with a balanced walk, if we add at least one $$\nearrow$$ anywhere, we get an $$\nearrow$$-unbalanced walk.

A grammar capturing the first type of structure is $$A \to WaW \mid WaA$$ A grammar capturing the second type of structure is a bit more complicated to state. Let us start with a grammar for at least as many $$a$$ as $$b$$s: $$A \to E \mid EAEAE \mid EaEAEbE \mid EbEAEaE \\ E \to aE \mid \epsilon$$ Here $$E$$ means "optional $$a$$s". In order to convert it to a grammar of more $$a$$ than $$b$$s we need to add a new symbol $$A'$$ which enforces adding at least one $$a$$: $$A' \to aE \mid aEAEAE \mid EA'EAE \mid EAaEAE \mid EAEA'E \mid EAEAaE \mid \cdots$$ (We let the reader complete the definition.)

Summarizing, if we choose option 1, then we get the following grammar: $$S \to A \mid B \\ A \to WaW \mid WaA \\ B \to WbW \mid WbB \\ W \to \epsilon \mid WW \mid aWb \mid bWa$$