antlr / antlr/antlr4

Always error: line 1:0 mismatched input 'Token' expecting {<EOF>,Token}

Open
#4,672 5 comments 0 reactions 0 assignees View on GitHub
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:
image

Parser:
image

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:
image

I cannot understand, the lexer seems to work fine, but parser cannot always match the first token.

Contributor guide

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.