Table of Contents
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. Adjust the Included and OutputFiles to fit your project.
To finish off the integration, look higher in the same file and find the XML block that contains AssemblyInfo.cs:
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.
Finally, do not forget to add InitialTargets attribute into Project node.
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:
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.
These .rules files only work for the C target.
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.
ANTLR IDE. An eclipse plugin for ANTLRv3 grammars.
- 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/
In version 7:
Navigate to File->Settings->IDE Settings->Plugins and install the "ANTLRWorks" plugin.
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.
- NetBeans (this solution was tested on version 6.5)
- Anltr v3 Ant Task
- Ant-contrib Ant Tasks (tested on versions 0.6 and 1.0b3, available from the project's download page)
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
<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).
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
.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:
_Test_.java(created from an ANTLRWorks debug session)
_Test_input.txt(created from an ANTLRWorks debug session)
classesdirectory found below the directory that
grammarFilename.gis 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).