ANTLR 未生成 ParserBase

问题描述 投票:0回答:1

我是 ANTLR 新手,正在研究 CPP14 语法这里。我尝试使用

antlr4 -Dlanguage=Python3 CPP14Lexer.g4

antlr4 -Dlanguage=Python3 CPP14Parser.g4

然后使用以下驱动程序代码:

import sys
from antlr4 import *
from CPP14Lexer import CPP14Lexer
from CPP14Parser import CPP14Parser

def main(argv):
    input_stream = FileStream(argv[1])
    lexer = CPP14Lexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = CPP14Parser(stream)
    tree = parser.start_()

if __name__ == '__main__':
    main(sys.argv)

并且,尝试运行它来解析 cpp 中的 Hello World 代码,但出现以下错误:

python Driver.py example.cc                                                                                   
Traceback (most recent call last):
  File "/Users/vedantamohapatra/antlr/Driver.py", line 4, in <module>
    from CPP14Parser import CPP14Parser
  File "/Users/vedantamohapatra/antlr/CPP14Parser.py", line 14, in <module>
    from CPP14ParserBase import CPP14ParserBase
ModuleNotFoundError: No module named 'CPP14ParserBase'

我应该如何纠正这个问题?

python parsing antlr antlr4
1个回答
0
投票

我认为你应该首先运行transformGrammar.py,然后在运行驱动程序文件之前CPP14ParserBase.py

© www.soinside.com 2019 - 2024. All rights reserved.