0% found this document useful (0 votes)
471 views8 pages

Antlr C Sharp Code Generation Using Visual

This document provides a step-by-step guide for using ANTLR to generate C# code from grammar files in Visual Studio.NET. It explains how to set up a sample project that parses a calculator grammar and evaluates expressions. Key steps include adding ANTLR runtime references, configuring a pre-build event to run ANTLR, and using the generated lexer and parser classes to build an AST and evaluate input.

Uploaded by

yailsk8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
471 views8 pages

Antlr C Sharp Code Generation Using Visual

This document provides a step-by-step guide for using ANTLR to generate C# code from grammar files in Visual Studio.NET. It explains how to set up a sample project that parses a calculator grammar and evaluates expressions. Key steps include adding ANTLR runtime references, configuring a pre-build event to run ANTLR, and using the generated lexer and parser classes to build an AST and evaluate input.

Uploaded by

yailsk8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Antlr C# Code Generation

using
Visual [Link]
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 [Link] 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.

[Link]
[Link]

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 [Link] 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 [Link] 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 [Link]

Step 4.
Rename your [Link] to to [Link], make CalcSample startup project and copy the
following code in [Link]. Overwrite all the existing code generated in [Link].
using System;
using
using
using
using
using

CommonAST
= [Link];
AST
= [Link];
CharBuffer
= [Link];
RecognitionException
= [Link];
TokenStreamException
= [Link];

// 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([Link]));
[Link]("<stdin>");
CalcParser parser = new CalcParser(lexer);
[Link]("<stdin>");
// Parse the input expression
[Link]();
CommonAST t = (CommonAST)[Link]();
// Print the resulting tree out in LISP notation
[Link]([Link]());
CalcTreeWalker walker = new CalcTreeWalker();
[Link]([Link]());
// Traverse the tree created by the parser
float r = [Link](t);
[Link]("value is "+r);
}
catch(TokenStreamException e)
{
[Link]("exception: "+e);
}
catch(RecognitionException e)
{
[Link]("exception: "+e);
}
}
}

Antlr C# Code Generation using Visual [Link]

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 [Link] 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

[Link] -o ..\..\ ..\..\calc.g

This will allow generating the corresponding ANTLR files required for the solution
building.

Antlr C# Code Generation using Visual [Link]

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 [Link]

Step 8
Right click and build the CalcSample Project. This will run the pre-build command
java

[Link] -o ..\..\ ..\..\calc.g

to generate the following files containing lexer and parsers in C# corresponding to the
given grammar.

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

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

..
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

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 [Link]

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 [Link]

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 = [Link]; }

;
LPAREN:
;

'('

RPAREN:
;

')'

STAR: '*'
;
PLUS: '+'
;
SEMI: ';'
;
protected
DIGIT
:
;

'0'..'9'

Antlr C# Code Generation using Visual [Link]

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 = [Link]([Link]());}
;

References

Antlr
[Link]

Antlr Grammars
[Link]

ANTLR Reference Manual

An Introduction To ANTLR

Antlr C# Code Generation using Visual [Link]

You might also like