antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.3k stars 3.3k forks source link

Syntax and other errors when using the Cpp runtime of ANTLR4 with grammars generated with antlr4.13.2-complete.jar #4674

Closed peytonk132 closed 3 months ago

peytonk132 commented 3 months ago

Hello everyone, I've been trying to create a parser using antlr in VS 2022 for awhile now but I can never seem to shake these syntax errors. I had originally posted on Stackoverflow as requested but I didn't really get any answers there even though the people were very helpful. I had originally generated grammar files from https://github.com/antlr/grammars-v4/tree/master/fortran/fortran90 for the Cpp targets and in my folly hadn't used the transformGrammars.py file but had then used them after realizing my mistake and still got the errors. They range from various things like certain classes not being base class members to general syntax errors. The one file that i think holds the key to fixing a lot of other errors is ParseTreeType.h. These are my errors for for that file:

ParseTreeType.h(20,13): error C2143: syntax error: missing '}' before 'constant'
ParseTreeType.h(20,13): error C2059: syntax error: 'constant'
ParseTreeType.h(22,9): error C2143: syntax error: missing ';' before '}'
ParseTreeType.h(25,1): error C2059: syntax error: '}'
ParseTreeType.h(25,1): error C2143: syntax error: missing ';' before '}'

and this is the file I have:

/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

#pragma once

#include <cstddef>

#include "../mainfunc/antlr-files/antlr4-common.h"

namespace antlr4
{
    namespace tree
    {

        enum class ParseTreeType : size_t
        {
            TERMINAL = 1,
            ERROR = 2,
            RULE = 3,
        };

    } // namespace tree
} // namespace antlr4

Has anybody else experienced this before? Also, almost all the include files in the runtime src files were completely wack and didn't work at all. Could this be a bug or am I just stupid?

kaby76 commented 3 months ago

Did you follow my comments to first build the runtime then build the app that I wrote for you?

The grammar works for Cpp on Windows with VS2022.

PS C:\msys64\home\Kenne\fortran90-Cpp> msbuild .\MyParser.sln -p:Configuration=Debug > out.txt
PS C:\msys64\home\Kenne\fortran90-Cpp> .\x64\Debug\MyParser.exe C:\msys64\home\Kenne\issues\g4-current\fortran\fortran90\examples\flang\test\f90_correct\src\ac02.f90
Cpp 0 C:\msys64\home\Kenne\issues\g4-current\fortran\fortran90\examples\flang\test\f90_correct\src\ac02.f90 success 0.688818
Total Time: 0.729936
PS C:\msys64\home\Kenne\fortran90-Cpp>

As I mentioned in SO, you may have a #define that is conflicting with the runtime. But, you need to first verify that your environment is at least set up correctly. You don't post the source code for your program, nor the .vcxproj. Every detail has to be checked, so it would be best that you wrap all the complete source and post a .zip or create a repo in github.com so we can see what you are doing.

I've updated the instructions in the Wiki to describe how to write a program with the grammars in the repo, including the bit about transformGrammar.py. But, as I mentioned there, it is better to start with trgen instead of writing the program from scratch.

But I now see #include "../mainfunc/antlr-files/antlr4-common.h" in ParseTreeTypes.h. You are not even using the runtime https://github.com/antlr/antlr4/blob/811b7fda58bd14d7f0abc496b6fd651dfa01ed97/runtime/Cpp/runtime/src/tree/ParseTreeType.h. You modified it! Who knows what runtime you are using.

kaby76 commented 3 months ago

I found the project you are working on here: https://github.com/peytonk132/FortIDE. Please check in the code you are working on (preferably in a branch of main git clone https://github.com/peytonk132/FortIDE.git; cd FortIDE; git branch with-antlr; git checkout with-antlr; ...... (modify with your new code) ...; (add, commit, and push changes).. Then, close this issue (https://github.com/antlr/antlr4/issues/4674). I can help you get this compiling.

Basically, you should not be modifying the Antlr4 sources. You need to change the .vcxproj to add in an include path for every library you are referencing. It's likely that you also aren't specifying the language level correctly, among other things.

peytonk132 commented 3 months ago

alright I made a seperate with-antlr branch.

kaby76 commented 3 months ago

Just in case someone happens to see the same compilation errors (e.g., ParseTreeType.h(17,5): error C2143: syntax error: missing '}' before 'constant' or IntStream.h(30,64): error C2589: '(': illegal token on right side of '::'), it is caused by #define conflicts when including windows.h before Antlr4 runtime headers. One way to fix this is to silo the code for windows from the code that uses antlr4. But, it may just as well be solved by reversing the order of the #include. In addition, I found (at least for the driver for the Cpp target used in grammars-v4) that after #include <windows.h>, I also needed to add #undef min. Apparently, min() is defined as a macro. Such as life with C++ and the preprocessor.