Antlr C Sharp Code Generation Using Visual
Antlr C Sharp Code Generation Using Visual
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
antlr.astframe.dll
antlr.runtime.dll
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
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;
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
This will allow generating the corresponding ANTLR files required for the solution
building.
Step 7:
Add the Project references to the CalcSample Project using Add Reference.
Step 8
Right click and build the CalcSample Project. This will run the pre-build command
java
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
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
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.
Appendix
Antlr Sample File - Calc.g
options {
language = "CSharp";
}
class CalcParser extends Parser;
options {
buildAST = true; // uses CommonAST by default
}
expr
:
;
:
;
mexpr
atom: INT
;
class CalcLexer extends Lexer;
WS
:
|
|
|
(' '
'\t'
'\n'
'\r')
{ _ttype = Token.SKIP; }
;
LPAREN:
;
'('
RPAREN:
;
')'
STAR: '*'
;
PLUS: '+'
;
SEMI: ';'
;
protected
DIGIT
:
;
'0'..'9'
INT
:
;
(DIGIT)+
References
Antlr
www.antlr.org
Antlr Grammars
http://www.antlr.org/grammar/
An Introduction To ANTLR