3

I know the grammar for the language $\{ xy : |x| = |y|, x ≠ y \}$ if $\Sigma=\{a,b\}$:

$$ \begin{align*} &S→AB∣BA \\ &A→a∣aAa∣aAb∣bAa∣bAb \\ &B→b∣aBa∣aBb∣bBa∣bBb \end{align*} $$

I know this is a grammar, but I need a PDA for this language, and intuition how $\{xy: |x|=|y|,x \neq y\}$ is a CFL while $\{xy: |x|=|y|,x=y\}$ is a CSL but not a CFL. How is this possible?

Raphael
  • 73,212
  • 30
  • 182
  • 400

1 Answers1

5

You are asking two questions: how to construct a PDA for the language, and why this language is context-free while the same language with the condition $x \neq y$ replaced by the condition $x = y$, is not. I will only answer the second, since for the first question there are known algorithms.

The reason that your language is context-free is that we can rewrite it as follows: $$ \{ \Sigma^i a \Sigma^i \Sigma^j b \Sigma^j : i,j \neq 0 \} \cup \{ \Sigma^i b \Sigma^i \Sigma^j a \Sigma^j : i,j \neq 0 \}. $$ This gives a different description of the language as the union of concatenations of context-free languages. A similar trick just doesn't work for the language $\{ xy : x=y \}$.

Here is a different example. Consider the following two collections of natural numbers (without zero):

  • $A = \{ x \cdot y : x \neq y \}$.
  • $B = \{ x \cdot y : x = y \}$.

The set $A$ consists of all natural numbers other than $1$, whereas the set $B$ consists of all squares. Even though we defined them in a similar way, the set $A$ has a much simpler description, while $B$ doesn't.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514