0

hello.g に含まれる単純な文法ファイルがあります

grammar Hello_world;
utterance : greeting | exclamation;
greeting : interjection subject;
exclamation : 'Hooray!';
interjection : 'Hello';
subject : 'World!';

文字列 'Hello World!' を検証します。と「万歳!」

しかし、この文法をテストする方法は? どこかで、パーサーなどをコンパイルする必要があると読んだことがあります。私はANTLRを初めて使用します。助けてください。

AntlrWorks1.5 を使用してレクサーとパーサーを生成しました。その後、以下のコードを使用してテストを実行しました。

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
4

0 に答える 0