编译原理 – DFA 简单实现
DFA 确定状态有限自动机NFA 非确定状态有限自动机目标实现如下的简单DFA的实现代码如下:''' DFA 的实现 2024-12-13 14:22:37 Author: www.o2oxy.cn(查看原文) 阅读量:1 收藏

DFA 确定状态有限自动机

NFA 非确定状态有限自动机

目标实现如下的简单DFA的实现

代码如下:

'''
    DFA 的实现
    -->0 --a-->1 --a--> [2]
       b       b        a,b  

表如下
---------------------------------
|状态\字符| a       |  b        |
--------------------------------
|0        | 1       |0          |
---------------------------------
|1        | 2       |1          |
---------------------------------
|2        | 2       |2          |
---------------------------------
'''
class DFA:
    def __init__(self):
        self.dfa={
            "0":{
                "a":"1",
                "b":"0"
            },
            "1":{
                "a":"2",
                "b":"1"
            },
            "2":{
                "a":"2",
                "b":"2"
            }
        }
        #接受的状态是
        self.accept_status=["a","b"]

        #当前状态
        self.start="0"

        self.accept="2"
        

    #读取状态转移到另外的状态
    def read(self,str):
        if str in self.dfa[self.start]:
            self.start=self.dfa[self.start][str]
        
    def is_accept(self):
        return self.start ==self.accept


def start_dfa(strs):
    dfa=DFA()
    for str in strs:
        dfa.read(str)
    return dfa.is_accept()


print(start_dfa("bbbbbbabca"))

文章来源: https://www.o2oxy.cn/4268.html
如有侵权请联系:admin#unsafe.sh