Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a program that allows me to design a Regex using a Finite State Machine Graph?

I'm sick of Regex notation. It's ugly, unreadable, and impossible to debug. However, mathemeticians have been using Finite State Machines to design regular expressions for decades.

Finite State Machines

If I'm getting annoyed by regexes, I'll go and draw it out as a finite state machine by hand, and then have to translate the finite state machine into whatever hideous regex syntax I'm using today.

Is there a program that lets me design finite state machines and spit out a regex?

like image 419
Tara Roys Avatar asked Oct 03 '13 16:10

Tara Roys


1 Answers

If you know Python, you can try greenery. The package fsm is for finite state machines, and lego is for regular expression objects. The two can be freely interconverted.

>>> from greenery import fsm
>>> x = fsm.fsm(
...     alphabet={"1", "2"},
...     states={"A", "B", "C", "D", "E"},
...     initial="A",
...     finals={"E"},
...     map={
...         "A": {"1": "C", "2": "A"},
...         "B": {"1": "B", "2": "D"},
...         "C": {"1": "E", "2": "C"},
...         "D": {"1": "A", "2": "B"},
...         "E": {"1": "C", "2": "D"}
...     }
... )
>>> print(x.lego())
(1(11|2)*12(21*2)*1|2)*1(11|2)*1

I feel I should point out that your example finite state machine lacks both an initial state and any final states, so I guessed them. Also, an arbitrary FSM usually converts into a pretty horrible regular expression, and simplifying a regular expression is computationally difficult...

like image 80
qntm Avatar answered Oct 07 '22 20:10

qntm