Total Pageviews

Disclaimer

This is a personal web page. The views expressed on this blog are mine and do not necessarily reflect the views of my current employer.

I am currently employed by Morgan Stanley.

July 08, 2012

How to Use ANTLR on .NET, Part III

Part III: C# Project Settings for ANTLR 3

Now let’s see how to use ANTLR 3 in a C# project by analyzing SharpSnmpLib.AST.csproj in #SNMP,

https://github.com/lextm/sharpsnmplib/blob/de58f7c050a0976654e7ee4b90a9217e99af6ae3/SharpSnmpLib/SharpSnmpLib.AST.csproj

The above lines (75-79) use ANTLR MSBuild tasks. The PropertyGroup tag sets two paths: the first is the folder that contains AntlrBuildTask.dll; the second is the path for Antlr3.exe. The Import tag tells MSBuild engine that this extra .targets file needs to be loaded.

This C# project contains two ANTLR grammar files,

The Antlr3 tag means Smi.g is the grammar file that needs to be converted to C# code by Antlr3.exe. Under DEBUG mode, the converted C# files will locate in obj folder.

The None tag means Smi_no_action.g is not in use. About why it is not in use, I will describe later.

You might notice that this project does add Antlr3 C# runtime as a reference,

In this project I use the ANTLR 3 NuGet package. If you don’t want to use NuGet, you can simply add lib\ANTLR\Antlr3.Runtime.dll instead.

OK. After analyzing this project I think you know how to add a grammar file to your C# project, and then configure related project level settings properly. In next post I am going to document what you need to pay attention to at code level. #SNMP will still be our example.

Stay tuned.