I am trying to design a CFG that generates the language $\{a^k b^m a^n a^k \mid m \leq n\}$. However, I am having trouble with the $b^m a^n$ where $m \leq n$. How do I solve this?

  • 2
    $\begingroup$ Think of it as $b^m a^m a^k$ where $k = n-m \ge 0$ $\endgroup$
    – rici
    Jun 3, 2020 at 5:24
  • $\begingroup$ So you know you'll have to generate $P\to bPa$, except sometimes the $b$ might be missing... $\endgroup$
    – frabala
    Jun 3, 2020 at 6:11

1 Answer 1


You can write this language as $$ \{ a^k b^m a^m a^\ell a^k \mid k,m,\ell \in \mathbb{N} \}. $$ This description easily lends itself to conversion into a context-free grammar.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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