问题
I need a CFG which will generate strings other than palindromes. The solution has been provided and is as below.(Introduction to theory of computation - Sipser)
R -> XRX | S
S -> aTb | bTa
T -> XTX | X | <epsilon>
X -> a | b
I get the general idea of how this grammar works. It mandates the insertion of a sub-string which has corresponding non-equal alphabets on its either half, through the production S -> aTb | bTa
, thus ensuring that a palindrome could never be generated.
I will write down the semantics of the first two productions as I have understood it,
S
generates strings which cannot be palindromes because their 1st and last alphabets are not equalR
consists of at-least oneS
as a sub-string ensuring that it is never a palindrome.
I don't completely understand the semantics of the third production, i.e. .
T -> XTX | X | <epsilon>
X -> a | b
The way I see it, T can generate any combination of a and b, i.e. {a, b}*. Why could it not have been like
T -> XT | <epsilon>
X -> a | b
Aren't the two equivalent? As the later is more intuitive, why isn't it used?
回答1:
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.
回答2:
The definition of T
in that grammer does indeed appear to be unnecessary complication. T
can generate any string of a
s and b
s, 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>
回答3:
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
回答4:
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)
.
回答5:
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
来源:https://stackoverflow.com/questions/6495154/context-free-grammar-for-non-palindrome