# Context free grammer for $L = \{a^i b^j a^n b^m \mid i + j = n + m\}$

Find a CFG for the following language:

$$L = \{a^i b^j a^n b^m \mid i + j = n + m\}$$

I'm not sure how I can do that context free. I know that I have to borrow one char from the left group for each char from the right group. but not sure how.

• Welcome to Computer Science! What have you tried? Where did you get stuck? We do not want to just hand you the solution; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See here for tips on asking questions about exercise problems. If you are uncertain how to improve your question, why not ask around in Computer Science Chat?
– Raphael
Jan 15 '17 at 12:06
• With an automaton, it's easy. You use the stack to count (in unary) and accept when it's empty. And you remind yourself which part you're reading with states. Jan 15 '17 at 13:03
The idea is to use non-terminals $X_{\alpha\beta}$, where $\alpha,\beta \in \{a,b\}$, that support the production $X_{\alpha\beta} \to \alpha X_{\alpha\beta} \beta$. You have to combine them somehow so that you obtain the language $L$, but I'll leave the details to you.