explicit package declaration in the g4 file (maven build)

208 views
Skip to first unread message

vme...@twitter.com

unread,
Apr 25, 2014, 9:04:43 PM4/25/14
to antlr-di...@googlegroups.com
Apologies if this has been asked before. My quick search could not find any such thread.

I am using: 

    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-maven-plugin</artifactId>
      <version>4.2.2</version>
      <type>maven-plugin</type>
    </dependency>

...
      <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>4.2.2</version>
        <executions>
          <execution>
            <goals>
              <goal>antlr4</goal>
            </goals>
            <configuration>
              <sourceDirectory>src/main/antlr</sourceDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

I have arranged my grammar in the following dir structure:

/fooparser/src/main/antlr/com/foo/Filter.g4

On mvn compile antlr recognizes the directory structure and automatically spits out package at the top of the generated java files:

package com.foo;

Now if I want to explicitly specify the package line in @header it spits out two package lines and the compilation fails.

@header {
package com.foo;
}

Generated java file starts with:

package com.foo; <- automatically generated

package com.foo; <- from @header

How can I workaround this? I have another build system to support besides maven, which I think (??? I am yet to confirm this) is using different version of antlr and it doesn't spit out the package line automatically. That is why I am looking for this workaround.

Please let me know if there is a way to turn off automatic spitting out of package line and depend on me to specify it in the @header.

Thanks!
Vinod.

vme...@twitter.com

unread,
Apr 25, 2014, 9:58:53 PM4/25/14
to antlr-di...@googlegroups.com
I think this specific behavior is limited to the antlr4-maven-plugin. The other build system I was talking about uses org.antlr.v4.Tool  to generate sources. So that is working as expected. java -jar /Users/vmehra/Library/antlr4/antlr-4.2-complete.jar' works as expected as well. 

Please let me know if there is a workaround for the antlr4-maven-plugin to turn off this autogen behavior for the package line.

Thanks!
Vinod.

Sam Harwell

unread,
Apr 25, 2014, 10:11:28 PM4/25/14
to antlr-di...@googlegroups.com

In the non-Maven build configuration, you need to pass the -package flag to the ANTLR 4 tool to let it know what package clause to add. The Maven plugin does this step for you.

 

ANTLR 4 grammars should never need to include a package clause in a @header{} block inside the grammar file itself. It would only be useful if a build system is misconfigured.

 

Sam

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages