1

What would be a way to construct an unrestricted grammar for sss? I know that in order to construct ss, you need to construct ss^r and then re-reverse the second string, but how would that be done for sss?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
riv94
  • 111
  • 12

1 Answers1

0

There may be a few bugs in this but the idea should get you on the right track.

// section 1
S := LTR
T := ATWX | BTYZ | N

// section 2
WW' := W'W
WY' := Y'W
YW' := W'Y
YY' := Y'Y

// section 3
WX' := W'X'
WZ' := W'Z'
YX' := Y'X'
YZ' := Y'Z'

// section 4
XW' := W'X
XX' := X'X
XY' := Y'X
XZ' := Z'X
ZW' := W'Z
ZX' := X'Z
ZY' := Y'Z
ZZ' := Z'Z

// section 5
XR  := X'R
ZR  := Z'R

// section 6
NW' := W'N
NX' := X'N
NY' := Y'N
NZ' := Z'N
NR  := Q

// section 7
AQ  := Qa
W'Q := Qa
X'Q := Qa
BQ  := Qb
Y'Q := Qb
Z'Q := Qb
LQ  := e

Section 1 sets out the basic scheme. We're going to mark the beginning and end of our working space with L and R to be perfectly explicit. T is our working space and L and R are the left and right markers. Our working space can add either three as (these are what A, W, and X will become later) or add three bs (these are what B, Y and Z will become later) or it can quit by generating the special nonterminal N. N is going to be what turns our string of nonterminals into a string of terminals, as we'll see later.

So we need xxx for x in (a+b)*. A and B get added to the end of the first x because of the way we set up the productions. The job of section 2 is to make sure that the W and Y get added to the end of the second x. Unfortunately, our productions put the W and Y at the beginning of the second x so, without correction, we'd end up with xx^R. Section 2 corrects this problem by "floating" the W and Y to the right as long as they are to the left of a W or Y that has already found its proper place. Ws and Ys can't pass each other and so this guarantees they will end up in the proper sequence once the music stops.

Section 3 shows how the symbols in the second occurrence of x find their correct positions. Basically, they move to the right (based on section 2) until they find symbols of the third x that are in their proper places. Since the symbols of the second x have their proper place before the symbols of the third x, the last possible right before either symbol of the third x (X or Z) is the right position for every symbol of the second x. So we float right all the way to the end of the second x.

We repeat the process for the third x in section 4, although we have to let these symbols (X and Z) float past all the symbols from the second or third x that have already found their proper places. These symbols can't pass themselves and so by the same logic as we saw in section 2 we'll get the symbols in the correct sequence.

Section 5 says when to stop floating symbols that make up the third x to the right. Remember the end of working space marker R? The last symbol of the third x occurs all the way at the right, right before R. So that's when we know we have found the proper place for the symbol.

Section 6 begins the process of finishing off our derivation. We have a lovely string of nonterminals and we want to turn these into terminals. The first thing we do is float N to the right past the well-placed symbols of the second and third xs all the way to the end of string marker R. We don't need R for anything, so we transform N into Q. As we'll see in the next section Q is what makes the magic happen.

Section 7 outlines how Q floats left back through the entire string of nonterminals that have found their proper place all the way to L, changing the nonterminals to the proper terminal along the way. Once it finds L, it obliterates both nonterminals leaving only a string of terminal symbols in its wake. The derivation is complete.

To illustrate the process, let's generate aabaabaab.

S
-> LTR 
-> LATWXR 
-> LAATWXWXR 
-> LAABTYZWXWXR
-> LAABNYZWXWXR
-> LAABNYZWXWX'R
-> LAABNYZWXW'X'R
-> LAABNYZWW'XX'R
-> LAABNYZWW'X'XR
-> LAABNYZWW'X'X'R
-> LAABNYZW'WX'X'R
-> LAABNYZW'W'X'X'R
-> LAABNYW'ZW'X'X'R
-> LAABNYW'W'ZX'X'R
-> LAABNYW'W'X'ZX'R
-> LAABNYW'W'X'X'ZR
-> LAABNYW'W'X'X'Z'R
-> LAABNW'YW'X'X'Z'R
-> LAABNW'W'YX'X'Z'R
-> LAABNW'W'Y'X'X'Z'R
-> LAABW'NW'Y'X'X'Z'R
-> LAABW'W'NY'X'X'Z'R
-> LAABW'W'Y'NX'X'Z'R
-> LAABW'W'Y'X'NX'Z'R
-> LAABW'W'Y'X'X'NZ'R
-> LAABW'W'Y'X'X'Z'NR
-> LAABW'W'Y'X'X'Z'Q
-> LAABW'W'Y'X'X'Qb
-> LAABW'W'Y'X'Qab
-> LAABW'W'Y'Qaab
-> LAABW'W'Qbaab
-> LAABW'Qabaab
-> LAABQaabaab
-> LAAQbaabaab
-> LAQabaabaab
-> LQaabaabaab
-> aabaabaab
Patrick87
  • 27,682
  • 3
  • 38
  • 73