ANTLR4 version 4.13.2, C++ target.
I have a Lexer and Parser rules as below.
Lexer:
enter image description here
Parser:
enter image description here
I use the command antlr4 -Dlanguage=Cpp -listener -visitor -o generated/ -package spec SpecLexer.g4 SpecParser.g4 to generate code.
Then, I have a program:
antlr4::ANTLRInputStream input("Test: { } ;");
spec::SpecLexer lexer(&input);
antlr4::CommonTokenStream tokens(&lexer);
tokens.fill();
for (auto token : tokens.getTokens()) {
std::cout << token->toString() << std::endl;
}
spec::SpecParser parser(&tokens);
antlr4::tree::ParseTree* tree = parser.root();
std::cout << tree2->toStringTree(&parser) << std::endl;
When I try to run it, it always report the error:
enter image description here
I cannot understand, the lexer seems to work fine, but parser cannot always match the first token.
I cannot understand, the lexer seems to work fine, but parser cannot always match the first token, why???
Simpure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.