I’m writing an interpreter in Python for a very simple language grammar. I have a superclass called Statement which has several child classes. I have a method in the superclass called CreateStatement that evaluates keywords, and based on the keyword found it creates an instance of a child class object. Some of the child classes will need to call this method.
My problem is that I’m getting this error:
AttributeError: 'Statement' object has no attribute 'Assignment'
Help!
import re
from TokenHandler import TokenHandler
from ParserException import ParserException
class Statement(object):
tokens = []
executedTokens = []
def __init__(self, tokenlist):
self.tokens = tokenlist
def getCurrentToken(self):
print self.tokens[0]
return self.tokens[0]
def createStatement(self):
currenttoken = self.getCurrentToken()
t = TokenHandler()
if currenttoken == "print":
p = Print(self.tokens)
return p
elif t.isVariable(currenttoken):
a = Assignment(self.tokens)
return a
elif currenttoken == "if":
i = IF(self.tokens)
return i
elif currenttoken == "while":
w = While(self.tokens)
return w
elif currenttoken == "begin":
s = self.CompoundStatement(self.tokens)
return s
else:
raise ParserException('Reserved word \'end\' expected')
#...other methods...
from Statement import Statement
from TokenHandler import TokenHandler
import VariableException
class Assignment(Statement):
def __init__(self, tokens = []):
super(Assignment, self).__init__(tokens)
#...other methods...
is wrong if
Assignmentis a subclass defined at the module level and not an attribute of theStatementclass or theselfinstance. Did you meanThis line is probably similarly wrong: