如何用c ++ target

时间:2018-01-22 09:15:55

标签: c++11 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

1 个答案:

答案 0 :(得分:1)

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

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