Integration with Development Environments

Table of Contents 


VisualStudio

C# Projects

For C# projects, you can integrate ANTLR with Visual Studio 2005/2008 by pasting the following snippet of XML near the end of your .csproj file. The <Target>-Tag must appear before <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/> for the Build to run successfully. (warning) Adjust the Included and OutputFiles to fit your project.

<ItemGroup>
    <Antlr3 Include="SimpleCalc.g">
      <OutputFiles>SimpleCalcLexer.cs;SimpleCalcParser.cs</OutputFiles>
    </Antlr3>
    <Antlr3 Include="BigCalc.g">
      <OutputFiles>BigCalcLexer.cs;BigCalcParser.cs</OutputFiles>
    </Antlr3>
  </ItemGroup>
  <Target Name="GenerateAntlrCode" Inputs="@(Antlr3)" Outputs="%(Antlr3.OutputFiles)">
    <Exec Command="java org.antlr.Tool -message-format vs2005 @(Antlr3)" Outputs="%(Antlr3.OutputFiles)"/>
  </Target>
  <PropertyGroup>
    <BuildDependsOn>GenerateAntlrCode;$(BuildDependsOn)</BuildDependsOn>
  </PropertyGroup>

(info) To finish off the integration, look higher in the same file and find the XML block that contains AssemblyInfo.cs:

<ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

  and add the additional Compile options below to have your .cs output files added to the project as dependent on your .g files.  Again, adjust the input and output names to fit your project.

<ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
        <Compile Include="SimpleCalcLexer.cs">
            <AutoGen>True</AutoGen>
            <DesignTime>True</DesignTime>
            <DependentUpon>SimpleCalc.g</DependentUpon>
        </Compile>
        <Compile Include="SimpleCalcParser.cs">
            <AutoGen>True</AutoGen>
            <DesignTime>True</DesignTime>
            <DependentUpon>SimpleCalc.g</DependentUpon>
        </Compile>
        <Compile Include="BigCalcParser.cs">
            <AutoGen>True</AutoGen>
            <DesignTime>True</DesignTime>
            <DependentUpon>BigCalc.g</DependentUpon>
        </Compile>
        <Compile Include="BigCalcLexer.cs">
            <AutoGen>True</AutoGen>
            <DesignTime>True</DesignTime>
            <DependentUpon>BigCalc.g</DependentUpon>
        </Compile>
  </ItemGroup>

Finally, do not forget to add InitialTargets attribute into Project node.

<Project DefaultTargets="Build" InitialTargets="GenerateAntlrCode" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

With ANTLR 3.0, there are some minor integration issues. Sometimes error messages have an empty "location" string, so Visual Studio will not detect the error. Also, it is possible for ANTLR to generate the output file successfully even though your grammar has errors in it. When this happens, the grammar will not be recompiled until it is edited again (since the output is more recent than the grammar).

C/C++ .rules Files for Visual Studio

The C runtime distribution - use the 3.1 distribution, which you may need to get from the latest interim build at the time of writing - comes with a set of .rules files, which you can add in to your Visual Studio configuration. When you add a .g file to your Visual Studio project, it will ask you which rule file you wish to use. Note that you must tell Visual Studio whether this is a lexer only, parser only, parser+lexer, or tree grammar (you can change it later if you get this wrong).

The rulefiles are called:

antlr3lexer.rules
antlr3lexerandparser.rules
antlr3parser.rules
antlr3treeparser.rules

Right clicking on a .g file in solution explorer and selecting properties will allow you to configure any of the antlr command line options to suit your needs. The defaults are usually fine, but you may wish to configure the output and lib directories to conform to the usual layouts of directories that Visual Studio assumes, and -Xconversiontimeout is useful for more complicated grammars. The base directory will be the .vcproj directory and so your output may or may not be in this directory (do you store your .g files in the same directory as the .vcproj file basically).

The examples solution (C.sln), which is part of the downloadable examples tar/zip on the main ANTLR downloads page, uses this technique to build the ANTLR grammars, if you are looking for some examples. Look under the C subdirectory for the C target examples.

N.B.

These .rules files only work for the C target.


Eclipse

 Eclipse 3.3+ for Antlr 3.x

AntlrDT is a standard Eclipse plugin implementing an Antlr 3.1+ specific grammar editor, outline, and builder. Also includes a StringTemplate group file editor and outline view.

 See http://www.certiv.net/projects/plugins/antlrdt.html

ANTLR IDE. An eclipse plugin for ANTLRv3 grammars.

Features

  • Support for ANTLR 3.0.x/3.1.x
  • Integrated ANTLR/Java Launcher and Debugger(beta). Note: ANTLR breakpoints not supported yet
  • ANTLR Built-in Interpreter, Java runner and debugger
  • Railroad diagrams
  • Custom targets
  • Automatically (Ctrl+S)/Manually (Ctrl+Shift+G) code generator
  • Problem markers for errors and warnings in grammar files
  • Advanced text editor, code selection (F3) and code completion (Ctrl+Space)
  • Simple syntax highlighting for target language (action code)
  • Outline and quick outline (Ctrl + O) views for options, tokens, scopes, actions and rules
  • Search rules references
  • Mark generated resources as derived

More information? Please visit http://antlrv3ide.sourceforge.net/

IntelliJ

In version 7:

Navigate to File->Settings->IDE Settings->Plugins and install the "ANTLRWorks" plugin.

NetBeans

A plugin that provides some support for editing ANTLR v3 grammar files is available from the NetBeans Plugin Portal.  The author notes that the plugin supports the following while editing: Coloring, Code folding, Code completion, Hyperlink, Mark Occurrences, Navigator.

The plugin does not provide direct support for generating Java source files, nor compiling those files, however NetBeans uses an Ant-based build system, therefore support for these operations can be added to individual projects.  This functionality is added by editing the build.xml file for the project (available from the "Files" tab).

Adding Build and Clean Support to an Individual Project

This is one possible method of adding support for ANTLR v3 to an individual project.  Those who are familiar with Ant build scripts and the Netbeans build chain can modify this to suit their own needs.

Prerequisites

Install and Add Support to Project

First, download the ANTLR v3 Ant task and copy the task's antlr3.jar file into the NetBeansInstallDir/java2/ant/lib directory, where NetBeansInstallDir is the directory where NetBeans is installed (e.g. C:\Program Files\NetBeans 6.5 on a Windows system).

Next, download the Ant-contrib tasks and copy the ant-contrib-1.0b3.jar to the same directory as above.

Next, start NetBeans, and open the project that you'd like to add ANTLR support to. Click on the "Files" tab, and find the build.xml file. Double click to open it. Be careful not to change anything at the top of this file, especially the contents of the <project> and <import> tags. Scroll down past the comments, and find the closing </project> tag. Add the following above the closing </project> tag, but below the closing comment tag (-->). In the first three lines, below, replace AntlrInstallDir with the location of your ANTLR installation (e.g., mine is C:\java\antlr-3.1.2).

build.xml fragment for Antlr v3 support
    <property name="antlr.libdir" location="AntlrInstallDir/lib" />
    <property name="antlr.tooldir" location="AntlrInstallDir/lib" />
    <property name="antlr.runtimedir" location="AntlrInstallDir/lib" />


    <patternset id="antlr.libs">
        <include name="stringtemplate-3.1.jar" />
        <include name="antlr277.jar" />
    </patternset>

    <patternset id="antlr.tool">
        <include name="antlr-3.1.2.jar" />
    </patternset>

    <patternset id="antlr.runtime">
        <include name="antlr-runtime-3.1.2.jar" />
    </patternset>

    <path id="antlr.path">
        <fileset dir="${antlr.tooldir}" casesensitive="yes">
            <patternset refid="antlr.tool" />
        </fileset>

        <fileset dir="${antlr.runtimedir}" casesensitive="yes">
            <patternset refid="antlr.runtime" />
        </fileset>

        <fileset dir="${antlr.libdir}" casesensitive="yes">
            <patternset refid="antlr.libs" />
        </fileset>
    </path>


    <target name="-pre-init">
        <taskdef resource="net/sf/antcontrib/antlib.xml"/>
    </target>


    <target name="-post-clean">
        <fileset id="antlr.grammars" dir="${src.dir}" includes="**/*.g"/>

        <pathconvert property="antlr.clean.files" pathsep=',' refid="antlr.grammars">
            <compositemapper>
                <globmapper from="${basedir}${file.separator}${src.dir}${file.separator}*.g"
                    to="*.tokens"/>
                <globmapper from="${basedir}${file.separator}${src.dir}${file.separator}*.g"
                    to="*Parser.java"/>
                <globmapper from="${basedir}${file.separator}${src.dir}${file.separator}*.g"
                    to="*Lexer.java"/>
                <chainedmapper>
                    <globmapper from="${basedir}${file.separator}${src.dir}${file.separator}*.g"
                        to="*.g"/>
                    <compositemapper>
                        <regexpmapper from="(([^/]*/)*).*\.g"
                            to="\1__Test__.java" handledirsep="true"/>
                        <regexpmapper from="(([^/]*/)*).*\.g"
                            to="\1__Test___input.txt" handledirsep="true"/>
                    </compositemapper>
                </chainedmapper>
            </compositemapper>
        </pathconvert>
        <pathconvert property="antlr.clean.dirs" pathsep=',' refid="antlr.grammars">
            <chainedmapper>
                <globmapper from="${basedir}${file.separator}${src.dir}${file.separator}*.g"
                    to="*.g"/>
                <compositemapper>
                    <regexpmapper from="(([^/]*/)*).*\.g"
                        to="\1classes/**/*" handledirsep="true"/>
                    <regexpmapper from="(([^/]*/)*).*\.g"
                        to="\1classes" handledirsep="true"/>
                </compositemapper>
            </chainedmapper>
        </pathconvert>

        <if>
            <not>
                <equals arg1="${antlr.clean.files}" arg2=""/>
            </not>
            <then>
                <echo level="info">Cleaning ANTLR- and ANTLRWorks-generated files (if any exist):${line.separator}</echo>
                <delete quiet="true" verbose="true">
                    <FileSet dir="${src.dir}" includes="${antlr.clean.files}" excludes="${src.dir}"/>
                </delete>
            </then>
        </if>
        <if>
            <not>
                <equals arg1="${antlr.clean.dirs}" arg2=""/>
            </not>
            <then>
                <echo level="info">Cleaning ANTLRWorks-generated directories (if any exist):${line.separator}</echo>
                <delete quiet="true" verbose="true" includeemptydirs="true">
                    <FileSet dir="${src.dir}" includes="${antlr.clean.dirs}" excludes="${src.dir}"/>
                </delete>
            </then>
        </if>
    </target>


    <target name="-pre-compile-single">
        <basename property="javac.includes.base" file="${javac.includes}"/>
        <if>
            <equals arg1="${javac.includes.base}" arg2="*"/>
            <then>
                <for param="antlr.target">
                    <path>
                        <fileset dir="${src.dir}" includes="${javac.includes}.g"/>
                    </path>
                    <sequential>
                        <antlr:antlr3 xmlns:antlr="antlib:org/apache/tools/ant/antlr"
                            target="@{antlr.target}">
                            <classpath>
                                <path refid="antlr.path" />
                            </classpath>
                        </antlr:antlr3>
                    </sequential>
                </for>
            </then>
        </if>
    </target>


    <target name="-pre-compile">
        <for param="antlr.target">
            <path>
                <fileset dir="${src.dir}" includes="**/*.g"/>
            </path>
            <sequential>
                <antlr:antlr3 xmlns:antlr="antlib:org/apache/tools/ant/antlr"
                    target="@{antlr.target}">
                    <classpath>
                        <path refid="antlr.path" />
                    </classpath>
                </antlr:antlr3>
            </sequential>
        </for>
    </target>

Usage

The above integrates with the regular Java build/clean cycle in NetBeans. Execute the IDE's build operation, and the grammar file(s) will be passed through the ANTLR tool to generate the necessary Java code prior to the compile step. You can access the build operation either from the "Run" menu, by pressing <F11>, or by right-clicking on the project in the "Projects" tab, and selecting "Build" from the contect menu. You can also right-click a package and choose "Compile Package" to generate and compile for a single package. Context menu support is not available for the grammar file itself, unfortunately.

The IDE's clean operation will remove all generated .java and .tokens files, and it will also remove files generated from an ANTLRWorks debug session. The latter allows you to use both NetBeans and ANTLRWorks together on a grammar file, and when ready, clean and build from NetBeans. If your grammar file is named grammarFilename.g, then the following files and subdirectories will be deleted (if they exist) from the directory that contains the grammar file, when you invoke the clean operation:

  • grammarFilename.tokens
  • grammarFilenameLexer.java
  • grammarFilenameParser.java
  • _Test_.java (created from an ANTLRWorks debug session)
  • _Test_input.txt (created from an ANTLRWorks debug session)
  • classes directory found below the directory that grammarFilename.g is found in (created from an ANTLRWorks debug session)

The clean operation is accessed by pressing <Shift>-<F11> (for a clean and build in one step), selecting "Clean and Build" from the "Run" menu, or "Clean" (or "Clean and Build") from the project's context menu. You can control what does and does not get deleted by modifying the -post-clean target in the above example (requires knowledge of Ant build script syntax).

Xcode

will follow...