I need help with constructing a CFG for a given language.
L = { x ∈ {a, b}* | x != x reversed }, in other words, L is the complement of all the palindromes in L.
I’m more interested in how to approach these kind of problems rather than the specific solution.
Well, I didn’t figure the common pattern for solving such problems yet, but I think I know how to solve this problem:
First consider the CFG
G(L)for palindrome languageL(let’s consider binary alphabet):The idea in constructing
G(L)is to ensure the last symbol is equal to the first symbol in S, therefore, for every positionitheith symbol from the beginning is equal to theith symbol from the end for the word produced by that grammar.For a word
wwhich is not palindrome we want to ensure there is such a positionithat theith symbol from the beginning is not equal to theith symbol from the end. So, let’s terminate word production only after we have put different letters in the beginning and the end of production:You can give
Sthe meaning of the state «we haven’t put different letters yet», andTthe meaning of «we have put different letters». Note I’ve removedS -> ""rule in this CFG , so we will finish only fromT, so we will definitely put different letters while producing word.