# Context sensitive grammar for $\{a^{2^n}\mid n\geq 0\}$

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*}

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.

• Is this really context sensitive? There is CB -> E which i think should not be in context sensitive grammar – unnamed Jun 4 '17 at 19:44
• No, what I have sent is unrestricted grammar. But you can modify it and rewrite as CSG (which is given in the example 9.5 on pg. 224 of the same book), which is not trivial. Note also "Almost any language one can think of is context sensitive; the only known proofs that certain languages are not CSL's are ultimately based on diagonalization" – fade2black Jun 4 '17 at 20:03
• can you show me how to transform the given grammar in context sensitive ? – unnamed Jun 4 '17 at 21:04
• See original post's update. – fade2black Jun 4 '17 at 21:26
• But the language should accept also the word a, when n is 0 – unnamed Jun 5 '17 at 11:58