2

Is the language $L = \{ w \mid w \in (a,b)^* \wedge w \text{ is not a palindrome} \}$, context-free? I think this grammar:

$ S \rightarrow aSa \mid bSb \mid aAb \mid bAa\\ A \rightarrow aAa \mid bAb \mid aAb \mid bAa \mid a \mid b \mid aa \mid ab \mid ba \mid bb $

generates it, but I'm unable to conclude anything using the pumping lemma for context free languages.

matheussilvapb
  • 146
  • 1
  • 7

1 Answers1

2

If $w$ is not a palindrome then there must be some $i$ such that the $i$th letter from the left is different from the $i$th letter form the right; and vice versa (prove!). This means that $$ L = \{ \Sigma^{i-1} a \Sigma^* b \Sigma^{i-1} : i \geq 1 \} \cup \{ \Sigma^{i-1} b \Sigma^* a \Sigma^{i-1} : i \geq 1 \}, $$ which is easily seen to be context-free.

Your grammar almost manages to capture this characterization. Unfortunately, it doesn't generate the words $ab$ and $ba$. A simpler grammar is $$ \begin{align*} &S \to aSa|bSb|aTb|bTa \\ &T \to aT|bT|\epsilon \end{align*} $$

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