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'm trying to construct a context-free grammar for the language $L = \{a^m!a^n : n > 0, m > 0, n > m\}$, but I'm not sure how to ensure that the right trail of $a$s is longer than the left one.

Is there a way I could include the left side in the right side using context-free grammar?

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

We could look at the language as strings of the form $a^n!a^ka^n.$ Guided by this insight we could use the CFG with productions $S\rightarrow aSa\ |\ a!Ta,\quad T\rightarrow aT\ |\ a$. We're generating strings of the form $a^n\dots a^n$ and then finishing off by placing $!a^k$ in the middle. This idiom of "constructing the outside part and then finishing with the interior" is fairly common in CFG construction.

share|cite|improve this answer

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.