第一次在这里提问,请多多包涵。因此,我从语法存储库中获取了一个语法文件,该文件正试图与C++一起使用。 (在macOS上开发)。我没有使用ANTLR生成词法分析器和解析器的问题。但是在那之后,我不知道如何运行/使用生成的.cpp和.h文件。我知道必须下载antlr运行时,并且已经从antlr.org网站下载了(给了我两个文件夹,antlr4-runtime和lib),但是我对C++的新手理解似乎阻止了我除此之外。如何使用运行时处理这些文件?我没有使用IDE,只是从命令行使用g++。感谢您的任何帮助!
最佳答案
我发现本指南很有帮助:Getting Started with ANTLR in C++。 (啊,之后看到了@ggorlen的评论。)
如果您在该页面上向下滚动到一半,则标题为如何在C++ 中使用ANTLR。我认为那是您的位置。
我将复制该示例,因为SO通常更喜欢此示例。说这是您的main.cpp:
#include <iostream>
#include "antlr4-runtime/antlr4-runtime.h"
#include "antlr4-runtime/SceneLexer.h"
#include "antlr4-runtime/SceneParser.h"
#include "ImageVisitor.h"
using namespace std;
using namespace antlr4;
int main(int argc, const char* argv[]) {
std::ifstream stream;
stream.open("input.scene");
ANTLRInputStream input(stream);
SceneLexer lexer(&input);
CommonTokenStream tokens(&lexer);
SceneParser parser(&tokens);
SceneParser::FileContext* tree = parser.file();
ImageVisitor visitor;
Scene scene = visitor.visitFile(tree);
scene.draw();
return 0;
}
您要在示例中包括lexer和parser .h(头)文件,而不是SceneLexer / Parser,并且还包括antlr4-runtime.h。然后在所有.cpp文件上运行g++,例如
$ g++ main.cpp YourLexer.h YourParser.h
关于c++ - ANTLR4和C++入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61669087/