Python 有限状态机问题(跳过处理?)
Python Finite State Machine Issues (Skipping the Proccessing?)
我正在为没有面向对象的有限状态机创建一个 python 程序。但是,我的处理阶段已关闭。它甚至似乎没有通过我创建的三重 for 循环 运行,我通过尝试打印 CurrentState 进行了检查。任何帮助,将不胜感激。
import sys
try:
Sfile = open("states.txt","r")
except IOError:
print "Could not open file", states.txt
os.kill()
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"
Sfile.close()
try:
Tfile = open("transistions.txt","r")
except IOError:
print "Could not open file", transitions.txt
os.kill()
Transitions = []
ReadLine = Tfile.readline()
while ReadLine != "":
ReadLine = ReadLine.rstrip()
CS, IN, NS = ReadLine.split(",")
Transitions.append((CS, IN, NS))
ReadLine = Tfile.readline()
print Transitions
Tfile.close()
try:
Strfile = open("strings2.txt","r")
except IOError:
print "Could not open file", strings2.txt
os.kill()
Strings = []
ReadLine = Strfile.readline()
while ReadLine != "":
Readline = ReadLine.rstrip()
Strings.append(Readline)
ReadLine = Strfile.readline()
print Strings, '\n'
Strfile.close()
CurrentState = ''
Start = ''
RejectState= ''
AcceptState= ''
for S in Strings:
if S != '':
for C in S:
for (CS, IN, NS) in Transitions:
if CS == CurrentState and IN == C:
CurrentState =NS
break
for (SN, SS, AS) in States:
if SN == CurrentState and SS ==C:
CurrentState = NS
if NS == AS:
NS = AcceptState
print "String", AcceptState, "is accepted"
break
else:
NS = RejectState
print "String", RejectState, "is rejected"
break
这是我的不同文本文件:
strings2.txt
01010
1001
010
transitions.txt
Start,0,State1
State1,1,State2
State2,0,State3
states.txt
State1,1,0
State2,0,1
State3,1,0
我复制了你的代码和数据,并且能够使用 python2.7:
使这段代码(从你的代码稍作修改)开始工作
import sys
import os
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:
Tfile = open("transitions.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:
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 = ''
RejectState= ''
AcceptState= ''
for S in Strings:
if S != '':
print "String:", S
for C in S:
print "Char:", C
for (CS, IN, NS) in Transitions:
if CS == CurrentState and IN == C:
CurrentState =NS
break
for (SN, SS, AS) in States:
if SN == CurrentState and SS ==C:
CurrentState = NS
if NS == AS:
NS = AcceptState
print "String", AcceptState, "is accepted"
else:
NS = RejectState
print "String", RejectState, "is rejected"
这是我得到的输出:
$ python2.7 test.py
States:
[('State1', True, False), ('State2', False, True), ('State3', True, False)]
Transitions:
[('Start', '0', 'State1'), ('State1', '1', 'State2'), ('State2', '0', 'State3')]
Strings:
01010
1001
010
String: 01010
Char: 0
Char: 1
Char: 0
Char: 1
Char: 0
String is rejected
String: 1001
Char: 1
Char: 0
Char: 0
Char: 1
String is rejected
String: 010
Char: 0
Char: 1
Char: 0
String is rejected
我正在为没有面向对象的有限状态机创建一个 python 程序。但是,我的处理阶段已关闭。它甚至似乎没有通过我创建的三重 for 循环 运行,我通过尝试打印 CurrentState 进行了检查。任何帮助,将不胜感激。
import sys
try:
Sfile = open("states.txt","r")
except IOError:
print "Could not open file", states.txt
os.kill()
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"
Sfile.close()
try:
Tfile = open("transistions.txt","r")
except IOError:
print "Could not open file", transitions.txt
os.kill()
Transitions = []
ReadLine = Tfile.readline()
while ReadLine != "":
ReadLine = ReadLine.rstrip()
CS, IN, NS = ReadLine.split(",")
Transitions.append((CS, IN, NS))
ReadLine = Tfile.readline()
print Transitions
Tfile.close()
try:
Strfile = open("strings2.txt","r")
except IOError:
print "Could not open file", strings2.txt
os.kill()
Strings = []
ReadLine = Strfile.readline()
while ReadLine != "":
Readline = ReadLine.rstrip()
Strings.append(Readline)
ReadLine = Strfile.readline()
print Strings, '\n'
Strfile.close()
CurrentState = ''
Start = ''
RejectState= ''
AcceptState= ''
for S in Strings:
if S != '':
for C in S:
for (CS, IN, NS) in Transitions:
if CS == CurrentState and IN == C:
CurrentState =NS
break
for (SN, SS, AS) in States:
if SN == CurrentState and SS ==C:
CurrentState = NS
if NS == AS:
NS = AcceptState
print "String", AcceptState, "is accepted"
break
else:
NS = RejectState
print "String", RejectState, "is rejected"
break
这是我的不同文本文件: strings2.txt
01010
1001
010
transitions.txt
Start,0,State1
State1,1,State2
State2,0,State3
states.txt
State1,1,0
State2,0,1
State3,1,0
我复制了你的代码和数据,并且能够使用 python2.7:
使这段代码(从你的代码稍作修改)开始工作import sys
import os
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:
Tfile = open("transitions.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:
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 = ''
RejectState= ''
AcceptState= ''
for S in Strings:
if S != '':
print "String:", S
for C in S:
print "Char:", C
for (CS, IN, NS) in Transitions:
if CS == CurrentState and IN == C:
CurrentState =NS
break
for (SN, SS, AS) in States:
if SN == CurrentState and SS ==C:
CurrentState = NS
if NS == AS:
NS = AcceptState
print "String", AcceptState, "is accepted"
else:
NS = RejectState
print "String", RejectState, "is rejected"
这是我得到的输出:
$ python2.7 test.py
States:
[('State1', True, False), ('State2', False, True), ('State3', True, False)]
Transitions:
[('Start', '0', 'State1'), ('State1', '1', 'State2'), ('State2', '0', 'State3')]
Strings:
01010
1001
010
String: 01010
Char: 0
Char: 1
Char: 0
Char: 1
Char: 0
String is rejected
String: 1001
Char: 1
Char: 0
Char: 0
Char: 1
String is rejected
String: 010
Char: 0
Char: 1
Char: 0
String is rejected