Generated parser code with antlr4.11.1 contains error

Viewed 72

I create a compiler with antlr and llvm in c++. I've created the two .g4 files and in my CMakeLists.txt I call the antlr jar to generate the lexer and the parser. Then I compile all my files with the same CMakeLists file and I got these errors :

Filc/src/generated/FilParser.cpp:803:16: error: invalid use of member function ‘antlrcppfil::FilParser::ExceptionContext* antlrcppfil::FilParser::ExprContext::exception()’ (did you forget the ‘()’ ?)
  803 |     _localctx->exception = std::current_exception();
      |     ~~~~~~~~~~~^~~~~~~~~
      |                         ()
Filc/src/generated/FilParser.cpp:804:43: error: invalid use of non-static member function ‘antlrcppfil::FilParser::ExceptionContext* antlrcppfil::FilParser::ExprContext::exception()’
  804 |     _errHandler->recover(this, _localctx->exception);
      |                                ~~~~~~~~~~~^~~~~~~~~

I looking in some tutorials and on stackoverflow why i've got these errors. But I didn't found any mention of that. And in tutorials code they have the same code as me, but it compile.

So I tried to update my antlr version to version 4.11.1 (originally I use 4.8) but it doesn't change anything.

For information, I use c++ 17, and this is my CMakeLists.txt :

project(Filc)

cmake_minimum_required(VERSION 3.10)

set (CMAKE_CXX_STANDARD 17)

set(antlr4-jar ${PROJECT_SOURCE_DIR}/bin/antlr-4.11.1-complete.jar)

set(antlr4-output
        ${PROJECT_SOURCE_DIR}/src/generated/FilLexer.cpp
        ${PROJECT_SOURCE_DIR}/src/generated/FilLexer.h
        ${PROJECT_SOURCE_DIR}/src/generated/FilParser.cpp
        ${PROJECT_SOURCE_DIR}/src/generated/FilParserBaseVisitor.cpp
        ${PROJECT_SOURCE_DIR}/src/generated/FilParserBaseVisitor.h
        ${PROJECT_SOURCE_DIR}/src/generated/FilParserVisitor.cpp
        ${PROJECT_SOURCE_DIR}/src/generated/FilParserVisitor.h
)

add_custom_target(GenerateLexerParser
        COMMAND java -jar ${antlr4-jar} -Dlanguage=Cpp -visitor -no-listener -o ${PROJECT_SOURCE_DIR}/src/generated/ -package antlrcppfil ${PROJECT_SOURCE_DIR}/src/antlr/FilLexer.g4 ${PROJECT_SOURCE_DIR}/src/antlr/FilParser.g4
        DEPENDS ${PROJECT_SOURCE_DIR}/src/antlr/FilLexer.g4 ${PROJECT_SOURCE_DIR}/src/antlr/FilParser.g4
)

include_directories(
        src
        src/generated
        src/utils
        antlr-runtime/src
)

add_executable(filc
        src/main.cpp
        src/utils/cxxopts.hpp
        src/generated/FilLexer.cpp
        src/generated/FilLexer.h
        src/generated/FilParser.cpp
        src/generated/FilParserBaseVisitor.cpp
        src/generated/FilParserBaseVisitor.h
        src/generated/FilParserVisitor.cpp
        src/generated/FilParserVisitor.h
)

add_dependencies(filc  GenerateLexerParser)

target_link_libraries(filc  antlr4-runtime.a)

install(TARGETS filc DESTINATION bin)

[Edit]

Someone found the solution on the antlr github https://github.com/antlr/antlr4/issues/3876

2 Answers

For reference purposes, in case anyone else searches for this, it was determined that the grammar defined a keyword exception and this clashed with the generated output. The exception keyword will be escaped in a future ANTLR release. For now, the grammar was changed by renaming the token.

I tried the c++ target on one of my project and it compiled correctly in 4.9.3.
Are you sure you didn't mix the version of the runtime you use and the version of antlr4 you used to generate the parser? You need the same version for both.

Related