Getting Started with ANTLR 4

I have been working with ANTLR 4 parser generator for my GSOC 13 project which is to generate a CSS parser and CSS coding support in the text editor in MonoDevelop. I have to mention that ANTLR is a very active project and you can find a good help from the authors in any issue that can come across.  You can also get a good help form the book they have published. But this is basically an explanation for Java developers. But you can get an Idea of what has to be come if you are working with C#.

I was working with a CSS parser and you can find a list of free parser grammar. You can find them in this link. But most of them are in Antlr3 grammar, but it is very easy to convert them to Antlr4 :).

You will be writing the grammar in Antlr syntax which is easier and Antlr will convert it to your favorite programming language. I used this to generate a C# CSS parser and I was able to find an already existing css parser in their list I mentioned above. Anltr has an excellent support group where you can get a good help. And also the Co-author Sam is also very active in Stack Overflow.

There are only two things that you have to create the parser which is very easy :). If you are familiar with theory of computing – context free grammar the syntax is very easy to follow.

  1. You write the grammar using Antlr syntax.
  2. You generate the parser using Antlr.

I will state all the resources I found on the internet to get my parser done.

  • There is a small video by the author Terence Parr available in YouTube. This is more of a motivational stuff.
  • You can download the C# targets for Antlr here.
  • Converting Antlr 3 to Antlr 4 is one issue you will face. So just in case here is a link to deal with them. Syntactic Predicates are a major thing that will come across. Here is another link for dealing with upgrading.
  • Finding a list of segments that are related to one particular grammar rule is very easy in Antlr. Here is a link which suggests several alternatives.
  • You have to have some mechanisms to accumulate the errors generated by the parser. Here is who it is done in Antlr4 which is a bit different in Antlr 3 grammar.
  • Since it is C# for me. I used Visual Studio 2012 and I had to do some changes your my project file. Here in Git-hub you can find how to set up the development environment. If you are interested in contributing GO AHEAD and do that !!
  • You can divide the stream that has to be parsed using streams and later you can access them. Here is how it is done.

I will add some more guidelines when I completed my project. Thanks.

Advertisements
About

I like music, gaming, maths and programming. I am a student of University of Moratuwa.

Tagged with: , , ,
Posted in Antlr, uncategorized
One comment on “Getting Started with ANTLR 4
  1. […] After the parser is implemented I bound it to IDE to create CSS files in projects and completed syntax coloring and folding of CSS elements and comments. I have also completed the friendly error message displaying but that can not be taken on the build error lists because this project is not integrated to a projects to use CSS in their projects.  You can find these demonstrations on the video posted on this blog. […]

What you have in your mind!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: