How to make ANTLRWorks and Visual Studio work together - with C# as the target language.
This has been tested on Vista (32-bit and 64-bit), and Windows 7 Beta 1 (64-bit) with Visual Studio 2008 and 2005
Download and Unpack ANTLR
- Download the latest version of the ANTLRWorks GUI Development Environment (Windows, Linux and Mac OS X).
- Download the latest version of ANTLR v3 (ANTLR Version 3.1.3 source distribution)
- Create a new folder of your choice. For this example assume C:\ANTLR, however any location may be chosen
- Unpack antlr-3.1.3.tar.gz into the new folder C:\ANTLR
- Unzip the file C:\ANTLR\antlr-3.1.3\runtime\CSharp\dist\DOT-NET-runtime-3.1.3.zip into C:\ANTLR\antlr-3.1.3\runtime\CSharp\Libraries
Install Java runtime and JDK
- ANTLR requires the Java runtime to run. Download and install if you don't have it.
- ANTLRWorks requires the JDK (Java Development Kit) for debugging. Download and install as needed.
Use ANTLR from Visual Studio
- Create a new Visual Studio project
- Add these project references from the folder C:\ANTLR\antlr-3.1.3\runtime\CSharp\Libraries
- Antlr3.Runtime.dll (Antlr runtime 3.1.3)
- antlr.runtime.dll (Antlr runtime 2.7.7 for StringTemplate)
- StringTemplate.dll
- Antlr3.Utility.dll
- Start ANTLRWorks with a double click on the jar-file (antlrworks-1.2.3.jar)
- Copy the following test grammar into the new file. Note the language property in the options section of the grammar is what specifies C# targeted output.
grammar Test;
options
{language = 'CSharp2';
output=AST;
}
expr : mexpr (PLUS^ mexpr)* SEMI!
;
mexpr
: atom (STAR^ atom)*
;
atom: INT
;
//class csharpTestLexer extends Lexer;
WS : (' '
| '\t'
| '\n'
| '\r')
{ $channel = HIDDEN; }
;
LPAREN: '('
;
RPAREN: ')'
;
STAR: '*'
;
PLUS: '+'
;
SEMI: ';'
;
protected
DIGIT
: '0'..'9'
;
INT : (DIGIT)+
; - Save this file as Test.g (the name of the file has to equal the name of the grammar) in your VS project directory.
- Add existing item Test.g (the file you just saved) to your project.
- Now go back to ANTLRWorks. Do Ctrl+R (check grammar): You should get a success message.
- Open the menu File->Preferences, choose tab General and set the output path to your Visual Studio Project directory. (In case you experience any Java related problems: check tab Compiler to set the path to javac). Click APPLY and close Preferences Window.
- Now try Ctrl+Shift+G (Generate Code). You should get a success message.
- Add the generated .cs files to your VS project.
- Back in Visual Studio you should be able to build your solution and work with the parser.
- Whenever you need to make changes to your grammar, you can do it in ANTLRWorks now and lexer and parser will be updated automatically in your project.
Debugging with ANTLRWorks
- From the menu select File->Preferences, choose tab Compile and set the javac path to <JDK Path>\jre\bin
- Add an exception to the Windows Firewall (or other firewall) to grant network access to java.exe and javaw.exe. By default these programs are installed to C:\program files (x86)\java\jre6\bin.
- From the menu select Debugger->Debug..., then set the option "Line Endings" to Windows (CRLF)
- For the "Text" option type in the expression "2+3*4", then press the enter key to add a CRLF (carriage return/line feed) to end of the expression, then click OK.
- You can now use the VCR style buttons to step through, debug, and run the grammar in the debugger.
What Next?
If you're not sure where to go next, a good start is to follow the tutorials on the help page.
8 Comments
Hide/Show CommentsOct 21, 2008
Sven Prevrhal
Looks like the automatic updating does not work with Visual Studio 2008 Express (C#).
Mar 23, 2009
Lee Whitney
The "automatic updating" should work fine with VS Express, however in any case it only applies when you have a generated code file open in the editor. If you don't there is not a difference.
Finally notification or not, VS will always compile from the latest file on disk and will always include the latest files generated by ANTLR.
May 28, 2009
cpedros
I'm presuming in the context of the above example we need to remove the output language:
from the grammar file in order to be able to debug it in ANTLRWorks?
Jun 03, 2009
Adrian Waters
I am very much a newcomer to this area so would welcome a little assistance.
I have followed the instructions in this page as best I can. I can compile my C# program and run it and connect the ANTLRWorks debugger to it. However, when I use the example expression "2+3*4", the parsing ends with the debugger showing "nil" -> "<missing INT>".
If I change the expression to "27+3*4", the debugger breaks showing "27" as the input and "nil" in the tree. Clicking "Step Forward" then changes the tree to "nil" -> "27".
I am not sure what I am supposed to see but I sense that this is incorrect. Why is "2" showing "<missing INT>" but "27" is recognised correctly (I copied the grammar directly off this web page)? Why, even when the integer is recognised, does parsing then stop, rather than continue with the rest of the expression?
I would really appreciate an example of how to process the AST in my C# program.
Many thanks in advance for any help.
Jun 17, 2009
Tony Gray
I'm also a newcomer and hadn't had any success at getting the debugger work work with C# as the target language. All I got was timeouts waiting to connect to the parser. My biggest problem was that I didn't really understand how debugging works generally with Antlr. Things got much better when I read:
Antlr 3 CSharp Target Debugging
Tony.
Oh, on another note, item 1) above it says "From the menu select File->Preferences, choose tab Compile and set the javac path to \jre\bin" but there is no javac.exe in that directory, I had to set it to\bin to get it to work.
Jun 19, 2009
Diego P. Dobniewski
Hello Tony
I am using ANTLRWorks 1.2.3, but I compiles the grammar with "antlr-3.1.3.jar" from command line, and Visual Studio 2008 Team System
I am using ANTLRWorks Debug Remote feature
I used:
java -classpath antlr-3.1.3.jar org.antlr.Tool -debug -message-format vs2005 grammar_file.g -o output_dir_to_visual_studio_source
I am use "-message-format vs2005" for MSBUILD integration. You shouldn't use it
Try this (works for me):
Best regards
Diego
NOTE (post modification)
I am using Windows Vista 64. I have realized that in my C:\Windows\System32\drivers\etc\hosts file, the entry "127.0.0.1 localhost" was missed, I fix it.
Now the conten of my hosts file is:
127.0.0.1 localhost
::1 localhost
"localhost" works as host for ANTLRWorks debugger!
Nov 04, 2011
Steve Knight
For newcomers like me, and Adrian Waters (circa 2009, above) there is an issue with this grammar in the latest v3 of ANTLR. To begin with the grammar does not generate/compile properly because the hidden channel should be called: 'Hidden' and not 'HIDDEN'. After making this change the whole example generates and compiles just fine but then when you try and debug the parse tree it produces the leaves of that tree show the disconcerting error: 'MismatchedTokenException(0!=0)' when the expression consists of single digit INTs.
The issue is with the DIGIT lexer rule which is called by the INT rule this needs to be marked as a fragment as does any lexer rule that is referenced from another lexer rule. So, according to the ANTLR Reference Manual, it should be written: fragment DIGIT: '0'..'9';
But I think this example should probably be rewritten because it's a little misleading since it's not compatible across all versions ... I might have a crack but first I think I need to understand ANTLR better!
Jan 06, 2013
Tony Gray
Sam Harwell has conceded that debugging the C# target through ANTLRWorks didn't work as of Feb 2012, and I'm currently unable to get it working for my AST grammar at the moment (Jan 2013)
C# debugging thread
I haven't checked thoroughly to see if the behaviour is any different between code generated with the .NET tool + Visual Studio integration and the Java tool. The allowed .g grammar does differ slightly for me because I'm using heterogeneous AST nodes (the .NET tool requires the "node=" prefix on the emitted node type, the Java tool forbids it) so I can't use one grammar file for both tools.