Creating an ANTLR-based application involves code generation, and thus requires more steps than developing a typical application. The general steps necessary to build an ANTLR-based application are:
- Develop a language description (grammar file) for the language to parse, which this article will explore later using a Test-Driven Development (TDD) approach later in this article.
- Execute ANTLR to generate source code for parsing the specified grammar.
- Compile the ANTLR-generated source together with the application source.
- Package the compiled source, including both application and ANTLR dependencies.
|Authors' Note: As proponents of repeatable, reliable automated builds, we used a Maven plug-in to accomplish steps 2-4 in the preceding list. Other forums have documented how to perform such steps using Ant, so we'd like to point out how you can use Maven and the ANTLR 3 Maven plug-in to build an ANTLR 3-based application. Maven's declarative nature and strong encapsulation of commonly repeated tasks make it well suited for this type of task. Also, it's convenient for developing (for example) minimal source bundles such as the downloadable code that accompanies this article.
Maven is heavily convention based. One of these conventions outlines a standard project directory structure. The sample application follows the conventions of Maven and the ANTLR 3 Maven plug-in. Its directory structure looks like:
/src/main/java -> Java source files
/src/main/antlr -> ANTLR grammar files
/src/test/java -> Java test source files
/target/generated-sources/antlr -> generated parser files
pom.xml -> Maven Project Object Model descriptor
By following this directory layout, all you need to leverage ANTLR in a Maven build is to bind the Maven ANTLR 3 plug-in to the generate-sources life-cycle phase of a Maven build, and to declare a run-time dependency on the ANTLR 3 library:
If you download the accompanying source
, you should be able to build the application by running "mvn clean install"
from the project's top-level directory.