Search code examples
pythonruntime-errorcomputation-theoryautomaton

Python nondeterministic epsilon authomaton : object is not iterable


I have to make non-deterministic finite automaton with epsilon transitions. I'm more of a c, c#, JavaScript guy, but my university thinks python is the only way to go for some reason, so today I learned python, but obviously not enough.

Anyway. Problem is in "automaton" function. I call it with initial state list with one element (stanje1) and array of characters that automaton has to read (inputArrays[0]) along with dictionary that defines transition function (dictionary key is string currentState,inputCharacter and value is array of next states).

First iteration runs fine, but when I want to recursively check all states I got via epsilon transition against next character I get error :

TypeError: 'NoneType' object is not iterable

Automaton recursion is called with all arguments defined similarely as in top call, yet error somehow just jumps out.

Here's link to my code, as well as test file that defines automaton (you need to run it like so myScript.py < automaton.txt). Also, here's a full error report:

C:\Users\Jinx\Desktop\UTRLabos1>sim.py < test.txt
['stanje1']
['st6']
Traceback (most recent call last):
File "C:\Users\Jinx\Desktop\UTRLabos1\sim.py", line 50, in <module>
    print(automaton(['stanje1'], inputArrays[0], transitionFunction))
File "C:\Users\Jinx\Desktop\UTRLabos1\sim.py", line 45, in automaton
    if estates : other = automaton(estates, ir[1:], fn)
File "C:\Users\Jinx\Desktop\UTRLabos1\sim.py", line 37, in automaton
    states += eNKA(s, ir[0], fn)
TypeError: 'NoneType' object is not iterable

Solution

  • Your hint is that the crash occurs on this line:

    states += eNKA(s, ir[0], fn)
    

    and not deeper inside the eNKA function. You may suspect that something on this line is None, instead of an iterable. ir can't possibly be None because you checked for that possibility earlier, so it must be the case that the return value of eNKA is None (and you are effectively doing states += None).

    In fact, this is the case. eNKA doesn't return a value in every possible case, which may result in the default value of None to be returned.

    Thus, the fix would be to see why every if condition in eNKA is failing, and fix that appropriately (or return something more sensible if every condition fails).