Kleene
Kleene
Rance Cleaveland
Spring 2000
1. Introduction
So far in class, we have concentrated on two classes of languages: the regular languages, which are
those that may be defined using regular expressions, and the FA languages, which are those that
are accepted by finite automata. Kleene’s Theorem states that, in fact, these classes are the same:
every regular language may be recognized by some FA, and every FA language may be represented
using a regular expression. The book presents one proof of these statements; this handout offers
an alternative, and I hope simpler, argument for the former.
On the basis of what we have seen in class, to establish that every regular language can be
recognized by an FA it suffices to show how, given a regular expression r, we can build a NFA M
such that L (r) = L (M). We give such a construction in a couple of steps.
√ √
1. We first define a predicate on regular expressions; intuitively, r is intended to hold if
ε ∈ L (r).
a
2. We then define a ternary relation −→⊆ R (Σ) × Σ × R (Σ). Intuitively, r −→ r′ is true if the
start state for a NFA for r can have an a-transition to the start state for a NFA for r′ . Put
a
differently, if r −→ r′ then a NFA for r should be able to “process” symbol a and then accept
all the strings in L (r′ ).
3. Using these relations, we then show how to build a NFA from r whose states are regular
√
expressions, whose transitions are given by −→, and whose final states are defined using .
2. Formal Definitions
√
We define recursively using the following rules.
√
Definition 2.1. Let Σ be an alphabet. Then r , where r ∈ R (Σ), is defined as follows.
√
• ε .
√
• r∗ .
1
√ √ √
• If r then (r + s) and (s + r) .
√ √ √
• If r and s then (rs) .
√
Intuitively, r holds if r is capable of “generating” the empty word, i.e. if ε ∈ L (r). Certainly
ε ∈ L (ε), and ε ∈ L (r∗ ) regardless of what r is. The definition of L (r + s) ensures that ε is in the
language of r + s if and only if it is in the language of r or s, while in the case of rs ε must be in
the language of both. As examples, we have the following.
√ √ √
εa∗ since ε and a∗ .
√ √ √
¬((a + b) ) since neither a nor b .
√ √
01 + (1 + 01)∗√ since (1 + 01) √
∗ .
In this latter example, note that applying the rules literally requires that we include the ε in
a a
εbb(abb + a)∗ . This is because the rule for a says that a −→ ε, meaning that abb −→ εbb, etc.
a
However, when we have εs like this, we will often leave them out; thus we will write abb −→ bb
a
rather than abb −→ εbb.
√
The following lemmas about and −→ formally establish the intuitive properties that we wish
them to have.
2
√
Lemma 2.3. Let r be a regular expression. Then r if and only if ε ∈ L (r).
Lemma 2.4. Let r ∈ R (Σ) be a regular expression over Σ, a ∈ Σ, and x ∈ Σ∗ . Then ax ∈ L (r) if
a
and only if there is an r′ ∈ R (Σ) such that r −→ r′ and x ∈ L (r′ ).
Both lemmas may be proved using strong induction on the size of regular expression r.
√
3. Building Automata using and −→
√
To see how and −→ may be used to build NFAs, first note how we can use them to determine
whether a string is in the language of a regular expression. Consider the following sequence of
“transitions” starting from the regular expression (abb + a)∗.
a b b a
(abb + a)∗ −→ bb(abb + a)∗ −→ b(abb + a)∗ −→ (abb + a)∗ −→ (abb + a)∗
Using Lemma 2.4 (four times!), we can√conclude that if x ∈ L ((abb +a)∗), then abbax ∈ L ((abb +
a)∗ ) also. In addition, since (abb + a)∗ , we know from Lemma 2.3 that ε ∈ L ((abb + a)∗). Since
abbaε = abba, then, it follows that abba ∈ L ((abb + a)∗).
a1 an √
More generally, if there is a sequence of transitions r0 −→ r1 ··· −→ rn and rn , then we can
assert that a1 ...an ∈ L (r0), and vice versa. This observation suggests the following possible strategy
for building a NFA from a regular expression r.
1. Let the states be all possible regular expressions that can be reached by some sequence of
−→-transitions from r.
2. Take r to be the start state.
3. Let the transitions be given by −→.
√
4. Let the accepting states be those regular expressions r′ for which r′ holds.
Of course, this would only work if the set of “all possible regular expressions” mentioned in part 1
is finite, since a NFA is required to have a finite number of states.
To make this construction precise, and to examine the issue of “finiteness” of state sets, we
need to define mathematically the set of “all possible regular expressions that can be reached by
some sequence”. We can do this as follows.
Definition 3.5. Let r ∈ R (Σ) be a regular expression. Then the set RS(r) ⊆ R (Σ) is defined
recursively as follows.
• r ∈ RS(r).
a
• If r1 ∈ RS(r) and r1 −→ r2 for some a ∈ Σ, then r2 ∈ RS(r).
The RS stands for “reachability set.” As an example, note that
RS((abb + a)∗) = {(abb + a)∗, bb(abb + a)∗, b(abb + a)∗}.
The following result indicates that the number of reachable regular expressions is always finite.
3
Lemma 3.6. Let r be a regular expression. Then RS(r) is finite.
Proof. Follows from observations such as
• RS(r∗ ) = { r′ r∗ | r′ ∈ RS(r) }.
✷
We can now define our NFA construction as follows.
Definition 3.7. Let r ∈ R (Σ) be a regular expression. Then NFA(r) = hQ, Σ, q0, δ, Ai is the NFA
defined as follows.
• Q = RS(r).
• q0 = r.
a
• δ(r1 , a) = { r2 ∈ Q | r1 −→ r2 }.
√
• A = { r′ ∈ Q | r′ }.
The next theorem establishes that r and NFA(r) always have the same languages.
It may not be apparent from the discussion up to now, but in fact the construction for NFA(r) given
above can be automated; that is, one can come up with a routine for building NFA(r), given r.
Before describing how this may be done, we first make precise the notion of “outgoing transitions”
from a regular expression and explain how they may be calculated.
Definition 4.9. Let r ∈ R (Σ) be a regular expression. Then the set of outgoing transitions from r
a
is defined as the set { ha, r′i | r −→ r′ }.
Intuitively, the outgoing transitions from r consists of pairs ha, r′ i that, when combined with r,
a
constitute a valid “transition” r −→ r′ . Figure 1 contains a recursive procedure computing for
outgoing transitions. The routine, out, uses the structure of r and the rules that define −→ to
guide its computation. For regular expressions of the form 0, / ε and a ∈ Σ, the definition of −→
immediately gives all the transitions. For regular expressions built using +, · and ∗ , one must
first recursively compute the outgoing transitions of the subexpressions of r and then combine the
results appropriately, based on the rules given in the definition of −→.
The next lemma states that out correctly computes of outgoing transitions.
4
{} if r = 0/ or r = ε
{ha, εi} if r = a ∈ Σ
out(r1 ) ∪ out(r2 ) if r = r1 + r2
out(r) =
{ ha, r1′ r2 i | ha, r1′ i ∈ out(r1 ) } √
∪{ ha, r2′ i | ha, r2′ i ∈ out(r2 ) ∧ r1 }
if r = r1 r2
{ ha, r1′ r1∗ i | ha, r1′ i ∈ out(r1) } if r = r1∗
Lemma 4.10. Let r ∈ R (Σ) be a regular expression, and let out be as defined in Figure 1. Then
a
out(r) = { ha, r′ i | r −→ r′ }.
Proof. The proof breaks into two pieces. The first requires us to show that every ha, r′ i ∈ out(r)
a a
satisfies: r −→ r′ . In the second, we establish that whenever r −→ r′ , then ha, r′ i ∈ out(r). Both
arguments can be carried out using induction, with the first being done on the structure of the
definition of out and the second using the definition of −→.
✷
We now sketch a routine for computing NFA(r); it relies on maintaining three sets of regular
expressions.
• toProc, a subset of Q containing states that have not yet had their transitions computed and
thus require some “processing”.
The algorithm works as follows. Initially, Q and toProc contain only r. While there remains
at least one regular expression to process, we remove one such an expression from toProc and
√
perform the following. First, we check to see if holds for the expression; if so then we add the
expression to the set of accepting states. Then we compute all the “outgoing transitions” from the
given expression; the target expressions of these transitions that are not already in Q are added both
to Q and to toProc, as they have not yet been encountered and thus need their transitions computed.
The algorithm terminates when toProc is empty. Pseudocode for this procedure may be found in
Figure 2, while Figure 3 gives the NFA resulting from applying the procedure to (abb + a)∗ .
5
procedure NFA (r) =
begin
Q := {r};
/
A := 0;
set δ(r, a) := 0/ for all a ∈ Σ;
toProc := {r};
while toProc 6= 0/ do
begin
choose r1 ∈ toProc;
delete r1 from toProc;
√
if r1 then add r1 to A;
compute T = out(r1 );
for each ha, r1′ i ∈ T do
begin
add r1′ into δ(r1 , a);
if r1′ 6∈ Q then add r2 to Q and toProc;
end
end;
return NFA hQ, Σ, r, δ, Ai;
end
(abb + a)∗ a
a
b bb(abb + a)∗
b
b(abb + a)∗