我试图在这个 nfa 中使用缩写来从一个州到另一个州。
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 事情是它没有创建状态。当:
-sign 出现时,我想创建来自("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
如何使缩写正常工作并返回 true?