Context free grammar for non-palindrome

。_饼干妹妹 提交于 2019-12-03 14:14:05

The construction the book I believe is shows some symmetry for better reading.

It means it first construct anything, T. Then there is a wrapper S, so that it becomes no longer a palindrome S, and then build everything upon it.

The latter might seems to intuitive. However, if you think of the definition or construction of palindrome, you might understand why writing in such way make sense.

If you have a palindrome, you would construct something like this

T -> aTa | bTb | a | b | epsilon

And if we want to violate construction, we just need to make sure that there is one layer looks like this (I use T to be one layer and S to something one step after T)

S -> aTb

And other layer we generally do not care

S -> aTa | aTb | bTa | bTb

So that forms the inner layer (T) and outer layer(R) and the layer that violates the construction of palindrome(S). Even thought T seems to be redundant, but it forms the similar construction like R, thus expressing the intention of the construction.

The definition of T in that grammer does indeed appear to be unnecessary complication. T can generate any string of as and bs, so the simpler definition would have been just as good.

I can only guess that the productions are given as they are because of the sausage-factory nature of writing a book.

ORIGINAL WRONG ANSWER:

They are not equivalent, because X itself cannot be <epsilon>, and T is not any combination of a and b. T can only expand to a palindrome (including the empty palindrome, a single character, or a palindrome with an unpaired central character).

If X could be empty, then T could expand to anything, but it can't.

NOTE

This answer is based on the supposition that the author’s intention for the production T -> XTX is that the two identical non-terminals in the substitution must represent identical strings of characters. Since I don't have the text to look at, I don't know if this assumption is well-founded except that it is motivated by the question itself. This supposition could be a mistake by the author if that is not the case elsewhere. I think that, in general, this requirement is not true of context-free grammers.

The correct productions would be:

R -> aRa | bRb | S
S -> aTb | bTa
T -> aTa | bTb | a | b | <epsilon>
Liad Ben-Yehuda

The best way to ensure that you have a grammar that generates only non-palindromes is the following: Define:

  • Pal - The language of palindromes
  • {a, b}* - The language containing all strings over the alphabet {a, b}
  • Non-Pal - The language of all strings that are not palindromes (i.e. not in Pal)

Observer that non-Pal = {a, b}* - Pal

The grammar for Pal is know to be the following:

  • S -> lambda | a | b | aSa | bSb

The grammar for {a, b}* can be written as follows:

  • S -> lambda | Sa | Sb

Now to construct the grammar of non-Pal observe the following:

  • If x is an element of non-Pal then:
    • axa is an element of non-Pal
    • bxb is an element of non-Pal
  • If y is an element of {a, b}* then:
    • ayb is an element of non-Pal
    • bya is an element of non-Pal

Combining all this information the grammar for non-Pal would be:

  • S -> aSa | bSb | aAb | bAa
  • A -> lambda | Aa | Ab

I hope this clarifies things

I found this definition of a non-palindrome quite intuitive. I assume that the author started with a definition for a palindrome

R -> aRa | bRb | a | b | <epsilon>

and now asked, how this definition can be "ruined".

That is, he unfolded the definition three times, exchanged one aRa | bRb by aRb | bRa and generalized the remaining productions to (a|b)R(a|b).

Any Non Palindrome can be split along middle , such that x(k) != x(k+ n)

n= half length x(i) = character at i th position

Keeping that in mind a simple solution would be

R  -> aRa | bRb | T
T  -> aSb | bSa
S  -> aRa | bRb | a | b | T | episoln

It can generate all non palindromes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!