Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create Domain-Specific Languages with ANTLR : Page 2

The latest version of ANTLR provides the tools you need to build a parser for special-purpose languages.


advertisement
Getting Started
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:

  1. 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.
  2. Execute ANTLR to generate source code for parsing the specified grammar.
  3. Compile the ANTLR-generated source together with the application source.
  4. 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:

/SPARQL-validator /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:

SPARQL-validator/pom.xml <project> ... <dependencies> ... <dependency> <groupId>org.antlr</groupId> <artifactId>antlr</artifactId> <version>3.0</version> </dependency> </dependencies> <build> <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>antlr3-maven-plugin</artifactId> <version>1.0-20071102.021231-1</version> <configuration> <grammars>Sparql.g</grammars> <outputDirectory> target/generated-sources/antlr </outputDirectory> </configuration> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>antlr</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ... </project>

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap