Skip to main content

Using NCrunch with ANTLR 3

· 2 min read

When I started out trying to use NCrunch, I was working on a project that needed to parse a domain-specific language created by someone else. To do the parsing, I was studying the use of ANTLR 3, which took me a while to learn and I wasn’t quite sure my grammar was correct, and that it would remain correct as I made small changes to it in order to accommodate some more obscure details in the language.

I decided simple unit tests to verify the parsing process would be a good idea – it would be simple to add new tests to verify each new language statement, and I’d easily know if I broke anything in the pre-existing parts. Sadly, however, NCrunch didn’t seem to work very well with ANTLR: after changing the obvious settings, NCrunch would still occasionally (but often) fail building the ANTLR project with a strange internal error from my grammar file:

System.InvalidCastException: Unable to cast object of type 'Antlr3.Targets.CSharp3Target' to type 'Antlr3.Codegen.Target'

Google didn’t find anything (probably not very many people use NCrunch and ANTLR), so I posted a question on the NCrunch forums, and got support very fast from the NCrunch developer, and we were able to find a way to make NCrunch able to build ANTLR projects:

  • Set “Performance/Build process memory limit” under “All Solutions” to 1. This causes NCrunch to restart the build process after each build, so it might be a good idea to change this back to 0 (or some other value) when not using ANTLR to get better performance.
  • Set “Build Settings/Run post-build events” and “Build Settings/Run pre-build events” under the project using ANTLR to True.
  • Add your grammar file and ANTLR tool directory (if under your solution, e.g. the Reference directory as discussed in the ANTLR 3 documentation) to “General/Additional files to include” under the ANTLR-using project.