简体   繁体   中英

How to get regular expression based on regular grammar?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM