2

the question is

The generation rule of the regular grammar G is

S → 0A | 1B | ε , A → 1B | ε , B → 0A |ε, 

express L(G) as a regular expression.

My solution is as follows.

S = 0A + 1B+ ε
A = 1B + ε
B = 0A + ε

then

S = 0(1B + ε) + 1(0A + ε) = 1(0A + 0B + ε) + 0ε + ε

I don't know how to simplify the expression anymore here. Any help in this area would be appreciated.

Maëlan
  • 3,586
  • 1
  • 15
  • 35
  • Sorry, the text has been revised. I would be grateful if you look at it again. A and B are regular expressions. –  Mar 28 '21 at 12:01
  • 2
    Is it even possible? I mean we have recursion here. On the other hand, it seems like alternating sequences of `1` and `0`. – Wolf Mar 28 '21 at 12:04
  • It seems that the positions of 0 and 1 should not be swapped. Is it possible to tie the positions of 0 and 1 to 1 when correctly deployed? –  Mar 28 '21 at 12:09
  • 1
    *deployed*? I don't understand. – Wolf Mar 28 '21 at 12:11
  • I mean, is it possible to group other expressions with regular expression 1 in my solution? –  Mar 28 '21 at 12:16
  • 1
    I see a possible solution here https://math.stackexchange.com/a/1139940/128761 – Wolf Mar 28 '21 at 12:17
  • 1
    BTW: your `1(0A + 0B + ε) + 0ε + ε` looks wrong to me, it seems because your forgot `+ ε` in `0(1B + ε) + 1(0A + ε) ` – Wolf Mar 28 '21 at 12:18
  • oh thanks! then 0(1B + ε) + 1(0A + ε) + ε Could this expression be the answer? –  Mar 28 '21 at 12:24
  • 1
    No, this isn't a regex, we need to eliminate `A` and `B`, the nonterminals. What are the meta symbols of your regex? Is it `*` for repetition (`0..n`)? I guess we will not get better than `(01)∗+(10)∗+(01)∗0+(10)∗1` I already linked to. – Wolf Mar 28 '21 at 12:28
  • The problem is not marked with any conditions other than the creation rules mentioned in the question. In my opinion, the meta symbol is equal to 0 and 1. –  Mar 28 '21 at 12:35
  • 1
    I'm used to use and write regex in may daily life, but not experienced with transformations from regular grammar, I think the two non-accepted answers to this question could help https://stackoverflow.com/q/8898049/2932052 – Wolf Mar 28 '21 at 12:37
  • I kept asking stuffy questions, but thank you very much for your kind answer. Thank you for referring to the link you left and solving it. –  Mar 28 '21 at 12:40
  • 1
    `0` and `1` are not meta symbols but terminals. `(`, `)`, `+`, and `ε` (and maybe `*`) are meta symbols (`A` and `B` are productions, these should not occur). – Wolf Mar 28 '21 at 12:44

1 Answers1

2

We first write some equations:

S = 0A + 1B + e
A = 1B + e
B = 0A + e

We can eliminate B by substitution:

S = 0A + 1(0A + e)+ e = 0A + 10A + 1 + e
A = 1(0A + e)+ e = 10A + 1 + e
B = 0A + e

We can now eliminate the recursion in A:

S = (0 + 10)A + 1 + e
A = (10)*(1 + e)
B = 0A + e

Now we can eliminate A by substitution:

S = (0 + 10)(10)*(1 + e) + 1 + e
A = (10)*(1 + e)
B = 0(10)*(1 + e) + e

We can simplify the expression for S slightly by observing the common 1 + e term, factoring, and then noticing the + 10 term adds nothing:

S = (0 + 10)(10)*(1 + e) + 1 + e
  = [(0 + 10)(10)* + e](1 + e)
  = (0 + e)(10)*(1 + e)

This appears to be the language of all strings over {0, 1} containing neither 00 nor 11. To prove this, we can show the regular expression generates all such strings, and that it generates only such strings.

Any string generated by the expression is the concatenation of three strings: the first cannot end with 1, the last cannot begin with zero, and the middle can neither begin with zero nor end with 1. Thus, the strings 00 and 11 cannot be formed at the boundaries. It is also clear that none of the three can contain 00 or 11. Therefore, anything the expression generates has neither 00 nor 11.

Any string without 00 or 11 can be generated. Assume some such string starts with x and has length n.

If n > 0 and x = 0, the expression chooses 0 from the first part, 10 a number of times equal to n minus one, quantity over two, times; then, it chooses 1 from the third part if and only if n is even.

If n > 0 and x = 1, the expression chooses e for the first part, takes 10 a number of times equal to n minus one, quantity over two, times, and then chooses 1 for the third part if and only if n is odd.

If n = 0, the expression chooses the empty string for both the first and third parts, and takes 10 zero times.

In all three cases, the regular expression was able to generate the string. Because the expressions generates all strings and only strings in our language, it is a regular expression for the language we described.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • 1
    We see that `(0 + e)(10)*(1 + e)` is equivalent to `(1 + e)(01)*(0 + e)`. In cases like this, is there some canonical form that suggests to prefer one over the other? BTW: great explanation, I was already searching for a good introduction. This is a really good example how theoretical informatics helps in practice. – Wolf Mar 28 '21 at 18:31
  • 1
    @Wolf Probably not. I wonder whether we'd have gotten the other form maybe if we'd solved & substituted A first, and then done B? We got this form mechanically by applying rules until we solved for S. In general, there will be infinitely many equivalent expressions and many of those may be similar in terms of length of operations. – Patrick87 Mar 28 '21 at 22:21
  • 1
    Thank you so much. I tried to give up because I couldn't solve it anyway, but it was a big help. –  Mar 29 '21 at 00:41
  • @bongbong if this answer helped you then *accepting* it is the right thing to do (earns +15 for the answerer and +2 for you) – Wolf Mar 29 '21 at 09:00