0
我试图创建一个方法是abreviations跳过从一个点到另一个。Abreviations在NFA,蟒蛇
我创建了一个NFA与当前边的什么,我试图完成
EDGES = [
(0, 'h', 1),
(1,'a',2),
(2,'z', 3),
(3,'a',4),
(4, 'r', 5),
(5, 'd', 6)
)]
例 nrec("h-rd", nfa, 1)
应该返回accept
nrec
是处理该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]:
# 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
我需要补充的是需要abreviations存入该帐户的方法。我不是我怎么能去跳过从一个状态到另一个状态十分肯定。 这是在类NFA:
def __init__(self,start=None, finals=None, edges=None):
"""Read in an automaton from python shell"""
self.start = start
self.edges = edges
self.finals = finals
self.abrs = {}
我因子评分有关使用区域边界路由器,而是试图如
nfa = NFA(
start = 0,
finals = [6],
abrs = {0:4, 2:5},
edges=[
(0,'h', 1),
(1,'a', 2),
(2,'z', 3),
(3,'a', 4),
(4,'r', 5),
(5,'d', 6)
])
我recive错误确定自己的ABR时,我constatly得到的错误“类型错误:初始化()得到了一个意想不到的关键字参数“区域边界路由器”” 为什么我reciveing的错误?
的修改我因子评分我会做这样的事情
elif char is tape[index]:
#get the next char in tape tape[index+1] so
#for loop this.char with abrs states and then continue from that point.
明智的选择或者没有更好的办法?