Need to find a grammar where $w\in A^*$, where $w$ consists of $n$ $a$'s followed by $n$ $b$'s, where $n\in\mathbb N$.

75 Views Asked by At

I need to find a grammar where $w\in A^*$, where $w$ consists of $n$ $a$'s followed by $n$ $b$'s, where $n\in\mathbb N$.

I came up with the rules $$ \begin{align} S&\to aSb\\ S&\to ab\\ S&\to \epsilon \end{align} $$

Is this the right way?

1

There are 1 best solutions below

4
On

More minimal

  1. $S \to aSb$
  2. $S \to \varepsilon$

will do. $ab$ is then a two-step derivation. But yours is fine too, minus the typo and lack of MathJax. Replace the 2 by

  1. $S \to ab$

if a natural number in your definition is $\ge 1$ or not.