试图用缩写跳过状态

时间:2013-02-17 13:49:03

标签: python nfa

我正试图在这个nfa中使用abreviations从一个州到另一个州。

NFA定义为

nfa = NFA(
start = 0,
finals = [6],
abrs = {1:5, 3:5},
edges=[
(0,'h', 1),
(1,'a', 2),
(2,'z', 3),
(3,'a', 4),
(4,'r', 5),
(5,'d', 6)
])

我在处理字符串时创建了这个内部方法。

 char = ":"
 elif char is tape[index]:
        print " abreviation found!!"
        #goto to state

        gotoState = set([e[2] for e in nfa.abrs
                        if e[0] in nfa.edges and
                        tape[index+1] == e[1]
                        ])
        index += 1

尝试使用test print => print nrec("h:d", nfa, 1)它返回false 问题在于它不会创建状态。当出现: - 符号时,我想要创建来自示例("h:z")的状态,该状态应该包含所有这些状态=> ('h'->'a'->'z'->'a'->'r'->'d'),如何修改此方法来执行此任务? 这是在创建NFA结构后处理字符串的已定义方法。

def nrec(tape, nfa, trace=0):
"""Recognize in linear time similarly to transform NFA to DFA """
char = ":"
index = 0
states = [nfa.start]
while True:
    if trace > 0: print " Tape:", tape[index:], "   States:", states
    if index == len(tape): # End of input reached
        successtates = [s for s in states
                          if s in nfa.finals]
        # If this is nonempty return True, otherwise False.
        return len(successtates)> 0
    elif len(states) == 0:
        # Not reached end of string, but no states.
        return False
    elif char is tape[index]:
        print " abreviation found!!"
        #skip to state

        gotoState = set([e[2] for e in nfa.abrs
                        if e[0] in nfa.edges and
                        tape[index+1] == e[1]
                        ])
        index += 1
    # the add on method to take in abreviations by sign: :
    else:
        # Calculate the new states.
        states = set([e[2] for e in nfa.edges
                           if e[0] in states and
                              tape[index] == e[1] 
                      ])
        # Move one step in the string
        index += 1 

如何使abreviation正常工作并返回true?

0 个答案:

没有答案