Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert finite state machine to regular expression

Is there a tool (or an algorithm) to convert a finite state machine into a regular expression?

(not the other way around, that would be easy).

like image 629
enrico.bacis Avatar asked Apr 25 '16 23:04

enrico.bacis


People also ask

What is the regular expression for finite state machine?

Regular expressions describe patterns which can be recognized by finite state machines (FSM). It is possible to algorithmically construct a FSM that corresponds to a given regular expression. A FSM can be described by a transition table (program), which can be represented by a string.

How do I convert FSA to regex?

Step 1 − Construct a Transition diagram for a given RE by using Non-deterministic finite automata (NFA) with ε moves. Step 2 − Convert NFA with ε to NFA without ε. Step 3 − Convert the NFA to the equivalent Deterministic Finite Automata (DFA).

How do you convert NFA to regex?

To convert an NFA to a regular expression, we first think of the NFA as a generalized NFA. We then transform it so that it has a single final state by adding epsilon transitions (we can do this, because ε is a regular expression). then the equivalent regular expression is (r1∣r2r4* r3) * r2r4* .

Which of the following is useful for converting finite automata into regular expression?

Explanation: Thompson construction algorithm is an algorithm in automata theory used to convert a given regular expression into NFA. Similarly, Kleene algorithm is used to convert a finite automaton to a regular expression.


1 Answers

There are several algorithms to perform this task: the "state-elimination method" from Brzozowski and Mc Cluskey, the resolution of a system of linear equation, the method from McNaughton and Yamada, etc. They are very well described in Automata and rational expressions by Jacques Sakarovitch.

The state-elimination method in particular is simple to understand. The key idea is that you are going to build an automaton labeled by rational (aka regular) expressions rather than letters. First, make sure you have a single initial state and a single final state (you may add fresh states and spontaneous transitions if necessary). Then choose a state s to eliminate, say state 1 in the following picture.

A simple automaton

Then consider all the couples (p, q) where p is a predecessor (states from which a transition reaches s, 0 in the picture) and q a successor (state 2). For each such couple (p, q) add a transition from p to q which is labeled by E(p, q) + E(p, s)E(s, s)*E(s, q) where E(p, s) means "the expression that labels the transition from p to s. Once you treated all the couple (p, q), remove the state s. In the previous example:

An automaton labeled by regexps

Do that until you eliminated all the inner states (i.e., keep the initial state and the final state), and just read the result on the transition from the initial state to the final state (here d+ab*c).

You may toy with this algorithm using Vcsn, a tool for rational expressions and automata. Here is a complete example you may reproduce at Vcsn Sandbox.

A complete run of the state elimination method

like image 165
akim Avatar answered Sep 24 '22 07:09

akim