Python 状态机:重置循环?

Python State Machine: Resetting Loop?

我需要我的 FSM 来读取二进制文件,与状态和​​转换进行比较。然后,说它是被接受还是被拒绝。接受状态是一个简单的 010。现在,无论我做什么,循环都会从头开始。我尝试了几种缩进方式。

import sys
import os


try:
    Tfile = open("transistions2.txt","r")
except IOError:
    print "Could not open file", "transitions.txt"
    sys.exit()
Transitions = []


ReadLine = Tfile.readline()
while ReadLine != "":
    ReadLine = ReadLine.rstrip()
    CS, IN, NS = ReadLine.split(",")
    Transitions.append((CS, IN, NS))
    ReadLine = Tfile.readline()

print "Transitions:\n", Transitions, "\n"
Tfile.close()


try:
    Sfile = open("states.txt","r")
except IOError:
    print "Could not open file", "states.txt"
    sys.exit()
States = []

ReadLine = Sfile.readline()
while ReadLine != "":
    SN, SS, AS = ReadLine.split(",")
    States.append((SN, bool(int(SS)), bool(int(AS))))
    ReadLine = Sfile.readline()

print "States:\n", States, "\n"
Sfile.close()

try:
    Strfile = open("strings2.txt","r")
except IOError:
    print "Could not open file", strings2.txt
    sys.exit()
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
    Readline = ReadLine.rstrip()
    Strings.append(Readline)
    ReadLine = Strfile.readline()

print "Strings:\n", '\n'.join(Strings), '\n'
Strfile.close()

CurrentState = ''
Start ='State1' 


for S in Strings:
    if S != '':
            print "String:", S
            for C in S:
                print "Number:", C
                CurrentState =Start
                for  Transitions in (CS, IN, NS):
                    print Transitions
                    if CS == CurrentState and IN == C:
                           CurrentState = NS
                           print NS
                           break
                for  States in (SN,SS,AS):
                    if SN == CurrentState:
                        print S, "is accepted"
                    elif  AS == CurrentState:
                        print S, " is rejected"
                    else:
                        print S,"Doesnt work"

我读入的几个文本文件是:

Transitions:
[('State3', '1', 'State3'), ('State3', '0', 'State4'), ('State2', '1', 'State3'), ('State2', '0', 'State2'), ('State1', '1', 'State1'), ('State1', '0', 'State2')] 

States:
[('State1', True, False), ('State2', False, False), ('State3', False, False), ('State4', False, True)] 

Strings:
01010
1001
010 

您尝试使用 states.txt 文件会使 FSM 实现变得比必要的更复杂 — 如果没有它,它会是这样的:

import sys

try:
    Tfile = open("transitions2.txt", "r")
except IOError:
    print "Could not open file transitions2.txt"
    sys.exit()
Transitions = []

ReadLine = Tfile.readline()
while ReadLine != "":
    ReadLine = ReadLine.rstrip()
    CS, IN, NS = ReadLine.split(",")
    Transitions.append((CS, IN, NS))
    ReadLine = Tfile.readline()

print "Transitions:\n", Transitions, "\n"
Tfile.close()


try:
    Strfile = open("strings2.txt", "r")
except IOError:
    print "Could not open file strings2.txt"
    sys.exit()
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
    Readline = ReadLine.rstrip()
    Strings.append(Readline)
    ReadLine = Strfile.readline()

print "Strings:\n", '\n'.join(Strings), '\n'
Strfile.close()


Start = 'State1'
Accept = 'State4'

for S in Strings:
    CurrentState = Start
    print "String:", S
    for C in S:
        print " Number:", C
        # find matching state and and input number
        for CS, IN, NS in Transitions:
            if CS == CurrentState and IN == C:
                CurrentState = NS  # make transition to next state
                print " NS ->", NS
                break

    if CurrentState == Accept:
        print " "+S, "Is accepted"
    else:
        print " "+S, "Doesn't work"
    print

输出:

Transitions:
[('State3', '1', 'State3'), ('State3', '0', 'State4'), ('State2', '1', 'State3'), 
 ('State2', '0', 'State2'), ('State1', '1', 'State1'), ('State1', '0', 'State2')]

Strings:
01010
1001
010

String: 01010
 Number: 0
 NS -> State2
 Number: 1
 NS -> State3
 Number: 0
 NS -> State4
 Number: 1
 Number: 0
 01010 Is accepted

String: 1001
 Number: 1
 NS -> State1
 Number: 0
 NS -> State2
 Number: 0
 NS -> State2
 Number: 1
 NS -> State3
 1001 Doesn't work

String: 010
 Number: 0
 NS -> State2
 Number: 1
 NS -> State3
 Number: 0
 NS -> State4
 010 Is accepted

由于没有从 Accept 状态转换,您可以 break 在到达 for C in S: 循环后立即跳出并获得完全相同的结果。