我是python新手,在我的一生中,我无法找到为什么或如何没有定义我的函数。这可能是一个愚蠢的问题,我很抱歉,但我真的卡住了,不能测试/修复其余的问题,直到我得到这部分工作。感谢任何帮助。 下面是调用scanner的主类,但我的问题是为什么getChar()没有被调用。堆栈跟踪的最深部分告诉我nextChar=getChar()没有定义。在
from Scanner import scanner
from Constants import *
def main():
python = scanner()
token = python.scanner()
while token.tokenType != END_OF_FILE:
print(token.tokenType, " ", token.lexeme)
token = python.scanner()
main()
class TokenRec(object):
def __init__(self, tokenType, lexeme, line, col):
self.tokenType = tokenType
self.lexeme = lexeme
self.line = line
self.col = col
class scanner():
# Constructor for the Scanner class
def __init__(self):
self.fileName = input("Enter the file name: ")
self.infile = open(self.fileName, "r")
self.fChar = self.infile.read(1)
self.line = 0
self.col = 0
# gets the next character out of the file
def getChar():
nextChar = file.read(1)
if nextChar == "":
nextChar = '\34'
return nextChar
# adds the next character onto the lexeme buffer
def addChar(nextToken, nextChar):
nextToken.lexeme += nextChar
def isKeyWord(nextChar):
return True
def isSingleToken(nextChar):
return True
def isMultiToken(nextChar):
return True
def scanner(self):
while True:
nextToken = TokenRec("","",self.line,self.col)
nextChar = getChar()
if nextChar == '\n':
self.line += 1
self.col = 0
if nextChar.isalpha():
addChar(nextToken, nextChar)
nextChar = getChar()
while nextChar != " ":
nextToken.lexeme += nextChar
nextChar = getChar()
if nextChar.issspace():
if isKeyWord(nextChar):
print("Error")
#Part 2
else:
nextToken.tokenType = 33
elif nextChar.isdigit():
nextToken.lexeme = nextChar
nextChar = getChar()
while nextChar != " ":
nextToken.lexeme += nextChar
nextChar = getChar()
nextToken.tokenType = 75
elif nextChar is '"':
nextChar = getChar()
while nextChar != '"':
nextToken.lexeme += nextChar
nextChar = getChar()
elif isSingleToken(nextChar):
print("Error")
# Part 2
elif nextChar is '#':
comment = file.readline()
elif nextChar is None:
nextToken.tokenType = 99
nextToken.lexeme = "END OF FILE"
else:
print("Character is illegal or unknown")
在贴出这篇文章几分钟后,我找到了我的答案。我只需将getChar()定义为扫描仪.getChar(). 如果这不是正确的方法,或者如果我仍然缺少一些东西,请随时提供帮助。在
问题可能是您的函数是在另一个类中定义的。在
例如:
相关问题 更多 >
编程相关推荐