ANTLR4 with C# in Visual Studio 2015…

So, some three years after the site launch, we have used plenty of products, but with kids, family and work, little time to write about it…

Nevertheless, I had been experimenting with ANTLR4 for about over half a year now. I had initial issues setting it up due to the somewhat out-dated instructions available then. When I was “forced” to upgrade to Windows 10 and re-installed everything from scratch, I realised I had totally forgotten what the steps were to get ANTLR4 working with Visual Studio 2015

Hence, in an effort to help others and as a reference to self, please find a step-by-step guide to using ANTLR4 (version 4.3.0 stable) with Visual Studio 2015 (Community Edition) (although the instructions should work for other VS editions as well).

Main Points

  • a JRE needs to be on the executable search path (i.e. the absolute path is in the %PATH% environment variable)
  • installation of the ANTLR4 packages is via the NuGet Package Manager
  • grammar file(s)’ compilation options need(s) to be customised manually by editing the project’s configuration file (*.csproj)
  • the latest stable version of ANTLR4 (version 4.3.0) is only supported on .NET Framework 4.5 and below, therefore there is a need to change the target framework from the default (if the default version is higher)
  • references to the ANTLR namespaces are required

 

Detailed Steps

  1. install a Java Runtime Environment (JRE) (whereas I am aware that later versions of ANTLR includes an IKVM, I prefer to use the “standard” JVM)
  2. assuming VS is installed, ensure that you have the latest NuGet Package Manager first:
    1. under the “Tools” > “Extensions and Updates…” menu option ensure that NuGet Package Manager is update:
      NuGet Package Manager Update
    2. in the same window, search for “antlr” and install “Antlr4Code”, an ANTLR grammar-aware editor extension:
      Antlr4Code Editor
  3. open or create a new project/solution, then start adding the ANTLR4 NuGet packages:
    1. in the Solution Explorer window, right-click on the solution and select “Manage NuGet Packages for Solution…” to quickly add ANTLR4 packages across multiple projects:
      NuGet for Solution
    2. or, in the Solution Explorer window, right-click on a project and select “Manage NuGet Packages…” to add ANTLR4 packages to that specific project:
      NuGet for Project
    3. browse/search for “antlr” and then select and install the “Antlr4” and “Antlr4.Runtime” packages:
      NuGet ANTLR Packages
  4. create or add your grammar file (*.g4):
    1. assuming creation from scratch, right-click on the project (which has the ANTLR4 packages) and select “New Item…”:
      New Item
    2. select “Text File” and give it a name with a “.g4” extension:
      g4 Text File
    3. the file will be opened in the editor, save it as a UTF-8 without BOM encoded file by selecting “File” > “Advanced Save Options…” and selecting the correct encoding accordingly:
      g4 Text File Save As
  5. configure the necessary customised options to compile your grammar (*.g4) file(s):
    1. in the Solution Explorer window, right-click on the project containing the grammar file(s) and select “Unload Project”:
      Unload Project
    2. right-click on the unloaded project again and select “Edit <project name>.csproj” to edit the project configuration file directly:
      Edit Project Configuration File
    3. with the project configuration file open, search for each of the grammar files’ names to find the XML node configuration snippet:
      Search for Grammar File Nodes
    4. replace the XML node with the following snippet, replacing the portions highlighted in red accordingly:
      <Antlr4 Include="<grammar file name>.g4">
                  <Generator>MSBuild:Compile</Generator>
                  <CustomToolNamespace><project namespace></CustomToolNamespace>
      </Antlr4>
      

      Edit with Custom Compilation Instructions

    5. save the configuration file, and in the Solution Explorer window, right-click on the unloaded project and select “Reload Project”:
      Reload Project
  6. change the project’s target framework version:
    1. in the Solution Explorer window, right-click on the project and select “Properties”, then change the target framework to “.NET Framework 4.5” or below:
      Change Target Framework Version
  7. finally, ensure to reference the appropriate ANTLR namespaces (“Antlr4.Runtime” and “Antlr4.Runtime.Tree” and any others as necessary) in your code:
    Referencing ANTLR Namespaces

 

A big “Thank You” to the creator of ANTLR and all its contributors!
– Daniel

Leave a Reply