What is the grammar for the following context-free language $L=\{w||a|>|b|\}$?

59 Views Asked by At

I want to find the grammar for the following context-free language:

$$L=\{w|\mbox{ the number of $a>$ the number of $b$ }\}$$

I tried the following :

\begin{align*} S&\rightarrow a|aK\\ K&\rightarrow\varepsilon |(ab)^*|a^*|bS \end{align*}

But it doesn't reach words with series of $b$ such as $aaaabbba$

1

There are 1 best solutions below

2
On BEST ANSWER

Try, $$S \to AS'bS' | bS'AS' | A $$ $$S' \to AS'bS' | bS'AS' | A | \varepsilon$$ $$A \to a | aA$$

I constructed this by taking a CFG for $L = \{ w : |a| = |b| \}$, and adding an intermediate state for $a$'s.