如何使用 C++ 目标报告 antlr4 中的语法歧义



这个问题是Java代码类似问题的后续问题 - how-to-report-grammar-ambiguity-in-antlr4。我正在尝试将该代码移植到 c++,但是在使用 antlr 模板时遇到了一些编译错误。

c++ 中的主程序如下:

// TestA_Main.cpp
using namespace std;
using namespace antlr4;
using namespace atn;
int main(int argc, char **argv) 
{
    string filename = argv[0];
    ifstream stream;
    stream.open(filename);
    ANTLRInputStream input(stream);
    AmbigLexer lexer(&input);
    CommonTokenStream tokens(&lexer);
    AmbigParser parser(&tokens);
    parser.addErrorListener(new DiagnosticErrorListener());
    // the following line has an error on the call to getInterpreter
    parser.getInterpreter().setPredictionMode(PredictionMode::LL_EXACT_AMBIG_DETECTION);
    parser.stat();
}

我在其上运行了以下命令:

java org.antlr.v4.Tool -Dlanguage=Cpp Ambig.g4
g++ -std=gnu++0x -I. -I/antlr4_cpp/runtime/include -I/antlr4_cpp/runtime/include/atn TestA_Main.cpp

我在调用getInterpreter时收到以下编译错误:

TestA_Main.cpp: In function ‘int main(int, char**)’:
TestA_Main.cpp:27:27: error: no matching function for call to ‘AmbigParser::getInterpreter()’
     parser.getInterpreter().setPredictionMode(PredictionMode::LL_EXACT_AMBIG_DETECTION);
                           ^
In file included from /softwares/antlr4/antlr4_cpp/runtime/include/Lexer.h:8:0,
                 from /softwares/antlr4/antlr4_cpp/runtime/include/antlr4-runtime.h:32,
                 from TestA_Main.cpp:4:
/softwares/antlr4/antlr4_cpp/runtime/include/Recognizer.h:73:8: note: candidate: template<class T> T* antlr4::Recognizer::getInterpreter() const
     T* getInterpreter() const {
        ^
/softwares/antlr4/antlr4_cpp/runtime/include/Recognizer.h:73:8: note:   template argument deduction/substitution failed:
TestA_Main.cpp:27:27: note:   couldn't deduce template parameter ‘T’
     parser.getInterpreter().setPredictionMode(PredictionMode::LL_EXACT_AMBIG_DETECTION);

你能告诉我如何修复上面的代码吗?我正在使用antlr-4.6

它是一个模板函数,您必须显式指定模板参数:

parser.getInterpreter<ParserATNSimulator>()->setPredictionMode(PredictionMode::SLL);

最新更新