Always error: line 1:0 mismatched input 'Token' expecting {<EOF>,Token}
- Dominant language
- Java
- Stars
- 19k
- Forks
- 3.5k
- PR merge metrics
- No merged PRs in 30d
Description
ANTLR4 version 4.13.2, C++ target.
I have a Lexer and Parser rules as below.
Lexer:
Parser:
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:
I cannot understand, the lexer seems to work fine, but parser cannot always match the first token.
Contributor guide
Research direction
Start by reproducing the reported C++ target generation with the two grammar files and the `antlr4` command shown. Inspect the generated lexer and parser, then compare the token dump from `CommonTokenStream` with the call to `parser.root()`. Done means the input parses without the reported first-token error and the parse tree is printed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100