Is there any general method to do so? For example, we have a general method to find grammar for L1 U L2 by adding a production S-> S1 | S2 where S1 and S2 are start symbols for grammars of L1 and L2 respectively. Thanks in advance..
-
2This question appears to be off-topic for Stack Overflow as defined in the [help], because it's a general theoretic question, rather than a specific programming problem. It may be better suited to the [cs.se] Stack Exchange site. – Toby Speight Jan 29 '19 at 13:17
-
If you had an RE for L, let's call it `x`, then L* would be matched by `(x)*`. If you have a grammar for L, with starting symbol `X`, how do you express the same idea? Hint: think about how you'd write a grammar for the language `a*`. – Welbog Jan 29 '19 at 14:03
1 Answers
In general, given a grammar G
such that L(G) = L'
, there is no algorithm which always produces a regular grammar G'
such that L(G') = (L')*
. For starters, (L')*
may not be a regular language. Even if you allow the procedure to recognize this case and print "not a regular language" in such a case, this cannot be generally possible since it would allow us to determine whether arbitrary unrestricted grammars generate particular strings (the construction is not too hard but I won't provide it unless desired). This is an undecidable problem, so we can't recognize regular languages in unrestricted grammars.
Perhaps your question is whether there is a neat construction to do this if initially given a regular grammar. In that case, the answer is a definite and clear, "yes!" Here is one easily described (though possibly inefficient in practice) procedure for doing just that:
- Convert the regular grammar into a nondeterministic finite automaton using the typical construction for doing so. There are easy constructions for left-regular and right-regular grammars.
- Construct a regular expression from the nondeterministic finite automaton using any known construction. One such construction is typically used in proving equivalence.
- Construct a new regular expression which is the Kleene closure of the one from the last step.
- Construct a nondeterministic finite automaton from the regular expression from the last step, using a standard construction.
- Construct a regular grammar from the nondeterministic finite automaton from the last step. There are known constructions for this.
Thus, we can mechanically go from regular grammar for L
to regular grammar for L*
.
If you just want ANY grammar for L*
, the simplest would probably be to introduce a new start state S'
and productions S' := S'S' | S
where S
is the start symbol of your input grammar. This obviously does not give a regular grammar, however - if the input grammar generates a regular language, this one will do so as well.
Example: given the regular grammar
S := 0S | 1T
T := 0S | 1T | 1
A construction gives us this nondeterministic finite automaton:
q s q'
- - -
S 0 S
S 1 T
T 0 S
T 1 T
T 1 (H)
A construction gives us the regular expression:
(0*1)(0*1)*1
The Kleene closure of this is:
((0*1)(0*1)*1)*
We recognize from the standard construction that this automaton is equivalent:
q s q'
- - -
(I) - S
S 0 S
S 1 T
T 0 S
T 1 T
T 1 H
H - (I)
Whence the following regular grammar:
I := S | -
S := 0S | 1T
T := 0S | 1T | H
H := I

- 27,682
- 3
- 38
- 73