Antlr 3 CSharp Target
ANTLR v3 C# Code generator and Runtime library
Johannes Luber (Maintainer)
jaluber AT gmx.de
Kunle Odutola
kunle UNDERSCORE odutola AT hotmail.com
Micheal Jordan
Contents
Getting Started
If you want to setup and start using ANTLR with C# as quickly as possible please see the FAQ page on using ANTLR, ANTLRWorks, and C#.
However this page (not the FAQ page) provides architectural and background information useful to understanding the workings and usage of ANTLR and C#.
Architecture
The C# target consists of a set of code generation templates and a runtime library (written in C#) for the .NET/CLR platform. The C# code generation templates and the .NET/CLR runtime library are modeled on the Java version. As a consequence, the C# target supports features such as grammar development/prototyping and remote debugging with the superb ANTLRWorks integrated grammar development editor. Given ANTLRWorks popularity, this is very a important feature for ANTLR users.
The .NET/CLR runtime library currently consists of two assemblies named Antlr3.Runtime.dll and Antlr3.Utility.dll. All projects that include an ANTLR v3.x Lexer, Parser or TreeParser must include a reference to:
Antlr3.Runtime.dll- the ANTLR v3.x .NET/CLR runtime libraryAntlr3.Utility.dll- OPTIONAL - only required if you use theDOTTreeGeneratorclass (correct as of May 2008)
No other references are required (except to the manadatory System assembly).
For projects that use the in-built integration with StringTemplate, the following assemblies must also be referenced:
StringTemplate.dll- the C# StringTemplate v3.x library (a.k.a ST# v3.x)antlr.runtime.dll- the ANTLR v2.7.x .NET/CLR runtime library (ST# v3.x was developed with ANTLR v2.7.x)
Status
In general, development progress on the C# target proceeds sporadically. With versions of ANTLR prior to 3.1.x, the C# code generator cannot be guaranteed to be in sync with the latest versions of the ANTLR tools and Java target. When practical to do so, the latest 3.1.x released version is recommended for the C# target.
Version 3.1.x
As of August 2008, the C# target is in sync with ANTLR 3.1. This new version of the C# target breaks source compatibility with previous versions (including previous beta-v3.1 builds). To a certain extent, regeneration of grammars does help, but certain fields have been renamed to follow .NET conventions, which means PascalCase (e.g. .tree is now .Tree). The exception is .st, which is now .ST. Additionally, a new target named CSharp2 has been introduced in addition to the existing CSharp target. The reason for this is three-fold:
Firstly,
CSharpcan retain it's compatibility with C# v1 and the .NET/CLR v1.1 platform.CSharpis restricted to C# v1 features and doesn't take advantage of any C# v2+ features in the code it generates.Secondly, a certain bug fix requires a C# v2 feature or, a change to the templates for each occurrence of the bug (and this has to be done by the user). Further details of this issue can be found in the #Known Issues section below.
Thirdly, because maintaining the backwards compatibility sucks majorly, creating the new
CSharp2target allows the existingCSharptarget to be deprecated without forcing the issue by simply abandoning C# v1 .NET/CLR v1.1 compatibility immediately.
Introducing the new target allowed changes to be made to its distinct code generation templates without fear of breaking anything else. Furthermore, as working on further enhancements to CSharp2 will at least break binary compatibility in the .NET/CLR runtime which is currently shared with CSharp, most of the changes will be done for a future ANTLR v3.2 release. During the life-time of ANTLR v3.1, the public API of the C# target(s) will be frozen (only necessary bugfixes may break this). If you wish to future-proof your grammar, you should change them to use the new CSharp2 target. The original CSharp target that uses only C# v1 and .NET/CLR v1.1 features is deprecated and the current plan is to remove it for the ANTLR v3.2 release.
The C# code generation templates and the .NET/CLR runtime library are feature complete for the CSharp and CSharp2 targets. Both C# targets leverage the existing C# StringTemplate implementations to support the broadest range of the features that ANTLR provides. The long open issue of unit tests has finally been tackled with the adoption of MbUnit and the inclusion (in the v3.1 version) of a wide range of tests for the runtime library. As before, basic sanity checks will done by ensuring that the sample grammars in the examples-v3 archive function correctly. This is currently a work-in-progess for the v3.1 release.
Target Platforms
CSharp target (versions 3.0.x and 3.1.x)
Microsoft .NET v1.1 and later
Mono v1.0 and later
CSharp2 target (version 3.1.x and later)
Microsoft .NET v2.0 and later
Mono v1.2 and later
Runtime Location
The compiled libraries are found in the distribution under the directory "runtime/csharp" (or "runtime/csharp/bin" in source distributions). Both targets use the same .NET/CLR runtime. Intermediary builds may not have the current version and can be compiled by using the build tools.
Supported build tools
Microsoft Visual Studio 2003, 2005 and 2008
Nant v0.85
Performance
The V3 target generates code that is easily faster than that generated by the V2 target (especially the lexers). We probably won't be able to match the bare-metal performance of the code generated by Jim Idle's C target or Ric Klaren's C++ target, but we expect to be very competitive with the other targets.
Source Code and Binaries
For ANTLR 3.0.x there is no C# target source code available. ANTLR 3.1.x has the files under the runtime/CSharp directory. Binaries are currently included in the distro subdirectory, but will be later available on the ANTLR download page as well. Available are the files in the official releases, in daily builds and for the head of the repo on the FishEye site (not reliable) or ask Terence Parr for a Perforce account.