2013-02-16 9 views
0

나는 한 점에서 다른 점을 생략하는 방법을 만들려고하고있다.NFA, Python에서 Abreviations

는 내가 달성하기 위해 노력하고있어의

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를 만들었은 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 

계좌에 절충을 가하는 방법을 추가해야합니다. 나는 한 국가에서 다른 국가로 건너 뛸 수있는 방법에 대해서는 잘 모르겠습니다. 이 클래스 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 = {} 

내가 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) 
]) 

로 내 자신의 ABRS을 정의 할 때 내가 constatly의 오류 "형식 오류 : 초기화()에 예기치 않은 키워드 인수 'abrs'가 있습니다. " 왜 그 오류가 나타나고 있습니까? 수정에 대한

내가 내가이

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. 

현명한 선택 또는 더 나은 솔루션과 같은 일을 할 것 문질러서?

답변

0

키워드 매개 변수가 정의되어 있으므로이 오류는 __init__이 수락하지 않아 발생합니다.

def __init__(self,start=None, finals=None, edges=None):

당신은 필수 인수하기 위해 그것을 키워드 인수 나 abrs를 만들기 위해 abrs=None (또는 다른 값)가 필요합니다 것입니다.