Thursday, February 23, 2012

Using ANTLR with Visual Studio 2008 (C# Target)

ANTLR can be used with different language targets such as C# and Java. This is how to use ANTLR with C# in VisualStudio IDE.
  1. Install Java Runtime Environment (JRE).
  2. Verify the above using "java -version" command.
  3. Set the Java CLASSPATH variable to point the ANTLR package version you want to use. Ex: antlr-3.1.3.jar
  4. Build the ANTLR project to verify that there are no issues using following command:
  5. java -cp "path to ANTLR package" org.antlr.Tool "grammer file name"
  6. Ex: java -cp "C:\antlr\antlr-3.1.3.jar" org.antlr.Tool tsqllexer.g
Some important options:

If you get "java.lang.OutofMemoryError: Java heap space" issue, use the following command to modify the JVM heap size.
Xmx750M

If you get "Multiple token rules can match input such as X, Tokens X.Y was disabled for that input" issue, use the following command to set NFA conversion timeout for each decision for a suitable value.
-Xconversiontimeout 10000
  1. Create new C# project.
  2. Add these dlls as references: antlr.runtime.dll, Antlr3.Runtime.dll, Antlr3.Utility, StringTemplate.dll
  3. Build the C# project.

1 comment: