Reputation: 1270
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.
Upvotes: 1
Views: 1694
Reputation: 2549
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 language L
(let's consider binary alphabet):
S -> ""
S -> "0" S "0"
S -> "1" S "1"
S -> "1" // odd length case
S -> "0" // odd length case
The idea in constructing G(L)
is to ensure the last symbol is equal to the first symbol in S, therefore, for every position i
the i
th symbol from the beginning is equal to the i
th symbol from the end for the word produced by that grammar.
For a word w
which is not palindrome we want to ensure there is such a position i
that the i
th symbol from the beginning is not equal to the i
th 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:
S -> "0" S "0"
S -> "1" S "1"
S -> "0" T "1"
S -> "1" T "0"
T -> ""
T -> "0" T "0"
T -> "1" T "1"
T -> "0" T "1" // we are allowed to put different symbols more than once
T -> "1" T "0" // we are allowed to put different symbols more than once
T -> "0"
T -> "1"
You can give S
the meaning of the state «we haven't put different letters yet», and T
the meaning of «we have put different letters». Note I've removed S -> ""
rule in this CFG , so we will finish only from T
, so we will definitely put different letters while producing word.
Upvotes: 2