Antlr C# Code Generation
using
Visual Studio.NET
A Step by Step Guide
By Adnan Masood
These instructions are an extension to Notes for using the ANTLR C# Code Generator
explaining the use of Visual Studio.NET to generate C# code from Antlr grammar files.
Step 1
Download and install Antlr from here
Step 2
In 2.7.5 release, the ANTLR C# runtime assemblies are
located in lib folder.
antlr.astframe.dll
antlr.runtime.dll
In previous versions, after 2.7.3, ANTLR C# runtime source
and build files located in the lib/ csharp.
If you are using the earlier version, please open the referenced
solution antlr.net-runtime.sln otherwise, create a new C#
console application project and add reference to the above
mentioned DLLs.
Ive named the project CalcSample for this example.
Step 3.
Copy the Antlr.jar in your java runtime extension folder. For instance, a default JRE
folder will be as follows.
C:\Program Files\Java\jre1.5.0_06\lib\ext
Antlr C# Code Generation using Visual Studio.NET
Step 4.
Rename your class1.cs to to Calc.cs, make CalcSample startup project and copy the
following code in Calc.cs. Overwrite all the existing code generated in Calc.cs.
using System;
using
using
using
using
using
CommonAST
= antlr.CommonAST;
AST
= antlr.collections.AST;
CharBuffer
= antlr.CharBuffer;
RecognitionException
= antlr.RecognitionException;
TokenStreamException
= antlr.TokenStreamException;
// wh: bug(?) in DotGNU 0.6 - "using antlr" will workaround the
problem.
#if __CSCC__
using antlr;
#endif
class Calc
{
public static void Main(string[] args)
{
try
{
CalcLexer lexer = new CalcLexer(new
CharBuffer(Console.In));
lexer.setFilename("<stdin>");
CalcParser parser = new CalcParser(lexer);
parser.setFilename("<stdin>");
// Parse the input expression
parser.expr();
CommonAST t = (CommonAST)parser.getAST();
// Print the resulting tree out in LISP notation
Console.Out.WriteLine(t.ToStringTree());
CalcTreeWalker walker = new CalcTreeWalker();
CalcParser.initializeASTFactory(walker.getASTFactory());
// Traverse the tree created by the parser
float r = walker.expr(t);
Console.Out.WriteLine("value is "+r);
}
catch(TokenStreamException e)
{
Console.Error.WriteLine("exception: "+e);
}
catch(RecognitionException e)
{
Console.Error.WriteLine("exception: "+e);
}
}
}
Antlr C# Code Generation using Visual Studio.NET
Step 5:
Use the calc.g, the example grammar file as your sample file for the project. This file can
be found at the following location of your installation
<RelativeFolderr>\Antlr\antlr-2.7.6\examples\csharp\calc\calc.g
Copy this file in your project folder (calcsample in this case).
Step 6:
Install BuildRules.exe from Visual Studio .NET 2003 Automation Samples to allow
build rules definition in the Pre-Build Event Command Line (Project->Properties).
Use the following code to add the Pre-Build Event Command Line
java
antlr.Tool -o ..\..\ ..\..\calc.g
This will allow generating the corresponding ANTLR files required for the solution
building.
Antlr C# Code Generation using Visual Studio.NET
Step 7:
Add the Project references to the CalcSample Project using Add Reference.
Your project will look like follows.
Antlr C# Code Generation using Visual Studio.NET
Step 8
Right click and build the CalcSample Project. This will run the pre-build command
java
antlr.Tool -o ..\..\ ..\..\calc.g
to generate the following files containing lexer and parsers in C# corresponding to the
given grammar.
CalcLexer.cs
CalcParser.cs
CalcParserTokenTypes.cs
CalcParserTokenTypes.txt
CalcTreeWalker.cs
AssemblyInfo.cs
The build process output will look like as follows.
------ Build started: Project: CalcSample, Configuration: Debug .NET ----Performing Pre-Build Event...
ANTLR Parser Generator
Version 2.7.6 (2005-12-22)
Preparing resources...
Updating references...
1989-2005
Step 9:
Run the program from Console.
05/25/2006
05/25/2006
05/25/2006
05/25/2006
05/25/2006
05/23/2006
05/23/2006
05/23/2006
05/23/2006
05/25/2006
05/25/2006
05/23/2006
12/07/2005
01:25
01:21
01:21
12:14
01:21
10:23
10:23
10:23
10:23
01:25
01:25
10:23
11:00
AM
AM
AM
AM
AM
PM
PM
PM
PM
AM
AM
PM
AM
<DIR>
20,480
24,064
118,784
667,136
8,845
5,395
427
177
24,576
34,304
2,824
8
..
antlr.astframe.dll
antlr.astframe.pdb
antlr.runtime.dll
antlr.runtime.pdb
CalcLexer.cs
CalcParser.cs
CalcParserTokenTypes.cs
CalcParserTokenTypes.txt
CalcSample.exe
CalcSample.pdb
CalcTreeWalker.cs
test.in
C:\adnano\edu\NSU\Third Semester\CISC 0630 Compiler\Antlr\antlr2.7.6\lib\csharp\CalcSample\bin\Debug>CalcSample
Antlr C# Code Generation using Visual Studio.NET
3+4*5;
( + 3 ( * 4 5 ) )
value is 23
And voila! Here is your C# code up and running, evaluating the expression as well as
displaying the AST. Happy Antlring.
Antlr C# Code Generation using Visual Studio.NET
Appendix
Antlr Sample File - Calc.g
options {
language = "CSharp";
}
class CalcParser extends Parser;
options {
buildAST = true; // uses CommonAST by default
}
expr
:
;
mexpr (PLUS^ mexpr)* SEMI!
:
;
atom (STAR^ atom)*
mexpr
atom: INT
;
class CalcLexer extends Lexer;
WS
:
|
|
|
(' '
'\t'
'\n'
'\r')
{ _ttype = Token.SKIP; }
;
LPAREN:
;
'('
RPAREN:
;
')'
STAR: '*'
;
PLUS: '+'
;
SEMI: ';'
;
protected
DIGIT
:
;
'0'..'9'
Antlr C# Code Generation using Visual Studio.NET
INT
:
;
(DIGIT)+
class CalcTreeWalker extends TreeParser;
expr returns [float r]
{
float a,b;
r=0;
}
:
#(PLUS a=expr b=expr)
{r = a+b;}
|
#(STAR a=expr b=expr)
{r = a*b;}
|
i:INT
{r = Convert.ToSingle(i.getText());}
;
References
Antlr
www.antlr.org
Antlr Grammars
http://www.antlr.org/grammar/
ANTLR Reference Manual
An Introduction To ANTLR
Antlr C# Code Generation using Visual Studio.NET