View on GitHub

Antlr-4-playground

Playground for upcoming ANTLR 4 version

Download this project as a .zip file Download this project as a tar.gz file

ANTLR-v4 Playground

Disclaimer

I am not an expert in the area of language processing. Everything written below is just my humble opinion. If you think I am wrong please correct me! I share my thoughts in the hope they will be useful for someone.

Motivation

Since a while I am evaluating several parser generators for small and mid-size projects. I have tried ANTLR v3 and some other alternatives. I even tried some PEG libraries such as parboiled. That's why I created this playground project which contains small example applications that make use of ANTLR v4.

Options

ANTLR v3:

ANTLR developers made several design decisions that kept me from using ANTLR. What I really dislike is the fact that one has to mix different languages in one file (the grammar file). There will never be an IDE or editor that provides good support for that. However, good IDE support highly increases productivity. Even though ANTLR supports different target languages such as C it is not easy to reuse one grammar for different target languages. Another problem is the fact that even for relatively simple applications it is necessary to mess around with tree creation etc. For small applications such as mine it was no fun at all!

parboiled

First of all, I really like parboiled for its simplicity. It is possible to directly use Java or Scala to define the grammar. This is really clever. Doing that you can always claim to have good IDE support. What I don't like is that with PEG grammars rule orders and spaces do matter a lot! This is a huge disadvantage.

ANTLR v4

I almost decided to use parboiled instead of ANTLR v3. But when I tried ANTLR v4 I was really impressed how simple it is. It seems like the developers have totally changed their mind regarding what matters most. Great! Grammar and code are strictly separated from each other. In addition to the automatically generated parser it is possible to use visitors to walk through the parse tree.

Which tree? Yes, you guessed right. The tree is automatically generated!

As it is possible to create labels in the grammar file the visitors automatically get the right methods. This is a nice way to integrate the grammar into the target language.

AUTOMATIC . How often did I use this word in the last section? ANTLR v4 seems to be all about automation and simplicity. Great!

Summary

Finally, ANTLR v4 seems to be the way to go. The expr project demonstrates how easy it is to write an expression evaluator.

Project Details

NOTE

I HATE Maven like dependencies. They never work. There is always a missing library. That is why ANTLR sources and binary build dependencies are included.

Compile

Open the antlr-4 project in NetBeans (location: antlr/antlr-4) and build the project. The final ANTLR library can be found in the antlr/deploy folder.

Try Experimental Applications

Open one of the NetBeans projects that are located in the antlr/experiments folder and run it.