My answer might come late. I hope it is yet useful for you (or other readers).
S -> a
S -> aIIIE
Finish counting:
aII -> aaFI
aFII -> aaFI
aFIE -> aa
Going on counting:
Produce "a"s and "J"s
aII -> aCaLPI
PII -> aLPI
PIE -> aRRRE
Move ALL "a"s to the left:
La -> aL
LR -> RR
Ca -> aC
Convert "R"s to "I"s:
CR -> IC
CE -> E
Names for states:
S : Start
E : "End"
I : "1"
F : "Finish"
P : "Produce"
L : "Left"
R : "Right"
C : "Convert"
Explanation:
Let me also sketch the idea of the solution.
Square numbers are always the sum of a sequence of odd numbers.
E.g. 2^2=1+3=4, 3^2=1+3+5=9, and so on.
Mathematically: "sum over 2*k-1 of k between 1 and n" = n^2
What you actually have to do is only counting over the odd numbers.
That is easier said than to be done.
My grammar works like this:
On the left hand side, you have the previous result.
The next odd number is indicated by the odd number of the same Non-Terminal ("I" in my case).
So I count like aIIIE, aaaaIIIIIE, and so on.
Whenever you achieved that state you always decide whether to go on or finish counting.
When you go on counting you need to produce I-times an "a" AND AT THE SAME TIME (I+2)-times "I"s again.
However "I"s and "a"s will be mixed in their order.
Therefore you must introduce some mechanism that moves all "a"s to the left
(and all "I"s to the right by that).
Moreover you must always limit the freedom of producing words
so that your "current path" cannot be left anyhow.
Otherwise you can run into a deadend with your production.
("F", "P", "L", "R", "C" , "E" serve for that.)
I want to demonstrate it with n=2 and n=3.
This should be enough.
"*->" : "produces"
n=2:
aIIIE
(aII)IE *-> (aaFI)IE
a(aFII)E *-> a(aaFI)E
aa(aFIE) *-> aa(aa)
aaaa
n=3:
aIIIE
(aII)IE *-> (aCaLPI)IE
aCaL(PII)E *-> aCaL(aLPI)E
aCaLaL(PIE) *-> aCaLaL(aRRRE)
a(Ca)LaLaRRRE *-> aaCLaLaRRRE
aaCLa(La)RRRE *-> aaCLa(aL)RRRE
aaCLaa(LR)RRE *-> aaCLaa(RR)RRE
aaC(La)aRRRRE *-> aaC(aL)aRRRRE
aaCa(La)RRRRE *-> aaCa(aL)RRRRE
aa(Ca)aLRRRRE *-> aa(aC)aLRRRRE
aaa(Ca)LRRRRE *-> aaa(aC)LRRRRE
aaaaC(LR)RRRE *-> aaaaC(RR)RRRE
aaaa(CR)RRRRE *-> aaaa(IC)RRRRE
aaaaI(CR)RRRE *-> aaaaI(IC)RRRE
aaaaII(CR)RRE *-> aaaaII(IC)RRE
aaaaIII(CR)RE *-> aaaaIII(IC)RE
aaaaIIII(CR)E *-> aaaaIIII(IC)E
aaaaIIIII(CE) *-> aaaaIIIII(E)
aaaaIIIIIE