简体   繁体   English

如何使用 Python 向 FSM 添加 epsilon 移动?

[英]How can I add an epsilon move to FSM using Python?

I am using the greenery module to implement an FSM :我正在使用greenery模块来实现FSM

在此处输入图像描述

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?问题是:如何添加那些进出状态 A 的 epsilon 移动? I couldn't find any examples.我找不到任何例子。

It seems greenery is a python library for deterministic FSMs似乎greenery是一个用于确定性FSM 的 python 库

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:您的 FSM是非确定性的:考虑输入 0,0:您最终可以得到以下状态:

  1. Even--0-->Odd--0-->Even偶数--0-->奇数--0-->偶数
  2. Or Even--0-->Odd--e-->A--e-->Even--0-->Odd或偶数--0-->奇数--e-->A--e-->偶数--0-->奇数

To use greenery, you need to manually (or via a script) transform your FSM to the equivalent Epsilon-free FSM:要使用 greenery,您需要手动(或通过脚本)将您的 FSM 转换为等效的无 Epsilon FSM:

Two FSM's are said to be equivalent if they accept exactly the same set of strings.如果两个 FSM 接受完全相同的字符串集,则称它们是等价的。 Given any nondeterministc FSM, it is always possible to find an equivalent one that has no epsilon transitions .给定任何非确定性 FSM,总是有可能找到一个没有 epsilon 转换的等价的 In fact, given a machine as defined and represented above, we can easily define such an equivalent epsilon-free machine.事实上,给定上面定义和表示的机器,我们可以很容易地定义这样一个等效的无 epsilon 机器。 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: ...因此,给定一个名为 mach 并在 m/4、mis/2 和 mfs/2 中定义的机器,我们将为其命名为 efree(mach) 的无 epsilon 版本定义转换、初始状态和最终状态,如下所示: ...

and then / while you are doing this, compute the non-deterministic equivalent FSM .然后 / 在您执行此操作时,计算非确定性等效 FSM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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