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.
- You write the grammar using Antlr syntax.
- You generate the parser using Antlr.
I will state all the resources I found on the internet to get my parser done.
- You have to get familiarize with the grammar.This is an article about getting started with Antlr3. It was a great help for me and that seems an excellent starting point for a fresher. There are 3 articles. So, don’t miss that :).
- 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.
- There is a huge list of available grammar in Antlr. You them at your own risk. 🙂
- 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.
- You can get an excellent guide to write grammar from this video tutorial.
- 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.