Tara Roys
Tara Roys

Reputation: 1836

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?

Upvotes: 7

Views: 598

Answers (2)

qntm
qntm

Reputation: 4417

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...

Upvotes: 3

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

JFLAP is software for experimenting with formal languages topics including nondeterministic finite automata, nondeterministic pushdown automata, multi-tape Turing machines, several types of grammars, parsing, and L-systems. In addition to constructing and testing examples for these, JFLAP allows one to experiment with construction proofs from one form to another, such as converting an NFA to a DFA to a minimal state DFA to a regular expression or regular grammar.

JFLAP

Upvotes: 1

Related Questions