为什么我的功能没有定义

时间:2014-11-11 05:07:40

标签: python object

我是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")

2 个答案:

答案 0 :(得分:0)

发帖后几分钟我找到了答案。我所要做的就是将getChar()定义为scanner.getChar()。如果这不是正确的方法,或者如果我仍然缺少某些东西,请随时提供帮助。

答案 1 :(得分: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
>>>