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 one S
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?
Although the set of palindromes is not a regular language, it is a context free language.
BNF and CFG (Context Free Grammar) were nearly identical. BNF may be a meta-language (a language that cannot describe another language) for primary languages. The symbol ::= means “may expand into” and “may get replaced with.” In some texts, a reputation is additionally called a non-terminal symbol.
The CFG for palindromes is straightforward: S → aSa | bSb | a | b | ϵ. Next, modify the grammar to keep track of the number of a's by creating different lev- els in the CFG to represent the different values of “mod 3” (while counting a's).
We can prove that a language is context-free if we construct a context-free grammar that generates it. Alternatively, we can create a pushdown automaton that recognizes the language. On the other hand, we use Ogden's lemma and the pumping lemma for context-free languages to prove that a language isn't context-free.
The best way to ensure that you have a grammar that generates only non-palindromes is the following: Define:
Observer that non-Pal = {a, b}* - Pal
The grammar for Pal is know to be the following:
The grammar for {a, b}* can be written as follows:
Now to construct the grammar of non-Pal observe the following:
Combining all this information the grammar for non-Pal would be:
I hope this clarifies things
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With