'How can I add an epsilon move to FSM using Python?

I am using the greenery module to implement an FSM:

Enter image description here

from greenery import fsm, lego

E, O = range(2)
z, o = '0', '1'

# Create the FSM
machine = fsm.fsm(
    alphabet = {o, z},
    states   = {E, O},
    initial  = E,
    finals   = {E},
    map      = {
            E : {z: O, o: E},
            O : {o: O, z: E},
    },
)

# Convert it to a regex
rex = lego.from_fsm(machine)

print(rex)

The problem is: How do I add those epsilon moves to and from state A? I couldn't find any examples.



Solution 1:[1]

It seems greenery is a python library for deterministic FSMs

This module provides for the creation and manipulation of deterministic finite state machines.

Your FSM is non-deterministic: Consider input 0,0: You can end up with states:

  1. Even--0-->Odd--0-->Even
  2. Or Even--0-->Odd--e-->A--e-->Even--0-->Odd

To use greenery, you need to manually (or via a script) transform your FSM to the equivalent Epsilon-free FSM:

Two FSM's are said to be equivalent if they accept exactly the same set of strings. Given any nondeterministc FSM, it is always possible to find an equivalent one that has no epsilon transitions. In fact, given a machine as defined and represented above, we can easily define such an equivalent epsilon-free machine. So given a machine named mach and defined in m/4, mis/2 and mfs/2, we will define the transitions, initial state and final state for its epsilon-free version named efree(mach) as follows: ...

and then / while you are doing this, compute the non-deterministic equivalent FSM.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1