1

I want to build a context sensitive grammar for the language $\{a^{2^n}\mid n\geq 0\}$. I think it should be something like this \begin{align*} S &\to aA \mid a\\ aA&\to aaaA \mid aa \end{align*}

David Richerby
  • 82,470
  • 26
  • 145
  • 239
unnamed
  • 65
  • 2
  • 7

4 Answers4

2

I just had this as a homework and would like to share an IMO much simpler and more intuitive solution than the one from the textbook.

$$ \begin{align*} S&\rightarrow EAE\ |\ a\ |\ aa\\ EA&\rightarrow E2A\ |\ TA\\ 2A&\rightarrow AA2\\ 2AE&\rightarrow AAE\\ TA&\rightarrow aaaaT\\ TAE&\rightarrow aaaa \end{align*} $$

$E$ marks beginning and end. $2$ marks that we do another doubling of our number of $A$s and whenever a $2$ is inserted, it then "walks" over the whole string, doubling all $A$s it passes. $T$ is used to translate the $A$s into $a$s. We translate $A$ to $aaaa$ instead of $a$, so that our grammar is context sensitive.

Keno
  • 53
  • 4
2

This is from Hopcroft and Ullman's book, Example 9.4, pg 220:

$$ S \rightarrow ACaB $$ $$ Ca \rightarrow aaC $$ $$ CB \rightarrow DB $$ $$ CB \rightarrow E $$ $$ aD \rightarrow Da $$ $$ AD \rightarrow AC $$ $$ aE \rightarrow Ea $$ $$ AE \rightarrow \epsilon $$

Update: in fact given grammar is unrestricted which can be modified as a context sensitive.

But if your ultimate goal is to prove that the given language is a CSL, then it is enough to create a linear bounded automaton - a TM whose computational ability is restricted to the portion of input. In other words, the head of the TM cannot move beyond the input area. LBA's and CSG's are equivalent.

You can just erase one input symbol at a time starting from the right, and each time you erase one symbol increase a counter by one (binary symbols 0 and 1) which are stored in place of erased symbols. When you erase all input symbols check if the counter is of the form 100...0. If yes then accept, otherwise reject). 100...0 means it is a power of 2.

enter image description hereenter image description here

fade2black
  • 9,905
  • 2
  • 26
  • 36
1

The shortest non-contracting grammar we could come up with is: \begin{align*} S&\rightarrow a\ |\ LR\\ L&\rightarrow LD\ |\ D\\ Da&\rightarrow a^2D\\ DR&\rightarrow aR\ |\ a^2 \end{align*}

  • L marks the left side of the word, produces the doubling symbol D, and can only be eliminated by conversion to a D.
  • D is moved through the word from left to right, doubles each a and can only be eliminated on an R.
  • R marks the right side of the word and also acts like an a, to accomplish non-contraction in the rules. It can only be eliminated with a D.

The shortest non-trivial derivations are: \begin{align*} S\rightarrow LR\rightarrow DR\rightarrow a^2\\ S\rightarrow LR\rightarrow LDR\rightarrow DDR\rightarrow DaR\rightarrow a^2DR\rightarrow a^4 \end{align*} Hence, the number $i$ of D's produced with the rule $L\rightarrow LD$ determines which word $a^{2^i}$ is generated by the grammar.

Athlici
  • 11
  • 1
0

How do you express the rule $Da\rightarrow aaD$ in terms of valid rules of CSG? I mean, rules like $\alpha A\beta\rightarrow\alpha\psi\beta$.