0

Give a grammar for the following language {0^n w 1^n | n>=0 w is in {0,1}* and |w|=n}

Attempt at solution:

S--> 0S1|R

R--> 0R|1R|empty

not sure how to guarantee the length of r is the same as the number of 0's or 1's.

Yvainovski
  • 314
  • 1
  • 2
  • 12

1 Answers1

2

Here goes nothing.

Every word should look like this: 0^n w 1^n. So if we have the rule S -> 0S1 we reach a state, where every sentential form we can generate from this looks like 0^n S 0^n.

Well... This is not quite what we want. isn't it?

We go a step further, we want some Variable V, involved in the rules V -> 0|1 (edit: this would have been our "goal", but things could go wrong, so we don't use these two rules), which gives us the possibility to use S -> 0SV1 instead of S-> 0S1. What's the change?

Now we get sentential forms like these: 0^n S (V1)^n. So, for example 000SV1V1V1 would be some such sentential form. One minor addition we definitely need now is the rule S -> empty

Still not quite there yet, though, after all we want it to look more like 0^nV^n1^n in the end. So we add a rule which swaps V and 1. So we add 1V -> V1 to the set of rules. What are the possibilites now? Given a sentential form like 000SV1V1V1 we can now move all the V's to the left and all the 1's to the right.

And now we become real grammar nazis. We don't want anything to go wrong, so we make some minor changes. We do a little swippidy swappidy. We swap every occurence of S we had so far with a T. So S -> 0SV1 becomes 0TV1 etcetera. Also, we add the rules S -> empty|T and we remove the rule T -> empty. What do we gain from this? Well... Nothing at first sight. BUT, now we can build a mechanism which assures that nothing can go wrong when we turn the V's into 1's and 0's.

We simply add the rules TV -> C and CV -> CC. Oh Jesus Christ, all these rules.

Now, given a sentential form 0^n T V^n 1^n, we can slowly transform it into 0^n C^n 1^n. What's the use? Nothing can go wrong, if we might not have pushed all the V's to the left. So: A sentential form like 0000CCC1V111 can do no harm to our cause, as we cannot do anything about the V, unless it's next to a C, also, we have no possibility of pushing the C's around, since there is no such rule. Also, since we're going to add the rules C -> 0|1, if we prematurely change them to 1's and 0's, we cannot finish our word if there is still a V floating around.

This might be not neccessary at all, I'm not sure about that, BUT it is part of our proof, that all the words we can create are in the set of words we want to specify with this grammar. The rules are:

S -> empty | T
T -> 0TV1
1V -> V1
TV -> C
CV -> CC
C -> 0|1

Edit: This is a Type-0 Grammer, though. With some changes, this can become an equivalent CSG, though:

S -> empty | T
T -> 0TV1 | 0C1
1V -> V1
CV -> CC
C -> 0 | 1

The main difference is, that we at some point can decide to stop adding 0TV1 to the sentential form, and instead finish up with 0C1, getting a form like 0^n C1 (V1) ^ (n-1). And again, if we prematurely transform all C's into 0's and 1's, we lose the possibility to remove all V's. So this should also generate the set we're looking for.

This is also my first answer to anything on stackoverflow, and since I kinda do like computer science theory, I hope my explanations are not wrong. If so, tell me.

  • 1
    I have no idea what this question is about, but you certainly put a lot of effort into writing a thorough answer. Welcome to StackOverflow! – ElmerCat Nov 06 '15 at 18:27
  • Close. You left out the swapping production, and `TV->C` violates the length-restriction, so that grammar is not a valid CSG. – rici Nov 07 '15 at 03:02
  • Ah, snap. Didn't see the CFG-tag. – Alex_The_Saw Nov 07 '15 at 11:47