2016-05-03 2 views
-1

バイナリを読むために私のFSMが必要です。状態と遷移に比べます。次に、それが受け入れられたか拒絶されたかを言う。受け入れ状態は簡単な010です。今は、私が何をしていても、ループは最初からやり直します。私はインデントのいくつかのバリエーションを試しました。テキストファイルでPython State Machine:ループをリセットしていますか?

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 
+0

インデントまたはインナーループを参照していますか?コルーチンを使って実装しようとすると、より洗練された外観になり、エラーが簡単に見つかります。 –

答えて

1

あなたは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状態からの遷移がないので、到達したらすぐにfor C in S:ループのうちbreakループになり、まったく同じ結果になります。

関連する問題