Using ANTLR4 MSBuild Tasks in Visual Studio 2017 and with the Dotnet CLI

Georg Dangl by Georg Dangl in DotNet Friday, August 11, 2017

Friday, August 11, 2017

Using ANTLR4 MSBuild Tasks in Visual Studio 2017 and with the Dotnet CLI
Posted in ANTLR4

With the latest 4.6.5-beta001 release of ANTLR4 for C#, it's finally easy again to work with ANTLR grammars in Visual Studio. Since the introduction of the project.json format, you had to write custom scripts to generate your lexers and parsers, cluttering your workspace with lots of generated code. Not anymore! It's now as simple as adding Antlr4 items to your csproj file, point them to your grammar files and the MSBuild process takes care of it. Generated files are again stored in the obj/ folder and don't mess with your project structure.

Here's an example of the new build tasks:

You can use the optional PropertyGroup and set Antlr4UseCSharpGenerator to true to circumvent the Java tooling altogether and rely on the new, native C# code generation. This process works seamlessly both from within Visual Studio 2017 as well as the dotnet CLI. I've got a demo project for you to dive right in on GitHub.

Remember, you only have to use the pre-release Antlr4.CodeGenerator as a build time dependency, the runtime can be kept at the latest stable version, not blocking you if you have to use stable versions or create packages of your own.

Happy parsing!

Share this post

comments powered by Disqus

About me

Hi, my name's George! I love coding and blogging about it. I focus on all things around .Net, Web Development and DevOps.


Need a consultant for BIM, GAEB or Software Development?

Contact me at [email protected], +49 (173) 56 45 689 or visit my professional page!

// Just 💗 Coding

Social Links