2014-11-11 3 views
0

저는 파이썬을 처음 접했고 저의 삶에서 제 기능이 정의되지 않은 이유 또는 방법을 알 수 없습니다. 이것은 어리석은 질문 일지 모르지만 사과를하지만,이 부분이 제대로 작동 할 때까지는 실제로 붙어 있고 나머지 부분을 테스트/수정할 수 없습니다. 어떤 도움을 주셔서 감사합니다. 다음은 스캐너를 호출하는 주요 클래스이지만 내 문제는 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") 
+0

오류의 전체 스택 추적을 게시하시기 바랍니다! 감사! –

+0

예, 스택 추적을 모두 게시하고 들여 쓰기를 잊지 마시기 바랍니다. –

+0

'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

답변

0

글자 그대로이 게시물을 게시 한 후 답변을 찾았습니다. 내가 할 일은 getChar()를 scanner.getChar()로 정의하는 것뿐입니다. 이것이 올바른 방법이 아니거나 내가 아직 뭔가를 놓치고 있다면 언제든지 도와주십시오.

0

함수가 다른 클래스에서 정의 된 것일 수 있습니다. 예를 들어

:

>>> class foo: 
...  @staticmethod 
...  def bar(): 
...    print 'foo+bar = foobar' 
... 
>>> bar() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'bar' is not defined 
>>> foo().bar() 
foo+bar = foobar 
>>> 
+0

예 또는 그게 무엇입니까? 내가 게시 한 답변에서 말한 것처럼 발견되었습니다. 때로는 그것을 작성하고 설명하는 데 도움이되는 경우도 있습니다. – Diesel298

+0

오 이런, 아직 당신의 대답을 보지 못했지만, 당신이 그것을 알아 낸 것을 기쁘게 생각합니다! –

관련 문제