qntm / greenery

Regular expression manipulation library
http://qntm.org/greenery
MIT License
331 stars 40 forks source link

Can't convert this seemingly simple FSM to a regex in good time #43

Closed qntm closed 4 years ago

qntm commented 5 years ago

Looking at this Twitter thread, we see a task which is exactly the kind of thing which greenery is intended to accomplish: building finite state machines and then converting them to regular expressions. I decided to apply greenery to this problem, and as an intermediate phase of this work, I ended up with the following finite state machine:

  name final? D  L  R  U
--------------------------
* 0    False  1        2
  1    False     3
  2    False     4
  3    False           5
  4    False  6
  5    False        7
  6    False        8
  7    False           9
  8    False  10
  9    False     11
  10   False     12
  11   False     13
  12   False     14
  13   False  15
  14   False           16
  15   False        17
  16   False        18
  17   False  19
  18   False           19
  19   False     20
  20   True

which can be built using the Python expression:

fsm(alphabet = {'R', 'L', 'U', 'D'}, states = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, initial = 0, finals = {20}, map = {0: {'D': 1, 'U': 2}, 1: {'L': 3}, 2: {'L': 4}, 3: {'U': 5}, 4: {'D': 6}, 5: {'R': 7}, 6: {'R': 8}, 7: {'U': 9}, 8: {'D': 10}, 9: {'L': 11}, 10: {'L': 12}, 11: {'L': 13}, 12: {'L': 14}, 13: {'D': 15}, 14: {'U': 16}, 15: {'R': 17}, 16: {'R': 18}, 17: {'D': 19}, 18: {'U': 19}, 19: {'L': 20}, 20: {}})

This FSM is simple enough that it's possible to convert it to a regular expression by hand. The result is:

(DLURULLDRD|ULDRDLLUR)L

However, running greenery.lego.from_fsm against this FSM seems to take minutes, at least, on my machine - long enough that it hasn't finished in the time it's taking me to raise this issue.

This indicates some kind of performance problem in greenery, or possibly some kind of accidental infinite loop.

qntm commented 4 years ago

Closed by #44.