You are on page 1of 24

Converting Finite Automata to

Regular Expressions
s Vrabel,

Alexander Meduna, Luka


and Petr Zemek
Brno University of Technology, Faculty of Information Technology
Bozet
echova 1/2, 612 00 Brno, CZ
http://www.fit.vutbr.cz/{meduna,ivrabel,izemek}

Supported by the FRVS MSMT FR271/2012/G1 grant, 2012.


Converting Finite Automata to Regular Expressions

1 / 23

Outline

Introduction
Basic terms
Why?
Methods
Transitive Closure Method
State Removal Method
Brzozowski Algebraic Method

Comparison

Converting Finite Automata to Regular Expressions

2 / 23

Basic Terms

Finite automata (NFAs, DFAs)


Regular expressions (REGEXPs)
...

Converting Finite Automata to Regular Expressions

3 / 23

Why?

Two possible transformations:


Regular expression Finite automaton

X
Finite automaton Regular expression

Uhm. . . Why?

Converting Finite Automata to Regular Expressions

4 / 23

Transitive Closure Method

Rather theoretical approach.

ab

q1

q2

b
c

q3

Sketch of the method:


1 Let Q = {q1 , q2 , . . . , qm } be the set of all automatons states.
2 Suppose that regular expression Rij represents the set of all
strings that transition the automaton from qi to qj .
3 Wanted regular expression will be the union of all Rsf , where
qs is the starting state and qf is one the final states.
The main problem is how to construct Rij for all states qi , qj .

Converting Finite Automata to Regular Expressions

5 / 23

How to construct Rij ?


Suppose Rijk represents the set of all strings that transition the

automaton from qi to qj without passing through any state


higher than qk . We can construct Rij by successively
|Q|
constructing Rij1 , Rij2 , . . . , Rij = Rij .
Rijk is recursively defined as:
k1 k1
) Rkj
Rijk = Rijk1 + Rikk1 (Rkk

Assuming we have initialized Rij0 to be:

r
r +
Rij0 =

if i 6= j and r transitions NFA from qi to qj


if i = j and r transitions NFA from qi to qj
otherwise

Converting Finite Automata to Regular Expressions

6 / 23

Example (1/5)

Transform the following NFA to the corresponding REGEXP using


Transitive Closure Method:
1

q1

q2

0, 1

Converting Finite Automata to Regular Expressions

7 / 23

Example (2/5)
1) Initialize Rij0 :
1
0

q1

0
R11
0
R12
0
R21
0
R22

q2

0, 1

+1
0

+0+1

Converting Finite Automata to Regular Expressions

8 / 23

Example (3/5)
2) Compute Rij1 :
1

q1

1
R11
1
R12
1
R21
1
R22

q2

0, 1

By direct substitution

Simplified

+ 1 + ( + 1)( + 1) ( + 1)
0 + ( + 1)( + 1) 0
+ ( + 1) ( + 1)
+ 0 + 1 + ( + 1) 0

1
1 0

+0+1

Converting Finite Automata to Regular Expressions

9 / 23

Example (4/5)
3) Compute Rij2 :
1
0

q1

q2

0, 1

By direct substitution
2
R11
2
R12
2
R21
2
R22

Simplified

1 + 1 0( + 0 + 1)
1 0 + 1 0( + 0 + 1) ( + 0 + 1)
+ ( + 0 + 1)( + 0 + 1)
+ 0 + 1 + ( + 0 + 1)( + 0 + 1) ( + 0 + 1)

1
1 0(0 + 1)

(0 + 1)

Converting Finite Automata to Regular Expressions

10 / 23

Example (5/5)

4) Get the resulting regular expression:


1

q1

q2

0, 1

2
= R12 = 1 0(0 + 1) is the REGEXP corresponding to the
R12
NFA.

Converting Finite Automata to Regular Expressions

11 / 23

State Removal Method


Based on a transformation from NFA to GNFA (generalized

nondeterministic finite automaton).


Identifies patterns within the graph and removes states,

building up regular expressions along each transition.


Sketch of the method:
1 Unify all final states into a single final state using -trans.
2 Unify all multi-transitions into a single transition that contains
union of inputs.
3 Remove states (and change transitions accordingly) until
there is only the starting a the final state.
4 Get the resulting regular expression by direct calculation.
The main problem is how to remove states correctly so the

accepted language wont be changed.


Converting Finite Automata to Regular Expressions

12 / 23

Example (1/3)
Transform the following NFA to the corresponding REGEXP using
State Removal Method:
e
b

a
q1

q2
d

q3
c

Converting Finite Automata to Regular Expressions

13 / 23

Example (2/3)
1) Remove the middle state:

e
b

a
q1

q2

q3
c

ae d

ce b
ae b

q1

q3
ce d

Converting Finite Automata to Regular Expressions

14 / 23

Example (3/3)

2) Get the resulting regular expression r:

ae d

ce b
ae b

q1

q3

ce d

r = (ae d) ae b(ce b + ce d(ae d) ae b) .

Converting Finite Automata to Regular Expressions

15 / 23

Brzozowski Algebraic Method

Janusz Brzozowski, 1964


Utilizes equations over regular expressions.
Sketch of the method:
1 Create a system of regular equations with one regular
expression unknown for each state in the NFA.
2 Solve the system.
3 The regular expression corresponding to the NFA is the regular
expression associated with the starting state.
The main problem is how to create the system and how to

solve it.

Converting Finite Automata to Regular Expressions

16 / 23

Example (1/5)

Transform the following NFA to the corresponding REGEXP using


Brzozowski Method:
a
b
q1

q2

Converting Finite Automata to Regular Expressions

17 / 23

Example (2/5)

1) Create a characteristic regular equation for state 1:


a
b
q1

q2

b
X1

aX1

bX2

Converting Finite Automata to Regular Expressions

18 / 23

Example (3/5)

2) Create a characteristic regular equation for state 2:


a
a
q1

q2

b
X2

bX1

cX2

Converting Finite Automata to Regular Expressions

19 / 23

Example (4/5)

4) Solve the arisen system of regular expressions:

X1
X2

=
=

aX1
bX1

+
+

bX2
cX2

Converting Finite Automata to Regular Expressions

20 / 23

Example (5/5)

Solution:
X1 = (a + bc b) bc
X2 = c [ + b(a + bc b) bc ]
a
b
q1

q2

b
X1 is the REGEXP corresponding to the NFA.

Converting Finite Automata to Regular Expressions

21 / 23

Comparison of presented methods

Transitive Closure Method


+ clear and simple implementation
- tedious for manual use
- tends to create very long regular expressions
State Removal Method
+ intuitive, useful for manual inspection
- not as straightforward to implement as other methods
Brzozowski Algebraic Method
+ elegant
+ generates reasonably compact regular expressions

Converting Finite Automata to Regular Expressions

22 / 23

References

J. Brzozowski.
Derivatives of regular expressions.
Journal of the ACM, 11(4):481494, 1964.
J. E. Hopcroft and J. D. Ullman.
Introduction to Automata Theory, Languages, and Computation.
Addison-Wesley, 1979.
P. Linz.
An introduction to Formal Languages and Automata.
Jones and Bartlett Publishers, 3rd edition, 1979.
C. Neumann.
Converting deterministic finite automata to regular expressions.
Available on URL:
http://neumannhaus.com/christoph/papers/2005-03-16.DFA to RegEx.pdf.
ska, T. Vojnar, and A. Smr
M. Ce
cka.
informatika.
Studijn opora do predm
etu teoreticka
Available on URL:
https://www.fit.vutbr.cz/study/courses/TIN/public/Texty/oporaTIN.pdf.

Converting Finite Automata to Regular Expressions

23 / 23

Discussion

You might also like