Question

I'm trying to create a method were abreviations skips from one point to another.

I've created a NFA with the current edges

EDGES = [
(0, 'h', 1),
(1,'a',2),
(2,'z', 3),
(3,'a',4),
(4, 'r', 5),
(5, 'd', 6)
)]

Example of what I'm trying to accomplish nrec("h-rd", nfa, 1) should return accept

nrec is the method that process the string for the NFA and checking wether it accepts or rejects.

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 

I need to add a method that takes abreviations into the account. I'm not quite sure on how I can go skip from one state to another. This is what is in the class 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 = {}

I tought about using the abrs, but i constatly get's error when trying to define my own abrs such as

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

I recive error "TypeError: init() got an unexpected keyword argument 'abrs'" Why I am reciveing that error?

for the modification i tought I'd do something like this

 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.

smart choice or any better solutions?

Was it helpful?

Solution

The error is caused by __init__ not accepting the abrs keyword parameter as it is defined.

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

You'd need abrs=None (or another value) to make it a keyword argument or abrs to make it a required argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top