-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always error: line 1:0 mismatched input 'Token' expecting {<EOF>,Token} #4672
Comments
You need to "reset" the lexer after getting the tokens. The lexer is positioned at the end of input after printing out the tokens. This is how it is done in testing the grammar/target in the grammars-v4 repo: https://github.com/antlr/grammars-v4/blob/feb9e9e082842d9cafed1e1558e9d1aad7052b87/_scripts/templates/Cpp/st.Test.cpp#L58-L68 |
Well, I did what you said as below:
But it still not works, the same error is triggered. However, I then put the lexer grammar and parser grammar into a single grammar file, and generate the cpp code. It turns out to work fine now. I don't know why, I guess this should be a quite serious bug. |
It's likely a bug on your part. The split grammar works fine.
I even took your code, added the missing
(Env: Windows 11, MSYS2) |
I am using the ANTLR4 cpp runtime as a library. First I compile the cpp-runtime without demo in the main repository (https://github.com/antlr/antlr4/tree/master/runtime/Cpp) as below:
Then I dynamically link the generated libantlr4-runtime.so to another c++ project. And I use the grammar files to generate parser code at this time, then the error is triggered. I tested the parser functions using the dynamically linked c++ project both on MacOS and Ubuntu 22.04, and the error emerged on both systems. When I combined the lexer and parser grammar files into a single one, it turned out to be fine both on MacOS and Ubuntu 22.04. |
Below is the Bash script I cannot reproduce the problem. You're going to have to fill in the details by providing a complete "build.sh" script, similar to the one I provide above, that reproduces the issue. I suggest you use the The split grammar and program code works on Windows 11 and Ubuntu 20.04, static and dynamically linked. Here's the output of the script on Ubuntu: |
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:
`
`
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.
The text was updated successfully, but these errors were encountered: