0
votes

I am trying to create a parser using antlr3 and I have problems using java classes from the same project.

I have built a second project, added the maven dependency and the import in the header and lexer:::header of my grammar and everything works OK.

But I want to be able to have everything in one project.

Do I have to include something in my .pom or my grammar headers to get that to work? I tried putting the import in the headers but that did not work. Any ideas?

Currently I have these in my pom:

<dependencies><dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr</artifactId>
    <version>3.3</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency></dependencies>

<build>

    <plugins>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr3-maven-plugin</artifactId>
            <version>3.3</version>
            <executions>
                <execution>
                    <id>run antlr</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>antlr</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${basedir}/target/generated-sources/antlr3</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>

</build>

I also include the first line of my grammar. The lines in comments are the ones that I use when I get it to work with a second project containing my custom classes:

grammar GCQLParser;

@header {
    package gr.uoa.di.madgik.search.parser.gcqlparser;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.List;
    import gr.uoa.di.madgik.search.parser.gcqlparser.tree.*;
    //import search.library.util.cql.query.tree.*;
}
@lexer::header {
    package gr.uoa.di.madgik.search.parser.gcqlparser;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.List;
    import gr.uoa.di.madgik.search.parser.gcqlparser.tree.*;
    //import search.library.util.cql.query.tree.*;
}

And here is a rule that uses custom classes:

booleanOp returns[GCQLBooleanNode node]
: AND  { $node = new GCQLAndNode(); }
| OR   { $node = new GCQLOrNode(); } 
| NOT  { $node = new GCQLNotNode(); }
| PROX { $node = new GCQLProxNode(); }
;
1
What is your maven configuration? How do you create the java classes for the grammar? (antlr script or through maven?)Absurd-Mind
I added parts of my .pom, and I use maven to create the parser/lexer filesPanagiotis
and please describe the "not working"-part a bit better. Is there an error when you execute mvn compile?Absurd-Mind
No. Just a hint to make your build simpler.khmarbaise
Yes, it is possible. The ANTLR 3, ANTLR 4, StringTemplate 3, and StringTemplate 4 projects all use parsers generated for grammars within the same project without problems.Sam Harwell

1 Answers

0
votes

Maybe you are missing the @header declaration

example grammar.g:

@header {
    package test.grammar;

    import test.other.*;
}

@header::lexer {
    package test.grammar;
}

@members {
     public void test() {
          new TestClass(); // test.other.TestClass
     }
}

make sure your pom.xml contains the following:

<build>
  <plugins>
    <plugin>
      <groupId>org.antlr</groupId>
      <artifactId>antlr3-maven-plugin</artifactId>
      <version>3.3</version>
      <executions>
        <execution>
          <goals>
            <goal>antlr</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

<dependencies>
  <dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr-runtime</artifactId>
    <version>3.1</version>
  </dependency>
</dependencies>

EDIT: If you are using antlr3-maven-plugin you need at least a 3.1 antlr runtime