If we are trying to construct a device that accepts a regular language , we can proceed one state at a time, as in Example 2.22, deciding at each step which strings it is necessary to distinguish. Adding each additional state may get harder as the number of states grows, but if we know somehow that there is an FA accepting , we can be sure that the procedure will eventually terminate and produce one.
We have examples to show that for certain regular expressions, nondeterminism simplifies the problem of drawing an accepting device. In this section we will use nondeterminism to show that we can do this for every regular expression. Furthermore, we now have algorithms to convert the resulting NFA to an FA. The conclusion will be that on the one hand, the state-by-state approach will always work; and on the other hand, there is a systematic procedure that is also guaranteed to work and may be more straightforward.
The general result is one half of Kleene’s theorem, which says that regular languages are the languages that can be accepted by finite automata. We will discuss the first half in this section and the second in Section 3.5.
Theorem 3.25Kleene’s Theorem, Part 1
For every alphabet , every regular language over can be accepted by a finite automaton.
ProofTheorem 3.25
Because of Theorems 3.17 and 3.18, it’s enough to show that every regular language over can be accepted by an NFA. The set of regular languages over is defined recursively in Definition 3.1, and we will prove the theorem by structural induction.
The languages and (where ) can be accepted by the two NFAs in Figure 3.26, respectively. The induction hypothesis is that and are both regular languages over and that for both and , can be accepted by an NFA . We can assume, by renaming states if necessary, that and are disjoint. In the induction step we must show that there are NFAs accepting the three languages , , and .
(a)
(b)
In each case we will give an informal definition and a diagram showing the idea of the construction. For simplicity, each diagram shows the two NFAs and as having two accepting states, both distinct from the initial state.
An NFA accepting is shown in Figure 3.27a. Its states are those of and and one additional state that is the initial state. The transitions include all the ones in and as well as -transitions from to and , the initial states of and . Finally, the accepting states are simply the states in .
If , for example, can accept by taking the -transition from to and then executing the moves that would allow to accept . On the other hand, if is any string accepted by , there is a path from to an element of or . The first transition in the path must be a -transition, which takes to or . Because , the remainder of the path causes to be accepted either by or by .
An NFA accepting is shown in Figure 3.27b. No new states need to be added to those of and . The initial state is , and the accepting states are the elements of . The transitions include all those of and and a new -transition from every element of to . If is the string , where is accepted by for each , then can process by moving from to a state in using ’s and the symbols of , taking the -transition to , and moving to a state in using ’s and the symbols of . Conversely, if is a string accepted by , then at some point during the computation, must execute the -transition from an element of to . If is the prefix of whose symbols have been processed at that point, then must be accepted by ; the remaining suffix of is accepted by , because it corresponds to a path from to an element of that cannot involve any transitions other than those of .
(a)
(b)
(c)
Schematic diagram for Kleene’s theorem, Part 1.
Finally, an NFA accepting is shown in Figure 3.27c. Its states are the elements of and a new initial state that is also the only accepting state. The transitions are those of , a -transition from to , and a -transition from every element of to . We can see by structural induction that every element of is accepted. The null string is, because is an accepting state. Now suppose that is accepted and that . When is in a state in after processing , it can take a -transition to and another to , process so as to end up in an element of , and finish up by returning to with a -transition. Therefore, is accepted by .
We can argue in the opposite direction by using mathematical induction on the number of times enters the state in the process of accepting a string. If visits only once in accepting , then , which is an element of . If we assume that and that every string accepted by that causes to enter or fewer times is in , then consider a string that causes to enter times and is accepted. Let be the prefix of that is accepted when enters the th time, and let be the remaining part of . By the induction hypothesis, . In processing , moves to on a -transition and then from to an element of using -transitions in addition to the symbols of . Therefore, , and it follows that .
Example 3.28An NFA Corresponding to
The three portions of the induction step in the proof of Theorem 3.25 provide algorithms for constructing an NFA corresponding to an arbitrary regular expression. These can be combined into a general algorithm that could be used to automate the process.
The transition diagram in Figure 3.29a shows a literal application of the three algorithms in the case of the regular expression . In this case there is no need for all the -transitions that are called for by the algorithms, and a simplified NFA is shown in Figure 3.29b. At least two -transitions are still helpful in order to preserve the resemblance between the transition diagram and the regular expression. The algorithms can often be shortened in examples, but for each step where one of them calls for an extra state and/or a -transition, there are examples to show that dispensing with the extra state or the transition doesn’t always work (see Exercises 3.45–3.48).
(a)
(b)
Constructing an NFA for the regular expression .