Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Johannes Luber (Maintainer)
jaluber AT gmx.de

Kunle Odutola
kunle UNDERSCORE odutola AT hotmail.com

Micheal Jordan

Contents

Architecture

...

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.

...

In general, development progress on the C# target proceeds sporadically.

...

  With versions of ANTLR prior to 3.

...

1.x

...

As of September 2007, , the C# code generator and runtime are NOT cannot be guaranteed to be in sync with the latest release and development versions of the ANTLR tool tools and Java language target. The latest release of the v3.0.x C# code generator and .NET/CLR runtime was developed for the ANTLR v3.0 release from July 2007. Nevertheless, no major problems have been reported by those using the C# codegen and .NET/CLR runtime with ANTLR v3.0.1 since that version was released in August 2007target.  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:

...

To specify that the ANTLR tool should generate C# code (rather than the default of generating Java code) for a grammar, set the grammar-level option named language to the value CSharp2 as shown below:

Code Block

grammar MyGrammar;

options
{
    language=CSharp2;
}

// rest of grammar follows
....

...

For an example grammar named MyGrammar, the following table list the files that would be generated by ANTLR 3.1+ using the CSharp2 (and CSharp) target.

Panel
bgColor#FFFFFF
borderStylenone
titleMyGrammar.gborderStylenone
Code Block

grammar MyGrammar;

options
{
    language=CSharp2;
}
// rest of grammar follows
....
Panel
bgColor#FFFFFF
borderStylenone

MyGrammarLexer.cs
MyGrammarParser.cs

 

Panel
bgColor#FFFFFF
borderStylenone
titleMyGrammar.gborderStylenone
Code Block

lexer grammar MyGrammar;

options
{
    language=CSharp2;
}
// rest of grammar follows
....
Panel
bgColor#FFFFFF
borderStylenone

MyGrammar.cs

Under ANLTR 3.0.x:
MyGrammarLexer.cs

Panel
bgColor#FFFFFF
borderStylenone
titleMyGrammar.gborderStylenone
Code Block

parser grammar MyGrammar;

options
{
    language=CSharp2;
}
// rest of grammar follows
....
Panel
bgColor#FFFFFF
borderStylenone

MyGrammar.cs

Under ANLTR 3.0.x:
MyGrammarParser.cs

Panel
bgColor#FFFFFF
borderStylenone
titleMyGrammar.gborderStylenone
Code Block

tree grammar MyGrammar;

options
{
    language=CSharp2;
}
// rest of grammar follows
....
Panel
bgColor#FFFFFF
borderStylenone

MyGrammar.cs

 

...

You can specify that your generated recognizer should be declared within a specific namespace as shown below. By default all recognizers are generated as top-level types with no enclosing namespace.

Code Block

grammar MyGrammar;

options
{
    language=CSharp2;
}

@parser::namespace { My.Custom.NameSpace.For.Parser.In.Combined.Grammar } // Or just @namespace { ... }

@lexer::namespace { My.Custom.NameSpace.For.Lexer.In.Combined.Grammar }

// rest of grammar follows
....
Code Block

lexer grammar MyGrammar;

options
{
    language=CSharp2;
}

@namespace { My.Custom.NameSpace.For.Lexer }

// rest of grammar follows
....
Code Block

parser grammar MyGrammar;

options
{
    language=CSharp2;
}

@namespace { My.Custom.NameSpace.For.Parser }

// rest of grammar follows
....
Code Block

tree grammar MyGrammar;

options
{
    language=CSharp2;
}

@namespace { My.Custom.NameSpace.For.TreeParser }

// rest of grammar follows
....

...

When generating grammars in debug mode the following preprocessor symbol is defined:

Code Block

#define ANTLR_DEBUG

It can be use like any other preprocessor symbol.

...

A problem, you may encounter while using the CSharp target, is that value types are initialized with null in the generated code (happens e.g. while using labels). The cause lies in the following definition of CSharp.stg:

Code Block

csharpTypeInitMap ::= [
    "int":"0",
    "uint":"0",
    "long":"0",
    "ulong":"0",
    "float":"0.0",
    "double":"0.0",
    "bool":"false",
    "byte":"0",
    "sbyte":"0",
    "short":"0",
    "ushort":"0",
    "char":"char.MinValue",
    default:"null" // anything other than an atomic type
]

...