Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem in constructing a Context-free Grammar for the Language $$L = \{a^mb^n : m≠n,m>0,n>0\} .$$ Though I can able to construct a Pushdown Automata.


I can construct a CFG, but it accepts both $m \neq n$ and $m=n$. My CFG is:

$S \to aS/Sb/aSb/aab/abb $.

I cannot able to consturct the CFG to accept for $m \neq n$ alone.

share|cite|improve this question
up vote 3 down vote accepted

How about

S ::= aSb | A | B
A ::= a | Aa
B ::= b | Bb
share|cite|improve this answer
This will accept if my input is 'a' but the condition is both m,n>0 – EAGER_STUDENT Sep 18 '11 at 16:02
@EAGER_STUDENT it is trivial to modify Henning's answer to make both m,n > 0. Just add T ::= aSb as the first production rule (and always start at T, this will force us to have at least one a and b. – Artem Kaznatcheev Sep 18 '11 at 16:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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