저는 파이썬을 처음 접했고 저의 삶에서 제 기능이 정의되지 않은 이유 또는 방법을 알 수 없습니다. 이것은 어리석은 질문 일지 모르지만 사과를하지만,이 부분이 제대로 작동 할 때까지는 실제로 붙어 있고 나머지 부분을 테스트/수정할 수 없습니다. 어떤 도움을 주셔서 감사합니다. 다음은 스캐너를 호출하는 주요 클래스이지만 내 문제는 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/Documents/Benedictine/Fall_2014/CMSC 385/Project2/Driver.py", 줄 17, main() 파일 "/ Users/diessner/Documents/Benedictine/Fall_2014/CMSC 385/Project2/Driver.py ", 줄 11, 기본 토큰 = python.scanner() 파일"/ Users/diessner/Documents/Benedictine/Fall_2014/CMSC 385/Project2/Scanner .py ", line 55, 스캐너에서 nextChar = getChar (self) NameError : 이름 'getChar'이 정의되지 않았습니다. 내 대답과 같이 충분히 지정하지 않았기 때문에 그랬다고 생각합니다. –
Diesel298