What is the grammar for language $L = \{ a^nb^m : n\neq m-1\}$?

I only know I have to write grammar for both $ n<m-1 $ and $ n>m-1 $, so this is what I wrote:

  • For $n<m-1$: \begin{align} &S\to Abb \\ &A\to aAb \mid \lambda \end{align}

  • For $n>m-1$: \begin{align} &S\to aaA \\ &A\to aAb \mid \lambda \end{align}

Yet I can not mix them and get a correct grammar for $L$.

  • $\begingroup$ What makes you think that there's a context-free grammar for it? $\endgroup$
    – Raphael
    Jan 19 '18 at 13:43
  • $\begingroup$ @Raphael As the question seems to distinguish $<$ and $>$ it seems we have to read $!$ as negation, rather than as factorial. $\endgroup$ Jan 19 '18 at 14:18
  • $\begingroup$ PS. I took the liberty of editing both $!=$ and landa. $\endgroup$ Jan 19 '18 at 14:21
  • $\begingroup$ @HendrikJan Ahhh, good point. Thanks! $\endgroup$
    – Raphael
    Jan 20 '18 at 0:38

If you split the grammar into two parts for disjoint parts of the language you better take different names for the variables, since they have a different meaning in the two parts.


In your first grammar, change S to X and leave A unchanged. In your second grammar, change S to Y and A to B. Then add S -> X | Y.

And fix your grammars to handle n < m-1 instead of n=m-2 and n>m-1 instead of n = m+2.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.