如何在python中运行antlr代码并验证输入

如何在python中运行antlr代码并验证输入,python,antlr,Python,Antlr,我有一个简单的语法文件hello.g包含 grammar Hello_world; utterance : greeting | exclamation; greeting : interjection subject; exclamation : 'Hooray!'; interjection : 'Hello'; subject : 'World!'; 验证字符串“Hello World!”“万岁!” 但是如何测试这种语法呢?在我读到的某个地方,我们需要编译解析器和其他程序。我是ANTLR

我有一个简单的语法文件hello.g包含

grammar Hello_world;
utterance : greeting | exclamation;
greeting : interjection subject;
exclamation : 'Hooray!';
interjection : 'Hello';
subject : 'World!';
验证字符串“Hello World!”“万岁!”

但是如何测试这种语法呢?在我读到的某个地方,我们需要编译解析器和其他程序。我是ANTLR的新手,请帮忙

我使用AntlrWorks1.5生成lexer和解析器; 之后,使用以下代码运行测试:

import antlr3
from helloworldLexer import helloworldLexer
from helloworldParser import helloworldParser

while True:
    expr = raw_input('>>> ')
    if expr == '':
        break

    cStream = antlr3.StringStream(expr)
    lexer = helloworldLexer(cStream)
    tStream = antlr3.CommonTokenStream(lexer)
    parser = helloworldParser(tStream)
    result = parser.evaluate()
    print result
运行上述python文件时,出现以下错误:

>>> Hello World!
Traceback (most recent call last):
  File "C:\Users\Lenovo\workspace\antlr\output\hello.py", line 12, in <module>
    lexer = helloworldLexer(cStream)
  File "C:\Users\Lenovo\workspace\antlr\output\helloworldLexer.py", line 26, in __init__
    state = RecognizerSharedState()
NameError: global name 'RecognizerSharedState' is not defined
>>你好,世界!
回溯(最近一次呼叫最后一次):
文件“C:\Users\Lenovo\workspace\antlr\output\hello.py”,第12行,在
lexer=helloworldLexer(cStream)
文件“C:\Users\Lenovo\workspace\antlr\output\helloworldLexer.py”,第26行,在\uu init中__
state=RecognizerSharedState()
NameError:未定义全局名称“识别器共享状态”

在您前面的一个问题中,我链接了两个答案,演示了如何让ANTLR与Python一起工作。@BartKiers,我已经添加了一个测试套件和语法错误输出。我无法找出到底是什么错误!