This is a (revised) attempt at a full summary of the steps needed to get ANTLR4 to generate a C# lexer, parser, and listener for a very simple grammar. The C# target is designed to work as a Visual Studio (VS) add-in only and cannot be used from the command line.
Terence Parr is the author of ANTLR and Sam Harwell is the author of the C# language target for ANTLR. Any errors in this document are mine alone.
STEP 1
Goal: A working Java RE installation and creation of the registry entries the ANTLR4 C# target uses to locate the java.exe executable.
* Run regedit and examine the HKEY_LOCAL_MACHINE\SOFTWARE key. It should contain a “JavaSoft” subkey:
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft.
I run Windows 7 64-bit. I found the above registry key was only created when I manually installed the 64-bit version of the JRE. It was no created by the 32-bit installer or the installer that runs when you click on the “Free Java Download” button on the java.com page.
The ANTLR4 C# target, needs the java runtime, and will not run without this registry key, so keep installing versions of java till you get one.
Note: Sam says that you should be able to install the 32-bit JDK on a 64-bit system and, so long as you tell the installer to install the JRE as well, the ANTLR4 C# Target will find the java runtime.
STEP 2
Goal: ANTLR Language Support extension installed in VS.
* Install ANTLR Language Support extension (a vsix file):
Goal: NuGet package manager at version 2.5 or greater.
* Run Visual Studio 2012.
STEP 4
Goal: A VS solution and project.
* Run Visual Studio 20120
STEP 5
Goal: ANTLR4 NuGet package installed in Hello project.
* In Solution Explorer, right click the Hello project folder and select “Manage NuGet Packages” in the context menu. A dialog pops up.
This package must be installed in each project that uses the ANTLR C# target.
STEP 6
Goal: A simple ANTLR Grammar.
* In Solution Explorer, right click the Hello project folder and select “Add -> New Item” in the context menu. A dialog pops up.
grammar Hello;
HELLOWORD : 'hello' ;
r : HELLOWORD ID ; // match keyword hello followed by an identifierID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ;
STEP 7
Goal: A program to parse the Hello.g4 grammar, parse a text string, and traverse the parsed tokens.
* Create a new class called MyVisitor as follows:
public class MyVisitor : HelloBaseVisitor<object> {
* Open the Program.cs file.
using Antlr4.Runtime;
* Replace all the class text with the following:
class Program {
public void Run() {
private void RunParser() {
If you have ReSharper installed you will probably see syntax errors reported by intellisense in AddParseListener and RContext. These are, according to Sam, due to a bug in ReSharper. Ignore the “errors” for now or disable ReSharper via the Tools -> Options -> ReSharper -> General tab.
* Build the program. It should build successfully.
STEP 8
Goal: Run the program with the expected result.
* Run the Hello program.
START
HelloVisitor VisitRVisit Symbol=helloVisit Symbol=worldDONE. Hit RETURN to exit:
The program parsed the text “hello world” and chopped it up into 2 terminal tokens, as you can see above.
STEP 9
Goal: Understand how it works (only a bit though).
* Look in the ANTLR4\Hello\obj\Debug directory and observe the following files:
HelloBaseListener.cs
HelloBaseVisitor.csHelloLexer.csHelloLexer.tokensHelloListener.csHelloParser.csHelloVisitor.cs
USEFUL LINKS
hello phurst!