Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context free grammar for non-palindrome

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 equal
  • R 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?

like image 933
Abhijith Madhav Avatar asked Jun 27 '11 15:06

Abhijith Madhav


People also ask

Are all palindromes context-free?

Although the set of palindromes is not a regular language, it is a context free language.

What is the difference between CFG and BNF?

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.

Is palindrome a CFG?

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).

How do you know if a language is context-free but not regular?

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.


3 Answers

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

like image 155
Liad Ben-Yehuda Avatar answered Sep 17 '22 20:09

Liad Ben-Yehuda


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>
like image 24
Jeffrey L Whitledge Avatar answered Sep 20 '22 20:09

Jeffrey L Whitledge


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.

like image 45
darwinsenior Avatar answered Sep 20 '22 20:09

darwinsenior