Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular...

24
Converting Finite Automata to Regular Expressions Alexander Meduna, Luk ´ s Vr ´ abel, and Petr Zemek Brno University of Technology, Faculty of Information Technology Boˇ zetˇ echova 1/2, 612 00 Brno, CZ http://www.fit.vutbr.cz/∼{meduna,ivrabel,izemek} Supported by the FRV ˇ SM ˇ SMT FR271/2012/G1 grant, 2012. Converting Finite Automata to Regular Expressions 1 / 23

Transcript of Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular...

Page 1: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 2: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

Outline

Introduction

Basic termsWhy?

Methods

Transitive Closure MethodState Removal MethodBrzozowski Algebraic Method

Comparison

Converting Finite Automata to Regular Expressions 2 / 23

Page 3: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

Basic Terms

• Finite automata (NFAs, DFAs)

• Regular expressions (REGEXPs)

• . . .

Converting Finite Automata to Regular Expressions 3 / 23

Page 4: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

Why?

Two possible transformations:

• Regular expression → Finite automaton

X

• Finite automaton → Regular expression

Uhm. . . Why?

Converting Finite Automata to Regular Expressions 4 / 23

Page 5: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 6: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 7: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 8: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 9: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 10: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 11: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 12: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 13: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 14: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 15: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 16: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 17: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 18: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 19: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 20: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 21: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 22: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 23: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

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

Page 24: Converting Finite Automata to Regular Expressions...Converting Finite Automata to Regular Expressions Alexander Meduna, Luka´ˇs Vr abel, and Petr Zemek´ Brno University of Technology,

Discussion