• 大小: 367KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-13
  • 语言: Python
  • 标签: python  DFA    

资源简介

python实现正规式转换成NFA、NFA转换成DFA、DFA转换成MFA算法,并实现画出三类形图,可以在用户图形界面显示,也可以在文件夹中显示。

资源截图

代码片段和文件信息

#-*-coding:utf-8 -*-
from os import popen
import time


class Automata:
    “““自动机类“““

    def __init__(self language = set([‘0‘ ‘1‘])):
        #五元组:状态集,开始状态,结束状态集,转化集,语言表(默认0,1)
        self.states = set()
        self.startstate = None
        self.finalstates = []
        self.transitions = dict()
        self.language = language

    @staticmethod
    def epsilon():
        return “:e:“

    def setstartstate(self state):
        self.startstate = state
        self.states.add(state)

    def addfinalstates(self state):
        if isinstance(state int):
            state = [state]
        for s in state:
            if s not in self.finalstates:
                self.finalstates.append(s)

    def addtransition(self fromstate tostate inp):
        if isinstance(inp str):
            inp = set([inp])
        self.states.add(fromstate)
        self.states.add(tostate)
        if fromstate in self.transitions:
            if tostate in self.transitions[fromstate]:
                self.transitions[fromstate][tostate] = self.transitions[fromstate][tostate].union(inp)
            else:
                self.transitions[fromstate][tostate] = inp
        else:
            self.transitions[fromstate] = {tostate : inp}

    def addtransition_dict(self transitions):
        for fromstate tostates in transitions.items():
            for state in tostates:
                self.addtransition(fromstate state tostates[state])

    def gettransitions(self state key):
        if isinstance(state int):
            state = [state]
        trstates = set()
        for st in state:
            if st in self.transitions:
                for tns in self.transitions[st]:
                    if key in self.transitions[st][tns]:
                        trstates.add(tns)
        return trstates

    def getEClose(self findstate):
        allstates = set()
        states = set([findstate])
        while len(states)!= 0:
            state = states.pop()
            allstates.add(state)
            if state in self.transitions:
                for tns in self.transitions[state]:
                    if Automata.epsilon() in self.transitions[state][tns] and tns not in allstates:
                        states.add(tns)
        return allstates

    def display(self):
        print “states:“ self.states
        print “start state: “ self.startstate
        print “final states:“ self.finalstates
        print “transitions:“
        for fromstate tostates in self.transitions.items():
            for state in tostates:
                for char in tostates[state]:
                    print “  “fromstate “->“ state “on ‘“+char+“‘“
            print

    def getPrintText(self):
        text = “language: {“ + “ “.join(self.language) + “}\n“
        text += “states: {“ + “ “.join(map(strself.states)) + “}\n“
        text += “start state: “ + str(self.startstate) + “\n“
        text += “final states: {“ + “ “.join(map(strself.finalstates)) + “}\n“
       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-21 18:24  automata-from-regex-master\
     目录           0  2013-04-08 11:04  automata-from-regex-master\screenshots\
     文件       72950  2013-04-08 11:04  automata-from-regex-master\screenshots\screenshot_DFA.png
     目录           0  2017-11-26 11:19  __MACOSX\
     目录           0  2017-11-26 11:19  __MACOSX\automata-from-regex-master\
     目录           0  2017-11-26 11:19  __MACOSX\automata-from-regex-master\screenshots\
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\screenshots\._screenshot_DFA.png
     文件       81494  2013-04-08 11:04  automata-from-regex-master\screenshots\screenshot_NFA.png
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\screenshots\._screenshot_NFA.png
     文件       56031  2013-04-08 11:04  automata-from-regex-master\screenshots\screenshot_minDFA.png
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\screenshots\._screenshot_minDFA.png
     文件       65959  2013-04-08 11:04  automata-from-regex-master\screenshots\screenshot_mainWindow.png
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\screenshots\._screenshot_mainWindow.png
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\._screenshots
     文件        5977  2017-11-21 19:13  automata-from-regex-master\graphmdfa.png
     文件       19178  2017-11-21 20:01  automata-from-regex-master\AutomataTheory.py
     文件         212  2017-11-21 20:01  __MACOSX\automata-from-regex-master\._AutomataTheory.py
     文件       14195  2017-11-21 19:13  automata-from-regex-master\graphdfa.png
     文件        2007  2013-04-08 11:04  automata-from-regex-master\README.md
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\._README.md
     文件        1000  2017-11-21 20:01  automata-from-regex-master\cli.py
     文件         212  2017-11-21 20:01  __MACOSX\automata-from-regex-master\._cli.py
     文件          12  2013-04-08 11:04  automata-from-regex-master\.gitignore
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\._.gitignore
     文件       33772  2017-11-21 19:13  automata-from-regex-master\graphnfa.png
     文件       17382  2017-11-21 18:24  automata-from-regex-master\AutomataTheory.pyc
     目录           0  2013-04-08 11:04  automata-from-regex-master\graphs\
     文件       18541  2013-04-08 11:04  automata-from-regex-master\graphs\graph_DFA.png
     目录           0  2017-11-26 11:19  __MACOSX\automata-from-regex-master\graphs\
     文件         212  2013-04-08 11:04  __MACOSX\automata-from-regex-master\graphs\._graph_DFA.png
     文件        6285  2013-04-08 11:04  automata-from-regex-master\graphs\graph_minDFA.png
............此处省略9个文件信息

评论

共有 条评论