Helper for ANTLR Grammar
So I mentioned I am working on a ANTLR grammar (though not for long). And the experience with it was not always smooth.
- We are trying to define a grammar for an existing language which is pretty complex (several hundreds of statements). Every new parser rule had the potential to break the grammar. After inputting several rules, we had difficulty identifying the problematic one. It required a lot of discipline to compile the grammar after each change.
- Two developers are preparing the grammar together. Once or twice a day, we are merging the .g file. The merging proved to be quite error-prone.
- The ANTLRWorks IDE is not always stable. In some scenarios it freezes and has to be restarted.
- The errors and warnings were displayed together, so we had no way of filtering only errors (and we had many warnings).
- The C# code generation is buggy in some scenarios - we got an error of a duplicate identifier.
- After each compilations we had to overwrite the lexer and parser CS file in our C# project.
So I wrote an desktop application that monitors the ANTLR grammar file and on each change, it compiles it and does some post-processing to fix the C# code using a simple regex.
· Specify settings in .config file (grammar file, working folder, antllr install dir)
· View compilation output - complete output, and only errors
· View history of compilation attempts and results
· View notifications as balloon tips (displayed only on significant changes – broken grammar and fixed grammar)
· Automatically save the last successful build in a subfolder of the working folder
Was it worth it?
Yes, automating error-prone operations is always a good idea. We had problems with merging a few times and this continuous compilation enabled us to quickly find the problematic rules. The tool took me initially 3 hours to develop and additional 50% to maintain and bug fix.
How could we extend it?
Generating the C# files from the grammar is not enough. Its output should be included in the project. Then compile successfully. Then pass some unit tests. We have not reached this stage yet.
Points of interest
There is a good discussion here about how to change the .g file, save, hit F5 in Visual Studio and have it use the most recent version of the generated files. Apparently you need a custom tool to do so. A custom tool as a managed assembly that contains a class that implements the IvsSingleFileGenerator interface. Here is a link to an article how to create such a custom tool.
P.S. If you are interested in the code, let me know. I would have uploaded it on CodePlex, but currently it has a dependency to a third-party grid, so I should better use a normal grid, instead.