Skip to content
Advertisement

Antlr4 Project Directory Structure Issue

I’m trying to set up an Antlr4 project using Java’s package directory structure.

The top level directory contains PondParser.g4, PondLexer.g4, and build_script.

build_script is expected to build and run the antlr4 test rig

JavaScript

Relevant PondParser.g4 header and options:

JavaScript

Relevant PondLexer.g4 header and options:

JavaScript

The build script fails while compiling (javac ./com/sevan/pond/*.java ).

The specific errors are as follows:

JavaScript

What’s missing/wrong here? Any suggestions, links, and answers would be greatly appreciated.


Update

The four errors above were due to a double package declaration in the generated java files. This was caused by specifying the package using -package and @header. After fixing this, however, the test rig still fails with Can't load PondParser as lexer or parser.

Advertisement

Answer

Shouldn’t that be:

JavaScript

?

I.e.: you want to add the current working dir to the classpath (.), as well as the ANTLR lib.

EDIT

Here’s a working demo (just tested it myself).

Files/folder structure:

JavaScript

Navigate to where antlr-4.8-complete.jar resides.

Generate lexer and parser classes:

JavaScript

And compile all generated .java files:

JavaScript

Fianlly, run TestRig as follows:

JavaScript

You can now enter some text and can terminate by pressing CTRL+D. I typed abcn and got this:

enter image description here

For reference, heres what the grammars look like:

PondLexer.g4

JavaScript

PondParser.g4

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement