# Determine whether two languages are context free

(1) $L_1 = \{a^ib^{i+j}c^j|i,j\geq 0\}$

(2) $L_2 = \{xy | x,y \in \{0,1\}^*, x \neq y, |x| = |y| \}$

I doubt that $L_1$ is CFL. I've been trying to go with the string $s$ = $a^pb^{2p}c^p$. Thus, we can write $s$ as $uvxyz$. Now I'm trying to show $uv^2xy^2z$ is not in $L_1$.

I really have no idea how to approach (2). And if it is context free, how in the world would you draw a pda?

• – D.W. Oct 2 '13 at 23:45

For the first one, $$L_1 = \{a^i,b^{i+j}c^j \mid i,j \geq 0 \}$$

Here's an rough idea of the algorithm:

• read off the all '$a$'s and push $a$ into the stack
• upon seeing a '$b$', pop the '$a$'s till the stack is empty
• then again as long as reading '$b$'s keep pushing $b$ into the stack
• on seeing a $c$, start popping the stack until empty.

For the second one,

Please see my answer here, The other answer is equally good (perhaps even better and simpler) than mine.

• $q0$ is also a final state, apologies, i missed it out while drawing it... – Subhayan Sep 22 '13 at 23:12
• oh. very clear. you didn't even need to do the actual pda diagram. so bonus points for that. – user678392 Sep 22 '13 at 23:23
• i have posted the final answer at the link Gilles provided. and i think he's right, you should try to read his desc. and construct your own, and i am sure you can do that :) – Subhayan Sep 22 '13 at 23:48
• Could you please explain why these steps work? we 'copy' the input tape onto the stack (q1) and leverage non-determinism for the check we keep reading off the stack and uninterestingly read while we keep encountering the same variables (q2) if the tape becomes empty, we just don't accept the language (q3) if we see even one position, where there's a difference, we are happy, and read the rest of the string (q4) and check if |x|=|y| – user678392 Sep 23 '13 at 0:02
• @user678392 please see this. I am pretty confident this is correct. Do you want me to cross post the answer here too? – Subhayan Sep 23 '13 at 7:23

$L_1$ is context-free. It is fairly easy to write a grammar for it: \begin{align} S &\to A C \\ A &\to \epsilon \\ A &\to a A b \\ C &\to \epsilon \\ C &\to b C c \\ \end{align}

Intuitively, $L_1$ needs to remember the number of $a$'s, then when a matching number of $b$'s have been found it needs to remember the excess, and compensate by $c$'s. This is within the capabilities of a pushdown automaton (push a token per excess $a$, pop one when a $b$ comes, then start pushing $b$'s when the stack is empty and pop them when $c$'s come).

$L_2$ is also context-free, but this is more difficult to see. Try reasoning by induction and deducing a grammar, but be warned that it isn't straightforward. Start with the idea that either $x$ and $y$ start with the same letter, in which case the rest of the words must be different, or $x$ and $y$ start with the same letter, in which case the rest of the words can be arbitrary. This is not enough, because you can't keep track of the fact that you're decomposing into words of equal length. So you need to do more to ensure that you aren't including words of the form $x x$ that you happened to break down in the wrong place. See Show that { xy ∣ |x| = |y|, x ≠ y } is context-free for a solution.

• how does this translate into a pda? Can you reword your explanation on L_1. Also, how does the pda work if j = 0 or i,j = 0? – user678392 Sep 22 '13 at 23:03
• @user678392 My intuitive explanation pretty much tells you how to construct a PDA. I've done about 90% of your homework exercise here, you should at least do the final step by yourself. – Gilles 'SO- stop being evil' Sep 22 '13 at 23:11
• Given that I don't understand the explanation or are confused by it, I would beg to differ. – user678392 Sep 22 '13 at 23:20
• you do realize that the solution you linked to is horrendous to understand. – user678392 Sep 22 '13 at 23:25
• @user678392 $L_1$ is an easy exercise, you've had everything handed to you. $L_2$ is a difficult exercise, if you have tried for yourself and don't understand the solution, feel free to ask a new question where you tell us how far you got and where you're stuck. – Gilles 'SO- stop being evil' Sep 22 '13 at 23:28

Another simple way to see that (1) is a context-free is to observe that both $L'_1=\{a^ib^i\mid i\ge 0\}$ and $L''_1 = \{b^jc^j \mid j\ge 0\}$ are context-free, and $L_1$ is simply $L'_1\cdot L''_1$, the concatenation of $L'_1$ and $L''_1$. And we know that the set of context-free languages is closed under concatenation.