尝试在Python中实现NFA的迭代识别器

尝试在Python中实现NFA的迭代识别器,python,Python,我正在寻找一种迭代方法来实现Python中NFA的单词识别器。 这是我的主要想法,也是我到目前为止得到的: from collections import defaultdict class NFA1: def __init__(self, initial, trns, final): self.initial = initial self.final = set(final) self.trns = defaultdict(set)

我正在寻找一种迭代方法来实现Python中NFA的单词识别器。 这是我的主要想法,也是我到目前为止得到的:

from collections import defaultdict

class NFA1:
    def __init__(self, initial, trns, final):
        self.initial = initial
        self.final = set(final)
        self.trns = defaultdict(set)
        for (src, char, tgt) in trns:
            self.trns[src, char].add(tgt)

def recognize(self, strng):
    #initial configuration
    agenda = [(self.initial, strng)]
    while agenda:
        current = agenda.pop()
        print(current)
        #if current is an acceptable configuration
        if current[0] in self.final and len(current[1]) == 0:
            return True
        else:
            #agenda is all possible subsequent configurations
    return False
现在我不知道如何继续。我理解,支持将所有可能的以下配置列入议程,并检查是否符合可接受配置的要求,但我不知道如何找到这些以下配置。我想我需要检查字符串的第一个字符是否在任何转换中,如果是,请删除第一个字符,更新我所处的状态,然后重新执行。但我只是不知道如何查看转换所处的defaultdict

我还应该提到,为了方便起见,代码一次只能使用一个字符,而且代码也不能识别空字符串

非常感谢你的回答