0

Context-free grammar can generate the string a 2 n for n≥0 .

The production rule P is S→SS|a .

The derivations is, for example:

1) S⇒a (this is when n = 0)

2) S⇒SS⇒aa (this is when n = 1)

3) S⇒SS⇒SSSS⇒aaaa (this is when n = 2)

4) S⇒SS⇒SSSS⇒SSSSSSSS⇒aaaaaaaa (this is when n = 3).

Am I right?

Someone told me yes but it takes a very long time for big enough n;

someone told me no - that this grammar actually generates $a^+$ because it can be $S \Rightarrow SS \Rightarrow SSS \Rightarrow aaa$.

So, does this mean context-free grammar cannot generate the string $a^{2^n}$?

Is there exist any other context-free grammars that can generate this language?

kate
  • 327
  • 2
  • 10

2 Answers2

5

It is well known (and not very difficult to prove) that a context-free language over a unary alphabet $\{a\}$ is regular.

Thus, your question is essentially, "is $\{a^{2^n}:n\in \mathbb N\}$ regular?" And the answer to that is no (easy to prove using the pumping lemma).

Shaull
  • 17,814
  • 1
  • 41
  • 67
-1

I just realize that my question is the duplicate of this question, but I'll try to put additional answer here myself, now knowing that $a^{2^n}$ is a context-sensitive language, not a context-free language.

An example of the production rules to generate $a^{2^n}, n \geq 0$:

$S \rightarrow \lambda |AA$

$A \rightarrow AA$

Until the above part, it's okay with context-free grammar rules (the derivation being beautifully $S \Rightarrow AA \Rightarrow A^{2^2} \Rightarrow A^{2^3} \Rightarrow \dots$), but then how to change the non-terminal symbols $A$ to the terminal symbols $a$ is tricky. We cannot simply apply $A \rightarrow a$ because it can affect the numbers for example $S \Rightarrow AA \Rightarrow aAA \Rightarrow aaa$, which is not $a^{2^n}$.

This is where the context-sensitive grammar rule comes in handy, simply change both $AA$ to terminal symbols thus preserve the number of $a$'s generated as $a^{2^n}$, i.e.

$AA \rightarrow aa$.

The need to apply this rule is why this language is not a context-free language.

kate
  • 327
  • 2
  • 10