3

I have this language $L=\{a^nb^m|n\not = m; n,m \in \mathbb{N}\}$ I have to make CFG for this language but I'm clueless.

James
  • 49

2 Answers2

2

Hint. If $n > m$, then $a^n b^m = a^{n-m} a^m b^m$. If $n < m$, then $a^n b^m = a^n b^n b^{m-n}$. Therefore the strings in $L$ are obtained from the strings of the form $a^n b^n$ by adding either one or more $a$'s at the beginning, or one or more $b$'s at the end.

Luca Bressan
  • 6,845
2

Split into two cases: $m > n$ and $m < n$. Suppose $L_1 = \{a^nb^m \mid n > m\}$. We can write $L_1$ as \begin{align*} L_1 & \rightarrow a \\ L_1 & \rightarrow aL_1 \\ L_1 & \rightarrow aL_1b \end{align*} $L_2 = \{a^nb^m \mid n < m\}$ can be dealt with similarly: \begin{align*} L_2 & \rightarrow b \\ L_2 & \rightarrow L_2b \\ L_2 & \rightarrow aL_2b \end{align*} Then $L$ is the union of $L_1$ and $L_2$: \begin{align*} L & \rightarrow L_1 \\ L & \rightarrow L_2 \end{align*}

Tunococ
  • 10,303