4
votes

I'm trying to call an Antlr task in my Ant build.xml as follows:

<path id="classpath.build">
  <fileset dir="${dir.lib.build}" includes="**/*.jar" />
</path>

...

<target name="generate-lexer" depends="init">
  <antlr target="${file.antlr.lexer}">
    <classpath refid="classpath.build"/>
  </antlr>
</target>

But Ant can't find the task definition. I've put all of the following in that dir.lib.build:

  • antlr-3.1.jar
  • antlr-2.7.7.jar
  • antlr-runtime-3.1.jar
  • stringtemplate-3.2.jar

But none of those seems to have the task definition. (I've also tried putting those jars in my Ant classpath; same problem.)

6

6 Answers

4
votes

The current Antlr-task jar is available at http://www.antlr.org/share/1169924912745/antlr3-task.zip

It can be found on the antlr.org website under the "File Sharing" heading.

2
votes

You should use the antlrall.jar jar. You can go ahead and just drop it into your Ant installation but that does mean that it will only work for that one install. We check the jar in and use taskdef to load the jar file so that it doesn't become another step for developers when they start on the team or move to a new computer.

2
votes

I just got this working for myself. Took me an hour. ugh. anyway,

Step 1: download ant-antlr3 task from

http://www.antlr.org/share/1169924912745/antlr3-task.zip

Step 2: copy to where ant can see it. My mac:

sudo cp /usr/local/lib/ant-antlr3.jar /usr/share/ant/lib/

my linux box:

sudo cp /tmp/ant-antlr3.jar /usr/local/apache-ant-1.8.1/lib/

Step 3: make sure antlr2, antlr3, ST are in classpath. All in one is here:

http://antlr.org/download/antlr-3.3-complete.jar

Step 4: use in build.xml

<path id="classpath">
    <pathelement location="${antlr3.jar}"/>
    <pathelement location="${ant-antlr3.jar}"/>
</path>

<target name="antlr" depends="init">
    <antlr:ant-antlr3 xmlns:antlr="antlib:org/apache/tools/ant/antlr"
        target="src/T.g"
        outputdirectory="build">
        <classpath refid="classpath"/>
    </antlr:ant-antlr3>
</target>

Just added a faq entry:

http://www.antlr.org/wiki/pages/viewpage.action?pageId=24805671

1
votes

The most basic way to run Antlr is to execute the Antlr JAR:

<project default="antlr">
  <target name="antlr">
    <java jar="antlr-4.1-complete.jar" fork="true">
      <arg value="grammar.g4"/>
    </java>
  </target>
</project>

This is a bit slower, because it forks the JVM and it runs Antlr even if the grammar did not change. But it works in the same way with every Antlr version and does not need any special targets.

0
votes

On Ubuntu this should make it available:

sudo apt-get install ant-optional

0
votes

Additional info on top of what everybody else contributed so far:

The ant-optional package in Ubuntu includes the task shipped with Ant 1.8.2 which is a task for ANTLR 2.7.2 so this will fail with an error as described in this post. The method described by Terence is the best way to use the ANTLR3 task.

If you do not have root access on a Linux machine, you can install the ant-antlr3.jar file in the Ant user directory: ~/.ant/lib. Check with ant -diagnostics whether ant-antlr3.jar is visible to Ant, as explained in this other post.

If you are using Eclipse, you will need to restart the IDE before it recognises the new task and you will also need to include antlr3.jar and stringtemplate.jar in your classpath (but ant-antlr3.jar is not necessary).