1

Hey i have been stuck on this problem for a few days now and even going through sample problems in my textbook as well as sample solutions i cant figure out how to make this grammar work.

Give a Grammar for this language L:

L = { a^n^2 : n ≥ 0 }

I know it may be a vague question but i really could use some help figuring this one out.

Thanks in advance!

Fragmatist
  • 31
  • 4
  • i couldn't figure out how to format the powers in the language but to clarify it is a language that supports strings of a's that are n squared in length for n is greater than or equal to zero. – Fragmatist Apr 24 '14 at 14:20

1 Answers1

0

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
mmirwaldt
  • 843
  • 7
  • 17