zell
zell

Reputation: 10204

A sample of build.xml for ANTLR project?

A project's building process is suffering, unless it becomes automatic.

I have started with ANTLR since recently. ANT seems to be the very building tool for that purpose. Compile, jar, and test... But I have found little code source of the script build.xml for that purpose.

So would you guys would like to share your template build.xml for your antlr project (either Java task or ANTLR task will be fine)? Thanks.

Upvotes: 2

Views: 1482

Answers (2)

Brad Mace
Brad Mace

Reputation: 27916

Here's the core pieces of mine, which I think integrates a little better. I'm not sure when ANTLR's -make option was added--I'm using 3.2.

  • It assumes that grammars are kept in the packages where their generated parsers will be going.
  • Keeps generated source files separate from normal source files so that they can be cleaned
  • Only regenerates parser+lexer sources when they are older than grammar file
  • multiple grammars can be processed in a single pass
  • ANTLR errors are reported correctly by ant

<project name="MyProject">
    <property name="lib.antlr" value="lib/antlr-3.2.jar" />

    <property name="src.dir" value="${user.dir}" />
    <property name="src.java" value="${src.dir}/java" />
    <property name="build.dir" value="build" />
    <property name="build.src" value="${build.dir}/src" />
    <property name="build.classes" value="${build.dir}/classes" />

    <path id="compile.class.path">
        <pathelement location="${build.classes}" />
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${reports.dir}" />
    </target>

    <target name="generate" description="Generate parsers from ANTLR grammars">
        <mkdir dir="${build.src}" />
        <java jar="${lib.antlr}" fork="true" dir="${src.java}" failonerror="true">
            <arg value="-verbose" />
            <arg value="-make" />
            <arg value="-o" />
            <arg path="${build.src}" />
            <arg value="com/example/io/Foo.g" />
            <arg value="com/example/text/Bar.g" />
        </java>

    </target>

    <target name="compile" depends="generate">
        <property name="javac.debug" value="on" />
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.classes}" />
        <javac destdir="${build.classes}" source="1.6" target="1.6" includeantruntime="false" debuglevel="lines,vars,source">
            <src path="${src.java}" />
            <src path="${build.src}" />
            <include name="com/example/**/*.java" />
            <classpath refid="compile.class.path"/>
        </javac>
    </target>
</project>

You can also look at How to use ant with ANTLR3.

Upvotes: 0

Bart Kiers
Bart Kiers

Reputation: 170308

This is roughly what I use:

<?xml version="1.0" encoding="UTF-8"?>

<project name="YourProject">

    <property name="main.package"    value="yourproject"/>
    <property name="parser.package"  value="${main.package}/parser"/>
    <property name="main.src.dir"    value="src/main"/>
    <property name="test.src.dir"    value="src/test"/>
    <property name="grammar.src.dir" value="src/grammar"/>
    <property name="grammar.file"    value="${grammar.src.dir}/YourGrammar.g"/>
    <property name="build.dir"       value="build"/>
    <property name="classes.dir"     value="${build.dir}/classes"/>
    <property name="main.build.dir"  value="${classes.dir}/main"/>
    <property name="test.build.dir"  value="${classes.dir}/test"/>

    <path id="classpath">
        <pathelement location="${main.src.dir}"/>
        <pathelement location="${test.src.dir}"/>
        <pathelement location="${main.build.dir}"/>
        <pathelement location="${test.build.dir}"/>

        <!-- the ANTLR jar is in the lib directory, of course -->
        <fileset dir="lib">
                <include name="*.jar"/>
        </fileset>
    </path>

    <!-- init target -->

    <target name="compile" depends="init" description="Compiles all source files.">
        <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false">
            <classpath refid="classpath"/>
        </javac>
        <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
            <classpath refid="classpath"/>
        </javac>
    </target>

    <target name="generate" depends="init" description="Generates the lexer and parser from the .g grammar file.">
        <echo>Generating the lexer and parser...</echo>
        <java classname="org.antlr.Tool" fork="true" failonerror="true">
            <arg value="-fo"/>
            <arg value="${main.src.dir}/${parser.package}"/>
            <arg value="${grammar.file}"/>
            <classpath refid="classpath"/>
        </java>

        <!-- 
            compile the generated parser and lexer source file to see
            if there's no illegal code inside these source files
        -->
        <antcall target="compile"/>
    </target>

    <!-- other targets -->

</project>

Upvotes: 3

Related Questions