How to create your own language with ANTLR in .NET?

Konstantin Poklonskii
4 min readJul 20, 2020
Photo by David Ballew on Unsplash

How to create your own languages in minutes? If you would say that’s impossible this article for you. ANTLR 4 (ANother Tool for Language Recognition) is a way to go, in the article you can find the sample of using ANTLR 4 in .NET in order to create domain-specific language (DSL).

I will use the practical example of the discount system that I was working on. Imagine you have an e-commerce website with lots of different discount types rules, not trivial models for the catalog of your products, each product related to different groups, many configurable options for each product. In the end, each aspect of it should be discountable. Just to easily imagine the complexity of such models let take an example of IKEA's wardrobe. If you are not familiar, you can create your own configuration of it. You can configure everything - shelves, size, doors, doors handle, legs, type of the material, etc. If the marketing team wants to have discounts for each of these pieces with all possible different combinations, the domain model of such discounts can go crazy. Eventually, it will contain lots of properties in your API payloads. Your UI would look like this:

Too many buttons

Too many buttons, right? Instead of all of these, we can create a DSL that serves our needs. The complicated UI can be replaced with one single text field, as well as an API contract simplified a lot, instead of dozen properties we will have the only one single string property. You can make the DSL as close as possible to the natural language, so not only software engineers will understand this, but also the domain experts. Also, the language can be used as an intermediate layer between UI and back end. In the case of a discount platform, the language allows you to create a discount with simple rules easy, but it would be still possible to create complicated rules as well.

“Simple things should be simple, complex things should be possible.”

Alan Kay

But let’s dive into the sample that I prepared, with code it always much easier. The whole project is in my GitHub repo, check this out.

If you want to know how the ANTLR 4 works I recommend you www.antlr.org, they also have published their code on Github.

In the sample, all our grammar rules are placed in two files DiscountsLexer.g4 and DiscountsParser.g4.

Basically, the first file contains your keywords. The second contains the grammar rules:

You need to include the Antlr4.Runtime NuGet package into your solution and I also recommend adding the Antlr4.CodeGenerator in order to generate a parser for your grammar during the build, otherwise you have to use the java antlr-4.8-complete.jar to generate it. ANTLR can generate parsers for many programming languages including C#, JAVA, Python, JavaScript. After you add .g4 files and all necessary packages, run the build in VisualStudio, and you’ll get the autogenerated classes which going to help you to parse the rules of your DSL. There two ways of traversing the syntax tree with ANTLR: Listeners and Visitors. Visitors have one advantage, their methods return values, what’s why we are going use them. All you need to do is override one of the methods of autogenerated generic visitor class, in our case, it’s DiscountsParserBaseVisitor<T>, where T is the type that you want to return from the visitor.

For each rule in DSL, we have a method generated with a name Visit(NameOfExpression), in the example above it’s “apply”. ApplyExpression is the custom model that we want to return as a result of parsing. We also can use visitors for subexpression, for example, for products we can use Accept method with visitor object as the parameter. In order to get the value of the node simply call GetText().

In order to parse the whole tree, create a visitor for the root expression and using the Antlr4.Runtime library returns the model that you want.

Now we can use the model to implement our business logic applying the discount. In the sample, we just get the filtered list of products and apply a percentage discount to them. I added the tests just to show which discount rules we can create and apply using DSL. Product model is the name and price of the product, Basket just container of products.

As you can see we can have already complex rules in or DSL, we can apply different percentages of discounts for different groups, we can add specific requirements for the basket using the language. The demo sample looks pretty simple, but we can extend our grammar further to get really existing results.

I hope the article was useful to you. Where do you think this approach with creating DSL might work as well? Have you done something like that already? If want to know more about Domain-Specific Languages and the cases when you can use this approach check the blog of Federico Tomassetti.

Looking forward to hearing from you LinkedIn.

--

--