简体   繁体   中英

How to convert regular expression to finite state machine?

Let the regular expression;

r = (a*|(ab)*)b*

what is the rules for converting this expression to finite state machine?

The rules for converting general regular expressions can be found in literature (eg Aho et al. "Compilers: Principles, Techniques, and Tools"), but quite a lot of effort is needed to program it. Presently many open source implementations are available for this task and other operations on finite-state machines and transducers, eg openFST, SFST, Foma, and HFST (which is a common interface for the three). They are available as standalone programs, as libraries and through eg Python. Below your example expression is compiled using the hfst-xfst standalone program (see http://hfst.github.io/ for more information).

$ hfst-xfst
hfst[0]: regex [a*|[a b]*]b* ;
? bytes. 6 states, 10 arcs, ? paths
hfst[1]: print net
Sfs0:   b -> fs1, a -> fs2.
fs1:    b -> fs1.
fs2:    b -> fs3, a -> fs4.
fs3:    b -> fs1, a -> s5.
fs4:    b -> fs1, a -> fs4.
s5: b -> fs3.
hfst[1]: 

The given regular expression

r = (a*|(ab)*)b*

The given regular expression can be broken into parts and can be combined together again to make it easy to design the DFA Let us break the regular expression into a*, ab, (ab) , b , a+b, a+((ab) ), (a |(ab) )b

Now a* can be made into finite automata as a*

Now ab can be made as

ab

b* can be made as b*

By joining both ab and b* into one automata we get (ab)* as (ab)*

Now a+b as a+b Now a+b and (ab)* can be combined by placing (ab)* in place of b in a+b then we get a+((ab) ) a+((ab)*) Now a+((ab) ) and b* can be joined using ab method and the required resultant Finite state automata is produced. The resultant converted (a*|(ab) )b

The rules of conversion from Regular expression to Finite state machine are:

1.Divide the expression to parts to make it easy to understand and add them 2.Make the Finte state machines for those partial expressions. 3.Join those partial expressions one by one. 4.Then we get the resultant NFA. 5.If we want to get DFA then by using ϵ-closure method convert NFA to equivalent DFA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM