Wednesday, May 30, 2007

Cleaning up the ANTLR DRL grammar (Edson Tirelli)

Yesterday I had the pleasure to work with Terence Parr on cleaning up the DRL grammar. All I can say is that I can't recommend him enough. He is an awesome person, extremely professional and knows about what he is talking.

The tasks he accomplished were:

1. He started solving ambiguities in the grammar rules to turn the backtracking option off, since that makes the parsing process a bit lighter. To do that, some of Terence's magic was required, but it all looks great after he did.

2. Then, he moved on to fix some keyword clashes we still had in the DRL files. In the end, we basically reduced the set of reserved keywords to:
  • and
  • or
  • not
  • exists
  • eval
  • forall
So, be aware... do not use any of the above keywords in your fields, package names, etc, otherwise you will end up with parsing errors.

3. Third task of the day was a wish from Mark to have DRL syntax to support prefixed "or" and "and" for patterns. So now you will be able to write:

rule X
when
A() or B() and C() or D()
then
// do something
end

Or, if you are a LISPer, you may prefer:

rule X
when
(and (or A() B()) (or C() D()))
then
// do something
end

Both syntax are equivalent.

4. Fourth item was to design a strategy to improve exception handling. This is a very difficult task when the parser is used by an IDE that requires incomplete ASTs to be built, error recovery and absolute friendly messages. Terence gave us some suggestion and showed some features ANTLR offers. That will still require a good amount of work to get it right.

5. The fifth item was the discussion of how to improve the parsing process to provide more information for tools using it, like the eclipse plugin and the BRMS. Terence mentioned that incremental parsing is in the pipeline for new ANTLR versions, but not available yet. On our side (Drools), we will put some effort on adding support for that in the grammar after 4.0 release.

6. Finally, Terence went over our whole grammar and did a complete analysis, highlighting problems and providing suggestions on how to fix them. Most is manual work, since the real ambiguity problems he had already fixed. The problems left were eventual use of deprecated syntax or rules that we could have written in a more succinct way.

All in all, nothing is better than having the expert advising us on how things should be.

If you use ANTLR as your parser and wants some help, go straight to the source and buy support. You will not regret. And surely Drools users will be able to see that in the versions to come.

Happy Drooling
Edson

[edit: added info about incremental parsing]