Integrating ANTLR Code Generation with Visual Studio 2008

January 2, 2008

ANTLR is a DSL tool that can generate a language parser based on a grammar (*.g) file. From there, you hook into the parser so that your code can interpret the language into constructs your application can understand. But, in order to get your grammar file correct, you make end up using a variety of development methods such as TDD, and all of them will have the same step: run the ANTLR tool against your grammar file to generate your parser code.The ANTLR web site has an article on its wiki that will let you integrate the ANTLR tool into your Visual Studio Build process. Follow the steps outlined in that article first.  Since VS 2008 project files are basically MS Build files, the steps work almost verbatim. However, here are a few caveats I encountered:The Exec command in the GenerateAntlrCode target needed some classpath modifications. Basically, I needed to tell java where the Antlr jar file was located. Here is what I ended up with:

1:  <Target Name="GenerateAntlrCode" Inputs="@(Antlr3)" Outputs="%(Antlr3.OutputFiles)">     2:      <Exec Command="java -cp %22$(Antlr3ToolPath)\antlr.jar;$(Antlr3ToolPath)\antlr-3.0.1.jar;$(Antlr3ToolPath)\stringtemplate-3.1b1.jar%22 org.antlr.Tool -message-format vs2005 -lib $(AntlrGrammarPath)  @(Antlr3)" Outputs="%(Antlr3.OutputFiles)" />     3:    </Target>

A couple of notes regarding this target:

  • %22 is the escaped form of double-quotes (“)
  • I decided to create a build variable so that any changes to a path was updated everywhere (DRY principle).
  • You need to reference several jars:
    • antlr.jar -> The Antlr 2.7.7 tool
    • antlr-3.0.1.jar -> The Antlr 3.0.1 tool
    • stringtemplate-3.1b1.jar -> The String Template library

The antlr 2.7.7 jar is needed for the StreamToken class that seems to be in a different namespace than the 3.0.1 jar.The properties called Antlr3ToolPath and Antlr3GrammarPath are defined in a property group like so:

1:    <PropertyGroup>     2:      <Antlr3ToolPath>$(MSBuildProjectDirectory)\..\..\Tools\ANTLR</Antlr3ToolPath>    3:      <AntlrGrammarPath>$(MSBuildProjectDirectory)\Model\Parsers</AntlrGrammarPath>     4:      <BuildDependsOn>GenerateAntlrCode;$(BuildDependsOn)</BuildDependsOn>     5:    </PropertyGroup>

The reason the GrammarPath is basically pointing to the output folder of the parser. This is needed because I am using the ANTLR output option of AST, which generates an Abstract Syntax Tree that much be consumed by another ANTLR grammar. For more details on why you need two grammar files for this process, I recommend the book The Definitive ANTLR Reference, by Terrence Parr.After that, I was able to use Visual Studio to generate the code on every build of the code. When using TDD tools like resharper or, this is an added bonus in that any changes made to the grammar are incorporated in my unit tests. Bonus number 2: since the changes are within the project file itself, and since my continuous integration server is based on my Visual Studio Solution file, the grammar files are also included on my build server and will cause the build to fail if the grammar fails to compile.I hope your build goes just as nicely.


4 Responses to “Integrating ANTLR Code Generation with Visual Studio 2008”

  1. ActiveEngine Sensei Says:

    It looks as though you have mastered what you need to know for ANTLR. I wanted to point you to this great article and sample on CodeProject regarding ANTLR:

    This article is what got me hooked. Enjoy.

  2. Gergely Mezei Says:

    The article is fine, but ANTLR does not really work with VS2008. More properly, ANTLR can produce source code from the .g files, but if this source file is a C++ library, then the library cannot be run in Vs2008 environment (it causes a null pointer exception in antlr.lib).

  3. mark abraham Says:

    Thank you for the insight as to the VS 2008 integration. I got roped-in with the SOTA Expression Eval article, also.

    I’m wondering if you know about the C# update for the latest ANTLR version, 3.1?

    …running into compiler issues because the C# library apparently hasn’t been updated. Here’s the error I get:

    “The type or namespace name ‘RecognizerSharedState’ could not be found (are you missing a using directive or an assembly reference?)”

    Best regards!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: