Setting Up ANTLR4 C# Target in Visual Studio 2012 (Revised)

8,648 views
Skip to first unread message

phurst

unread,
Aug 22, 2013, 12:55:22 PM8/22/13
to antlr-di...@googlegroups.com

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.

* Install the Java JRE (http://java.com/en/download/manual.jsp )

* 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):

 
STEP 3

Goal: NuGet package manager at version 2.5 or greater.

* Run Visual Studio 2012.

* Click on Tools -> Extensions and Updates in the menu.
* Select Updates -> Visual Studio Gallery in the left hand panel.
* If the NuGet Paakage Manager appears, click the update button. (In my case it updated from version 2.2.40116.9051 to 2.6.40627.9000). Your goal is to get a NuGet version of 2.5 or more.
* Restart Visual Studio.

STEP 4

Goal: A VS solution and project.

* Run Visual Studio 20120

* Create a new solution called ANTLR4 with a new Console Application project in it called Hello.

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.

* In the left hand panel select “NuGet official package source”.
* At the top select “Include Prerelease” in the left hand combo box.
* In the search panel at top right type “ANTLR4”.
* The “ANTLR 4” package will appear in the central list. Install it.

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.

* Select Visual C# Items -> ANTLR in the left hand panel.
* Select “ANTLR4 Combined Grammar” in the central panel.
* Enter the name “Hello.g4” at the bottom.
* Click the Add Button.
* Open the Hello.g4 file and replace all its text with the following:

 grammar Hello;

 HELLOWORD : 'hello' ;

 r  : HELLOWORD ID ;         // match keyword hello followed by an identifier

 ID : [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> {

 
        public override object VisitR([NotNull] HelloParser.RContext context) {
            Console.WriteLine("HelloVisitor VisitR");
            context
                .children
                .OfType<TerminalNodeImpl>()
                .ToList()
                .ForEach(child => Visit(child));
            return null;
        }
  
        private void Visit(TerminalNodeImpl node) {
            Console.WriteLine(" Visit Symbol={0}", node.Symbol.Text);
        }
    }
 

* Open the Program.cs file.

* Add the following using statement at the top of the file:

 using Antlr4.Runtime;

* Replace all the class text with the following:

    class Program {

        private static void Main(string[] args) {
            (new Program()).Run();
        }

        public void Run() {

            try {
                Console.WriteLine("START");
                RunParser();
                Console.Write("DONE. Hit RETURN to exit: ");
            } catch (Exception ex) {
                Console.WriteLine("ERROR: " + ex);
                Console.Write("Hit RETURN to exit: ");
            }
            Console.ReadLine();
        }

        private void RunParser() {

            AntlrInputStream inputStream = new AntlrInputStream("hello world\n");
            HelloLexer helloLexer = new HelloLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(helloLexer);
            HelloParser helloParser = new HelloParser(commonTokenStream);
            HelloParser.RContext rContext = helloParser.r();
            MyVisitor visitor = new MyVisitor();
            visitor.VisitR(rContext);
        }
    }
 

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.

* You should see the following output in the console window:
 

 START

 HelloVisitor VisitR
 Visit Symbol=hello
 Visit Symbol=world
 DONE. 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.cs
 HelloLexer.cs
 HelloLexer.tokens
 HelloListener.cs
 HelloParser.cs
 HelloVisitor.cs

 

USEFUL LINKS

http://antlr.org/

 
ATTACHED FILES
 
A word file with this text in it.
A zip file of the Visual Studio Hello project. This should be placed in a solution directory and would be accompanied by a packages directory containing Antlr4.4.1.0-alpha003 and Antlr4.Runtime.4.1.0-alpha003 which are got from NuGet.
 

getting to first base with antlr4 c in visual studio.docx
Hello.zip

osca...@gmail.com

unread,
Aug 23, 2013, 2:28:17 AM8/23/13
to antlr-di...@googlegroups.com, nigel_...@phurst.com
Hello phurst

Perhaps it could be interesting do modify the phrase


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.

to give the users a clue of what should be changed in order to be able to use the command line for C# language (it is necessary to change the way used to get the path of the java executable, giving an alternative form to find it: one that doesn't depend of a key in the Windows Registry), as Sam replied in


Greetings,

Oscar

CSo

unread,
Feb 28, 2014, 9:24:07 AM2/28/14
to antlr-di...@googlegroups.com, nigel_...@phurst.com
hello phurst!
tried to follow your instructions: didn't work; so i downloaded your enclosed code archive: source compilation once more didn't work. i belive the system i am on (windows7, prof, 64bit, java7 64bit, vs2013, .net4.5 (and .net4.5.1)) fullfills the necessary preconditions to run your c#-project. Please consider the accompanying screenshots (java installation and version and manual try, registry, antlr4.jar, vs2013 project settings and compilation error [couldn't initialize the JVM]) and the following build-output:
(thx for your appreciated help)

NuGet package restore started.
Restoring NuGet packages for solution C:\Projekte\tests\antlr4\Hello\Hello\Hello.sln.
NuGet Package restored finished for solution C:\Projekte\tests\antlr4\Hello\Hello\Hello.sln.
Restoring NuGet packages for project Hello.
Skipping NuGet package Antlr4 4.2.0-alpha001 since it is already installed.
Skipping NuGet package Antlr4.Runtime 4.2.0-alpha001 since it is already installed.
NuGet Package restored finished for project Hello.
All packages are already installed and there is nothing to restore.
NuGet package restore finished.
1>------ Rebuild All started: Project: Hello, Configuration: Debug Any CPU ------
1>Build started 28.02.2014 14:50:32.
1>Antlr4Compile:
1>  Unknown build error: Executing command: "C:\Program Files (x86)\Java\jre7\bin\java.exe" -cp C:\Projekte\tests\antlr4\Hello\Hello\packages\Antlr4.4.2.0-alpha001\build\..\tools\antlr4-csharp-4.2-SNAPSHOT-complete.jar org.antlr.v4.CSharpTool -o obj\Debug\ -listener -visitor -Dlanguage=CSharp_v4_5 -package Hello C:\Projekte\tests\antlr4\Hello\Hello\Hello.g4
1>C:\Projekte\tests\antlr4\Hello\Hello\packages\Antlr4.4.2.0-alpha001\build\Antlr4.targets(132,5): error AC1000: Unknown build error: Error occurred during initialization of VM
1>C:\Projekte\tests\antlr4\Hello\Hello\packages\Antlr4.4.2.0-alpha001\build\Antlr4.targets(132,5): error AC1000: Unknown build error: java/lang/NoClassDefFoundError: java/lang/Object
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.50
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

hello_vs2013_net45.PNG
java_regedit.PNG
antlr4_jar.PNG
java_installation_and_version.PNG

Sam Harwell

unread,
Feb 28, 2014, 10:43:15 AM2/28/14
to antlr-di...@googlegroups.com, nigel_...@phurst.com
Hi CSo,

The current instructions for installing the C# target are located on the project page:
https://github.com/tunnelvisionlabs/antlr4cs/blob/master/Readme.md

The message you received is an indication that your Java installation is incomplete or corrupted. You will need to reinstall Java before you can generate code with this target.

Thanks,
Sam

John Washburn

unread,
Mar 4, 2015, 10:15:42 AM3/4/15
to antlr-di...@googlegroups.com, nigel_...@phurst.com
This is still a problem with version 4.3.  I have the  Java 7 JDK installed on my team city agent machines.  These are the machine upon which my VS2012 projects are built during the CI build process. The JDK installation is sufficiently well installed so that the Team City software on the agent machine can find the Java VM, but not sufficiently well installed for ANTLR build targets for VS2012 to find the Java VM.  Exactly how does the ANTLR language support plugin for VS2012 look for the Java VM when deciding if to throw the error:

Antlr4.targets(132, 5): error AC1000: Unknown build error: Could not locate a Java installation

?

John Washburn

unread,
Mar 4, 2015, 10:26:18 AM3/4/15
to antlr-di...@googlegroups.com, nigel_...@phurst.com
For Goal 1: Introduce language support plugin to locally install JRE:

I peeked into the source code, Antlr4ClassGenerationTaskInternal.cs, for v4.3 of the C# language support project.

If the registry is not setup correctly, but a JRE is actually installed somewhere on disk, then an alternative is to set the environment variable, JAVA_HOME, to the location of installed JRE; e.g. JAVA_HOME=%PROGRAMFILES %\Java\jre1.8.0_31
ble
Upon failing to find JavaHome within the registry, the C# plugin will then look into the environment variable of the machine and use the directory referenced in the JAVA_HOME environment variable.

maninder Lal

unread,
May 20, 2016, 10:48:55 PM5/20/16
to antlr-discussion, nigel_...@phurst.com
Hi,

I have tried to use antrl4cs (https://github.com/sharwell/antlr4cs).
I tried to follow this doc.
 At step 7:Goal: A program to parse the Hello.g4 grammar, parse a text string, and traverse the parsed tokens,
I could see lexer and parser cs files getting generated with empty partial class.
Later I found that there are more files generated but are copied to "<My ANTLR project>\obj\Debug" folder.

Is it expected ?

maninder Lal

unread,
May 23, 2016, 12:06:36 PM5/23/16
to antlr-discussion, nigel_...@phurst.com
Apologies, it is as documented :)
Reply all
Reply to author
Forward
0 new messages