I have an if/elif statement that appends objects into a stack. The code works fine for the first 3 parts of the statement but is giving errors for the else part. I've tried a few different things but they just end up giving more errors. Thanks in advance for any help!
class nfa:
intial, accept = None, None
def __init__(self, initial, accept):
self.intial, self.accept = initial, accept
def compile(postfix):
nfaStack = []
for c in postfix:
# join the initial and accept states together to create a loop for your characters
if c == '*':
nfa = nfaStack.pop()
initial, accept = state(), state()
initial.edge1, nfa.accept.edge1 = nfa.intial
initial.edge2, nfa.accept.edge2 = accept, accept
nfaStack.append(nfa(initial, accept))
# merge the two automata by linking 1's accept to 2's initial states
elif c == '.':
nfa2, nfa1 = nfaStack.pop(), nfaStack.pop()
nfa1.accept.edge1 = nfa2.intial
nfaStack.append(nfa1.intial, nfa2.accept)
# create new initial and accept states and use them to link nfa1 and nfa2
elif c == '|':
nfa2, nfa1 = nfaStack.pop(), nfaStack.pop()
initial, accept = state(), state()
initial.edge1, initial.edge2 = nfa1.intial, nfa2.intial
# both old accept states now point to our new accept state
nfa1.accept.edge1, nfa2.accept.edge1 = accept, accept
nfaStack.append(nfa(initial, accept))
# creates new states and edges; labels each edge with what the current non-special character is
else:
initial, accept = state(), state()
initial.label = c
initial.edge1 = accept
# create instance of class nfa()
nfaStack.append(nfa(initial, accept))
# should only ever have one nfa in the stack
return nfaStack.pop()
You're getting this error because you're accidentally redefining nfa
as a variable here:
# join the initial and accept states together to create a loop for your characters
if c == '*':
nfa = nfaStack.pop() # `nfa` is now a variable!
The simplest solution is to capitalize the class to NFA
, so it's clear what is a variable and what is a class name. That would mean changing lines like this:
# This:
nfaStack.append(nfa(initial, accept))
# Becomes:
nfaStack.append(NFA(initial, accept))