converting finite automata to regular expressions...converting finite automata to regular...
TRANSCRIPT
Converting Finite Automata to
Regular Expressions
Alexander Meduna, Lukas Vrabel, and Petr Zemek
Brno University of Technology, Faculty of Information Technology
Bozetechova 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 termsWhy?
Methods
Transitive Closure MethodState Removal MethodBrzozowski 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.
• q1 q2 q3a bc
ab
• 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 Rkij represents the set of all strings that transition the
automaton from qi to qj without passing through any statehigher than qk . We can construct Rij by successively
constructing R1ij ,R
2ij , . . . ,R
|Q|ij = Rij .
• Rkij is recursively defined as:
Rkij = Rk−1
ij + Rk−1ik (Rk−1
kk )∗Rk−1kj
• Assuming we have initialized R0ij to be:
R0ij =
r if i 6= j and r transitions NFA from qi to qj
r + ε 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 usingTransitive Closure Method:
q1 q2
1
0, 10
Converting Finite Automata to Regular Expressions 7 / 23
Example (2/5)
1) Initialize R0ij :
q1 q2
1
0, 10
R011 ε+ 1
R012 0
R021 ∅
R022 ε+ 0 + 1
Converting Finite Automata to Regular Expressions 8 / 23
Example (3/5)
2) Compute R1ij :
q1 q2
1
0, 10
By direct substitution Simplified
R111 ε+ 1 + (ε+ 1)(ε+ 1)∗(ε+ 1) 1∗
R112 0 + (ε+ 1)(ε+ 1)∗0 1∗0
R121 ∅+ ∅(ε+ 1)∗(ε+ 1) ∅
R122 ε+ 0 + 1 + ∅(ε+ 1)∗0 ε+ 0 + 1
Converting Finite Automata to Regular Expressions 9 / 23
Example (4/5)
3) Compute R2ij :
q1 q2
1
0, 10
By direct substitution Simplified
R211 1∗ + 1∗0(ε+ 0 + 1)∗∅ 1∗
R212 1∗0 + 1∗0(ε+ 0 + 1)∗(ε+ 0 + 1) 1∗0(0 + 1)∗
R221 ∅+ (ε+ 0 + 1)(ε+ 0 + 1)∗∅ ∅
R222 ε+ 0 + 1 + (ε+ 0 + 1)(ε+ 0 + 1)∗(ε+ 0 + 1) (0 + 1)∗
Converting Finite Automata to Regular Expressions 10 / 23
Example (5/5)
4) Get the resulting regular expression:
q1 q2
1
0, 10
⇒ R212 = R12 = 1∗0(0 + 1)∗ is the REGEXP corresponding to the
NFA.
Converting Finite Automata to Regular Expressions 11 / 23
State Removal Method
• Based on a transformation from NFA to GNFA (generalizednondeterministic 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 theaccepted language won’t be changed.
Converting Finite Automata to Regular Expressions 12 / 23
Example (1/3)
Transform the following NFA to the corresponding REGEXP usingState Removal Method:
q3q1 q2
e
a
d
b
c
Converting Finite Automata to Regular Expressions 13 / 23
Example (2/3)
1) Remove the ”middle” state:
q3q1 q2
e
a
d
b
c
⇓
q1 q3
ae∗d ce∗b
ae∗b
ce∗d
Converting Finite Automata to Regular Expressions 14 / 23
Example (3/3)
2) Get the resulting regular expression r :
q1 q3
ae∗d ce∗b
ae∗b
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 regularexpression 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 tosolve it.
Converting Finite Automata to Regular Expressions 16 / 23
Example (1/5)
Transform the following NFA to the corresponding REGEXP usingBrzozowski Method:
q1 q2
a
c
b
b
Converting Finite Automata to Regular Expressions 17 / 23
Example (2/5)
1) Create a characteristic regular equation for state 1:
q1 q2
a
c
b
b
X1 = aX1 + bX2
Converting Finite Automata to Regular Expressions 18 / 23
Example (3/5)
2) Create a characteristic regular equation for state 2:
q1 q2
a
c
a
b
X2 = ε + bX1 + cX2
Converting Finite Automata to Regular Expressions 19 / 23
Example (4/5)
4) Solve the arisen system of regular expressions:
X1 = aX1 + bX2
X2 = ε + bX1 + 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∗]
q1 q2
a
c
b
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):481–494, 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.
M. Ceska, T. Vojnar, and A. Smrcka.
Studijnı opora do predmetu teoreticka informatika.Available on URL:https://www.fit.vutbr.cz/study/courses/TIN/public/Texty/oporaTIN.pdf.
Converting Finite Automata to Regular Expressions 23 / 23
Discussion