Python 2.7 在python中使用antlr

Python 2.7 在python中使用antlr,python-2.7,antlr,Python 2.7,Antlr,我试图在python中使用lexer.py和parser.py,但代码中有错误: import antlr3 import antlr3.tree import traceback from test22Lexer import test22Lexer from test22Parser import test22Parser char_stream = antlr3.ANTLRStringStream("input.txt") lexer = test22Lexer(char_stream)

我试图在python中使用lexer.py和parser.py,但代码中有错误:

import antlr3
import antlr3.tree
import traceback
from test22Lexer import test22Lexer
from test22Parser import test22Parser
char_stream = antlr3.ANTLRStringStream("input.txt")
lexer = test22Lexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = test22Parser(tokens)
parser.block()
错误是:

  class block_return(ParserRuleReturnScope):
  NameError: name 'ParserRuleReturnScope' is not defined
我需要帮助:D

解析器:

class block_return(ParserRuleReturnScope):
def __init__(self):
    super(test22Parser.block_return, self).__init__()

    self.tree = None

老实说,我对ANTLR一无所知,但您应该更加相信Python的错误消息:

NameError: name 'ParserRuleReturnScope' is not defined
它清楚地表明它没有定义,所以你忘了包含它。正如您在中所看到的,他们建议使用通配符将其包括在内,如:

from antlr3 import *
这将使它能够工作,因为如果我没有弄错的话,
parserureturnscope
是在
antlr3
包中定义的。如果您不想导入
antlr
中的所有内容,可以像导入一样导入它,并按包为所有对象添加前缀,如:
altlr3.parserureturnscope


正如我所写,我对ANTLR一无所知,如果它不起作用,我很抱歉,我试图帮助:)

我只是遇到了完全相同的问题。我在鸡蛋中查找类(ParserRuleReturnScope),但它似乎不存在。这是使用版本3.0.1。然后我就能够找到3.1.3运行时,以及ANTLR生成器JAR文件的相关版本。一旦我使用3.1.3生成,问题就消失了。因此,我建议安装3.1.3,以下是我使用的相关链接:

ANTLR发生器:

下载.jar文件,然后运行: java-jarpath/to/jar/file myGrammar.g

使用3.1.3生成解析器/词法分析器后,请使用相同版本的运行时:

您可以安装它,例如使用easy_install

现在模块加载(语法正确),但我仍在处理运行时错误。进步