Я новичок в python и для жизни меня не могу понять, почему и как моя функция не определена. Это может быть глупый вопрос, и я прошу прощения, но я действительно застрял и не могу проверить/исправить остальную часть этого, пока я не получу эту часть работы. Любая помощь приветствуется. Вот мой основной класс, который вызывает сканер, но тогда моя проблема в том, почему 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")
Пожалуйста, пост полный трассировки стека ошибки! Благодаря! –
Да, пожалуйста, напишите полную статистику стека, а также, пожалуйста, не забудьте свой отступ! –
'Traceback (самый последний вызов последнего): Файл "/ Users/diessner/Документы/бенедиктинский/Fall_2014/CMSC 385/Проект2/Driver.py", строка 17, в основной() Файл«/ Users/diessner/Documents/Benedictine/Fall_2014/CMSC 385/Project2/Driver.py ", строка 11, в основном token = python.scanner() Файл«/Пользователи/diessner/Documents/Benedictine/Fall_2014/CMSC 385/Project2/Scanner .py ", строка 55, в сканере nextChar = getChar (self) NameError: имя 'getChar' не определено' Я считаю, что это было потому, что я не указывал достаточно, как мой ответ ниже –
Diesel298