• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

AntLR And Visual Studio

July 1, 2010 By _tasos 1 Comment

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 command language = "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.

Filed Under: .NET Development Tagged With: ANTLR

Reader Interactions

Comments

  1. James F. says

    March 6, 2012 at 16:19

    Thanks for the post,

    It really helped me with some legacy code I had to work!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • Capitalism for people. Socialism for companies? https://t.co/zgUoPcqnix January 5, 2020 07:46
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in