Skip to content
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

CPP Target: Unknown error -1 #4499

Closed
4 tasks done
devNamanG opened this issue Dec 26, 2023 · 6 comments
Closed
4 tasks done

CPP Target: Unknown error -1 #4499

devNamanG opened this issue Dec 26, 2023 · 6 comments

Comments

@devNamanG
Copy link

  • I have reproduced my issue using the latest version of ANTLR
  • I have asked at stackoverflow
  • Responses from the above seem to indicate that my issue could be an ANTLR bug
  • I have done a search of the existing issues to make sure I'm not sending in a duplicate

My ANTLR Version: 4.13.1
Target: Cpp
Compiler: Ninja, CMake and GCC, CMAKE both give this error
I am using Ubuntu 20.04

Clarification: I have not asked at StackOverflow about my issue, but there already exists an question about this issue there, which can be found here which uses ANTLR version 4.11.1, and the response there indicates that it might be an ANTLR bug.

Also, I am able to reproduce this in the latest ANTLR release too. Hence I am filing the issue here.

This issue can be reproduced using the runtime/Cpp/demo project too, hence I am not providing a minimum reproducible grammar or code.

Expected Behaviour: Correct Lexing and Parsing without error.
Actual Behaviour:

$ ./antlr4-demo
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted

Also, I tried to do some debugging, and it turns out that this error is throws when the TLexer object is initialized. More specifically, the error comes from the static method named initialize inside the generated lexer class.

Please note that the build and linking is successfull, and I have linked the PThreads library.

Please look into this as soon as possible. Thank you very much! :)

@kaby76
Copy link
Contributor

kaby76 commented Dec 28, 2023

Please provide a stack trace for the crash. Best if you provide a repo containing the example too.

@devNamanG
Copy link
Author

devNamanG commented Dec 28, 2023

Yeah sure, but you can reproduce the same with the runtime/Cpp/demo project too in this repository. Just compile the runtime with -DWITH_DEMO=TRUE and then run the executable in the generated run/share directory, you'll get the error too. Here's the link to the ANTLR4 Cpp runtime. And I'll provide a stack trace in some time. Thank you!

Stack Trace
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7bf0859 in __GI_abort () at abort.c:79
#2  0x00007ffff7e798d1 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7e8537c in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7e853e7 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff7e85699 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7e7c6fd in std::__throw_system_error(int) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x0000555555570b65 in std::call_once (__once=..., 
    __f=@0x55555556b503: {void (void)} 0x55555556b503 <(anonymous namespace)::tlexerLexerInitialize()>) at /usr/include/c++/9/mutex:691
#8  0x000055555556fb9f in antlr4::internal::call_once (onceFlag=..., 
    callable=@0x55555556b503: {void (void)} 0x55555556b503 <(anonymous namespace)::tlexerLexerInitialize()>)
    at ~/Downloads/antlr4/runtime/Cpp/runtime/src/internal/Synchronization.h:150
#9  0x000055555556e57b in antlrcpptest::TLexer::initialize () at ~/Downloads/antlr4/runtime/Cpp/demo/generated/TLexer.cpp:285
#10 0x000055555556e108 in antlrcpptest::TLexer::TLexer (this=0x7fffffffdc80, input=0x7fffffffdb30)
    at ~/Downloads/antlr4/runtime/Cpp/demo/generated/TLexer.cpp:181
--Type  for more, q to quit, c to continue without paging--
#11 0x000055555556a314 in main () at ~/Downloads/antlr4/runtime/Cpp/demo/Linux/main.cpp:24
Edit: Correction to the executable's location

Edit 2: Added the stack trace

@kaby76
Copy link
Contributor

kaby76 commented Dec 28, 2023

The demo cmake files are ancient.

Let's start with the basics. Forget CMake. Install g++, java, python3, pip, and antlr4-tools. This Bash script works on my Linux box.

rm -rf antlr4
git clone https://github.com/antlr/antlr4.git
cd antlr4
git checkout master
cd runtime/Cpp/demo
cp Linux/* .
rm -rf Mac Windows CMakeLists.txt README.md generate.cmd generate.sh Linux
ls

java --version
antlr4
g++ --version

antlr4 -Dlanguage=Cpp -package antlrcpptest *.g4
g++ -std=c++17 `find ../runtime/src/ -name '*.cpp'` -pthread -g -o ./demo -I ../runtime/src/ ./*.cpp
# g++ -std=c++17 `find ../runtime/src/ -name '*.cpp'`  -g -o ./demo -I ../runtime/src/ ./*.cpp   # does not work.
./demo

And, here's the output.

ken@DESKTOP-DL44R7B:~/so77347027$ bash test.sh
Cloning into 'antlr4'...
remote: Enumerating objects: 134547, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 134547 (delta 3), reused 13 (delta 1), pack-reused 134526
Receiving objects: 100% (134547/134547), 68.04 MiB | 25.05 MiB/s, done.
Resolving deltas: 100% (79323/79323), done.
Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'master'
TLexer.g4  TParser.g4  main.cpp
openjdk 11.0.19 2023-04-18
OpenJDK Runtime Environment (build 11.0.19+7-post-Ubuntu-0ubuntu120.04.1)
OpenJDK 64-Bit Server VM (build 11.0.19+7-post-Ubuntu-0ubuntu120.04.1, mixed mode, sharing)
ANTLR Parser Generator  Version 4.13.1
 -o ___              specify output directory where all output is generated
 -lib ___            specify location of grammars, tokens files
 -atn                generate rule augmented transition network diagrams
 -encoding ___       specify grammar file encoding; e.g., euc-jp
 -message-format ___ specify output style for messages in antlr, gnu, vs2005
 -long-messages      show exception details when available for errors and warnings
 -listener           generate parse tree listener (default)
 -no-listener        don't generate parse tree listener
 -visitor            generate parse tree visitor
 -no-visitor         don't generate parse tree visitor (default)
 -package ___        specify a package/namespace for the generated code
 -depend             generate file dependencies
 -D<option>=value    set/override a grammar-level option
 -Werror             treat warnings as errors
 -XdbgST             launch StringTemplate visualizer on generated code
 -XdbgSTWait         wait for STViz to close before continuing
 -Xforce-atn         use the ATN simulator for all predictions
 -Xlog               dump lots of logging info to antlr-timestamp.log
 -Xexact-output-dir  all output goes into -o dir regardless of paths/package
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

^C
ken@DESKTOP-DL44R7B:~/so77347027$ e test.sh
ken@DESKTOP-DL44R7B:~/so77347027$ bash test.sh
Cloning into 'antlr4'...
remote: Enumerating objects: 134547, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 134547 (delta 3), reused 13 (delta 1), pack-reused 134526
Receiving objects: 100% (134547/134547), 68.04 MiB | 28.60 MiB/s, done.
Resolving deltas: 100% (79323/79323), done.
Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'master'
TLexer.g4  TParser.g4  main.cpp
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:        20.04
Codename:       focal
openjdk 11.0.19 2023-04-18
OpenJDK Runtime Environment (build 11.0.19+7-post-Ubuntu-0ubuntu120.04.1)
OpenJDK 64-Bit Server VM (build 11.0.19+7-post-Ubuntu-0ubuntu120.04.1, mixed mode, sharing)
ANTLR Parser Generator  Version 4.13.1
 -o ___              specify output directory where all output is generated
 -lib ___            specify location of grammars, tokens files
 -atn                generate rule augmented transition network diagrams
 -encoding ___       specify grammar file encoding; e.g., euc-jp
 -message-format ___ specify output style for messages in antlr, gnu, vs2005
 -long-messages      show exception details when available for errors and warnings
 -listener           generate parse tree listener (default)
 -no-listener        don't generate parse tree listener
 -visitor            generate parse tree visitor
 -no-visitor         don't generate parse tree visitor (default)
 -package ___        specify a package/namespace for the generated code
 -depend             generate file dependencies
 -D<option>=value    set/override a grammar-level option
 -Werror             treat warnings as errors
 -XdbgST             launch StringTemplate visualizer on generated code
 -XdbgSTWait         wait for STViz to close before continuing
 -Xforce-atn         use the ATN simulator for all predictions
 -Xlog               dump lots of logging info to antlr-timestamp.log
 -Xexact-output-dir  all output goes into -o dir regardless of paths/package
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

../runtime/src/Vocabulary.cpp: In static member function ‘static void antlr4::dfa::Vocabulary::__static_initialization_and_destruction_0(int, int)’:
../runtime/src/Vocabulary.cpp:12:18: warning: ‘antlr4::dfa::Vocabulary::EMPTY_VOCABULARY’ is deprecated: Use the default constructor of Vocabulary instead. [-Wdeprecated-declarations]
   12 | const Vocabulary Vocabulary::EMPTY_VOCABULARY;
      |                  ^~~~~~~~~~
../runtime/src/Vocabulary.cpp:12:18: note: declared here
[@0,0:0='🍴',<6>,1:0]
[@1,1:1=' ',<27>,channel=99,1:1]
[@2,2:2='=',<9>,1:2]
[@3,3:3=' ',<27>,channel=99,1:3]
[@4,4:4='🍐',<6>,1:4]
[@5,5:5=' ',<27>,channel=99,1:5]
[@6,6:6='+',<13>,1:6]
[@7,7:7=' ',<27>,channel=99,1:7]
[@8,8:10='"😎"',<22>,1:8]
[@9,11:11=';',<12>,1:11]
[@10,12:12='(',<16>,1:12]
[@11,13:13='(',<16>,1:13]
[@12,14:14='(',<16>,1:14]
[@13,15:15='x',<6>,1:15]
[@14,16:16=' ',<27>,channel=99,1:16]
[@15,17:17='*',<15>,1:17]
[@16,18:18=' ',<27>,channel=99,1:18]
[@17,19:19='π',<6>,1:19]
[@18,20:20=')',<17>,1:20]
[@19,21:21=')',<17>,1:21]
[@20,22:22=')',<17>,1:22]
[@21,23:23=' ',<27>,channel=99,1:23]
[@22,24:24='*',<15>,1:24]
[@23,25:25=' ',<27>,channel=99,1:25]
[@24,26:26='µ',<6>,1:26]
[@25,27:27=' ',<27>,channel=99,1:27]
[@26,28:28='+',<13>,1:28]
[@27,29:29=' ',<27>,channel=99,1:29]
[@28,30:30='∰',<6>,1:30]
[@29,31:31=';',<12>,1:31]
[@30,32:32=' ',<27>,channel=99,1:32]
[@31,33:33='a',<6>,1:33]
[@32,34:34=' ',<27>,channel=99,1:34]
[@33,35:35='+',<13>,1:35]
[@34,36:36=' ',<27>,channel=99,1:36]
[@35,37:37='(',<16>,1:37]
[@36,38:38='x',<6>,1:38]
[@37,39:39=' ',<27>,channel=99,1:39]
[@38,40:40='*',<15>,1:40]
[@39,41:41=' ',<27>,channel=99,1:41]
[@40,42:42='(',<16>,1:42]
[@41,43:43='y',<6>,1:43]
[@42,44:44=' ',<27>,channel=99,1:44]
[@43,45:45='?',<20>,1:45]
[@44,46:46=' ',<27>,channel=99,1:46]
[@45,47:47='0',<4>,1:47]
[@46,48:48=' ',<27>,channel=99,1:48]
[@47,49:49=':',<11>,1:49]
[@48,50:50=' ',<27>,channel=99,1:50]
[@49,51:51='1',<4>,1:51]
[@50,52:52=')',<17>,1:52]
[@51,53:53=' ',<27>,channel=99,1:53]
[@52,54:54='+',<13>,1:54]
[@53,55:55=' ',<27>,channel=99,1:55]
[@54,56:56='z',<6>,1:56]
[@55,57:57=')',<17>,1:57]
[@56,58:58=';',<12>,1:58]
[@57,59:58='<EOF>',<-1>,1:59]
(main (stat (expr (id 🍴)) = (expr (expr (id 🍐)) + (expr "😎")) ;) (stat (expr (expr (expr ( (expr ( (expr ( (expr (expr (id x)) * (expr (id π))) )) )) )) * (expr (id µ))) + (expr (id ∰))) ;) (stat (expr (expr (id a)) + (expr ( (expr (expr (expr (id x)) * (expr ( (expr (expr (id y)) ? (expr 0) : (expr 1)) ))) + (expr (id z))) ))) ;) <EOF>)

ken@DESKTOP-DL44R7B:~/so77347027$

@devNamanG
Copy link
Author

The demo cmake files are ancient.

I agree, they are ancient. But if so, then shouldn't they be updated to ensure that the instructions are applicable for the latest version of ANTLR?

Also, I was already including pthread for compilation, and the error was still there.

This Bash script works on my Linux box.

Thank you! This works on mine too, but I can't use g++ directly for compilation in my project. Do you know what changes I need to make in order to ensure that ANTLR version 4.13.1 works with CMake + Ninja?

I am providing the relevant parts of my CMakeLists.txt.

cmake_minimum_required(VERSION 3.28.1)
project(Agohya VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)

# Adding the source files
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.h")

include(CTest)
enable_testing()

# Include -lpthreads, mandatory for ANTLR to work
find_package(Threads REQUIRED)

set(ANTLR4_JAR_LOCATION ${CMAKE_SOURCE_DIR}/external/antlr-4.13.1-complete.jar)

set(ANTLR4_ZIP_REPOSITORY ${CMAKE_SOURCE_DIR}/external/antlr4-master.zip)

LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake )

include( ExternalAntlr4Cpp )

# Set the path to the Generated Files Directory
set(ANTLR4_GENERATED_SRC_DIR ${CMAKE_SOURCE_DIR}/src/antlr_gen)

# Add a custom target to generate lexer and parser files
add_custom_target(antlr4_generate
    COMMAND java -jar external/antlr-4.13.1-complete.jar -Dlanguage=Cpp -o ${ANTLR4_GENERATED_SRC_DIR} -listener -visitor -package nsagohya ${CMAKE_CURRENT_SOURCE_DIR}/grammar/AgohyaLexer.g4 ${CMAKE_CURRENT_SOURCE_DIR}/grammar/AgohyaParser.g4
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    COMMENT "Generating lexer and parser files with ANTLR4"
)

include_directories( ${ANTLR4_INCLUDE_DIRS} )
link_directories( ${ANTLR4_SHARED_LIBRARIES} )
message(STATUS "Found antlr4cpp libs: ${ANTLR4_SHARED_LIBRARIES} and includes: ${ANTLR4_INCLUDE_DIRS} ")

# Include the generated files
include_directories(${ANTLR4_INCLUDE_DIRS})

# Include the generated source directory for headers
include_directories(${ANTLR4_GENERATED_SRC_DIR})

add_executable(Agohya ${ANTLR4_GENERATED_SRC_DIR} ${SRC_FILES} )

add_dependencies(Agohya antlr4_generate)

target_link_libraries(Agohya Threads::Threads antlr4_shared)

@kaby76
Copy link
Contributor

kaby76 commented Dec 28, 2023

I'm not sure how to best modify your CMakeList.txt to get things to work as I'm not an expert at CMake. Instead, I took the one from the demo/ directory then modified a few things, adjust the flags for compiling with -pthread, which won't be portable to other compilers.

Here is a bash file that works on Ubuntu.
test2.sh.txt

And the output from the build.
test2.sh.output.txt

Indeed, the demo should be updated with build files that work.

@devNamanG
Copy link
Author

Ohh okay, Thank you very much though! @kaby76

Since this issue is occurring by following the Demo, I suppose it is an ANTLR issue. I don't think I'll be able to fix this myself, but I'll try to check if I can tweak some stuff and fix this.

But I would be really grateful if someone from the maintainers can help me with the CMakeLists.txt file. Thank you!

@devNamanG devNamanG closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants