51

If I call:

java org.antlr.Tool -o outdir sources/com/example/Java5.g

...with antlr-3.1.3 the parser and lexer code will be generated in the directory outdir/sources/com/example. But the generated classes don't have any package statement. I need them to life in the package com.example.

Is there a way to specify the target package?

tangens
  • 39,095
  • 19
  • 120
  • 139
  • On http://www.jguru.com/faq/view.jsp?EID=16185 they explain how to embed the package inside the grammar. But is there a way to specify it as command line parameter? – tangens Oct 31 '09 at 23:17

4 Answers4

75

ANTLR provides a header tool which allows you to include package and imports. You include this in your *.g grammar file:

@header {
    package org.xmlcml.cml.converters.antlr;
    import java.util.HashMap;
}

And you may need it in the Lexer as well:

@lexer::header {package org.xmlcml.cml.converters.antlr;}

and in case you need to add some members and code:

@members {
    HashMap<String, Object> objectMap = new HashMap<String, Object>();
    //...

    private void addArrayValue(String content) {
    //... code required by snippets in the grammar

    }
}
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
29

An old question with a perfectly good answer, but since the comment on the question asked for a command line option (and that was what I was actually searching for when I got here), I thought I'd just chime in and say the following...

You can specifiy the package on the command line if you are using ANTLR 4. I checked and it seems to not be there in version 3 so the other answer is the way to go for ANTLR 3.

Here is an example:

java -cp antlr-4.4-complete.jar org.antlr.v4.Tool -package my.package MyGram.g4

See the -package option at ANTLR Tool Command Line Options for more information.

Community
  • 1
  • 1
kmp
  • 10,535
  • 11
  • 75
  • 125
  • 1
    The "ANTLR Tool Command Line Options" documentation requires credentials to access. Here's an open documentation of antlr4: https://github.com/antlr/antlr4/blob/master/doc/index.md – EFreak Mar 18 '16 at 21:11
  • ANTLR Tool Command Line Options: https://github.com/antlr/antlr4/blob/master/doc/tool-options.md – EFreak Mar 18 '16 at 21:12
  • Thanks @EFreak - I adjusted the link in the question to your suggestion – kmp Mar 20 '16 at 15:13
  • 3
    any idea why this isn't exposed in the gradle plugin? – Groostav Mar 30 '16 at 07:53
  • 3
    In gradle you can append the argument to the `arguments` property of the `generateGrammarSource` task: `generateGrammarSource {arguments += ["-package", "my.package"]}` – Askin Geeks Nov 04 '16 at 22:25
  • 1
    For an Eclipse newbie... how does one find the generateGrammerSource property? – Ross Youngblood Jan 27 '17 at 17:40
2

For folks using Gradle's Antlr plugin toss this into your build.gradle.kts,

tasks {
    generateGrammarSource {
        val pkg = "com.stackoverflow.antlr"
        arguments = arguments + listOf("-package", pkg)
        outputDirectory = outputDirectory.resolve(pkg.split(".").joinToString("/"))
    }
}

The outputDirectory part might not be needed, but for whatever reason Antlr does not put generated files into the package directory layout even though you give it a package. I wonder if this is a bug because the documentation mentions another option which makes it sound like it tries to. Either way, this works for me.

-Xexact-output-dir all output goes into -o dir regardless of paths/package

(Also, I am on Windows and the / instead of \\ seems to be working just fine.)

Captain Man
  • 6,997
  • 6
  • 48
  • 74
  • Your answer really helped me. Generated sources are placed in right folder/package. I removed this line `arguments = arguments + listOf("-package", pkg)` and added `@header { package com.stackoverflow.antlr; }` after `grammar ...` line in .g4 file. – Evgeny Cheryomushkin Mar 29 '23 at 08:56
0

It may have changed in more recent versions.
I'm working on a DSL for a Savantly game service, and I place my grammar files in subfolders. The generated classes are in a package corresponding to the folder structure.

My plugin configuration looks like this -

<plugin>
    <groupId>org.antlr</groupId>
    <artifactId>antlr4-maven-plugin</artifactId>
    <version>${antlr.version}</version>
    <configuration>
        <sourceDirectory>${basedir}</sourceDirectory>
        <includes>
           <include>**/*.g4</include>
        </includes>
        <visitor>true</visitor>
        <listener>true</listener>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>antlr4</goal>
            </goals>
        </execution>
    </executions>
</plugin>

And the resulting folder structure -

enter image description here

Jeremy
  • 2,970
  • 1
  • 26
  • 50