Here is a simple tutorial in which there the instructions you need in order to begin working with AntLR and make it work with your Visual Studio Project. This article was written a looong time ago and since then I was not further involved with antlr. The antlr version I used is 2.9.x.
Requirements
Since we talk about Visual studio, we are in Windows. You have two ways to use AntLR. One is to download the antlr-x.y.z.zip and unzip it (you will need the extracted files more than once, so keep them) and with Java installed you can run AntLR with the following instructions
$ java antlr.Tool mygrammar.g $ javac *.java $ java Main < input
Keep in mind that:
- The file ‘mygrammar.g‘ is a file with the antlr declaration of your grammar
(remove from options the commandlanguage = "CSharp";
in order for these instructions to work). - The file ‘input‘ is a text file that contains an example of your grammar.
- You will also need a ‘Main.java‘ file that you will use it to test the grammar.
( The Main.java file is below. Note that ‘L’ and ‘P’ keywords are the names of the lexer and parser classes in the .g file) - The ‘.’ path, and the path of the .jar file inside the .zip should be added as classpaths.
import java.io.*; class Main { public static void main(String[] args) { try { L lexer = new L(new DataInputStream(System.in)); P parser = new P(lexer); parser.startRule(); } catch(Exception e) { System.err.println("exception: "+e); } } }
The other way (the easy way) is to use the executable antlr.exe that can be downloaded from the antlr web page.
Writing your grammar with AntLR
From the web page of antlr you can learn how to write grammars and use the utility to generate code. The only thing you need to do is to declare that you want the output language to be C#. Below is a sample with the basic code structure that can generate C# code.
header { using System.Collections; using MyLibrary; //A project in your solution } options { language = "CSharp"; } class P extends Parser; options { exportVocab=SADL; k=1; } { int a; char b; ... /* Here declare the properties that your parser wants to have */ } myprogram : (myblock01 | myblock02) EOF ; myblock01 . . ; myblock02 . . ; . . . class L extends Lexer; options { exportVocab=SADL; k=1; } /* No properties for the lexer, you will be using only the parser */ mytokens { T_WHILE = "while"; T_for = "for"; . . } . . .
Creating your .dll from the C# generated code
After you generate your C# code (3 .cs files and 1 .txt file) you need to put this code into your solution. Select to Add a new project into your solution and select ‘C# projects’ and ‘Class Library’.
Name the project the way you want, and include the 4 files into the folder of this project. You can do that by using the ‘Add existing Item’ feature.
In order to build this project you have to add a reference to it. The reference you need you can create it by building the ‘JavaParse’ project that exists in the ‘examples\csharp\java’ folder of the antlr-x.y.z.zip. Before you build it you must create a few files by using AntLR with the files java.g and java.tree.g. You can build the ‘JavaParse’ project and the file ‘antlr.runtime.dll’ will be created.
You have to add this file as a reference into your C# class library. When you do that you can build your C# library. In order to use this library you must add this ‘myantlrlib.dll’ and the ‘antlr.runtime.dll’ as references to the project you want to use the parser.
Thanks for the post,
It really helped me with some legacy code I had to work!