[environment info]
ANTLR ver 2.77
Visual Studio 2003
C++ language
Thanks to the detailed "getting started" documents from the ANTLR online resources, I successfully compiled and run my first ANTLR project in VC++. This tutorial is very clear on building ANTLR Library and configurations of your own projects. It is written for VC6, but can be easily implemented in VC7. I'd suggest you refer to this tutorial first to get your first ANTLR project live before reading the following staff.
Usually, we use ANTLR tools like this:
[translate grammar file into source code] --> [compile project] --> [edit test case] --> [run]
I used to swithing among my project in VS, editor for my test case and "antlr.Tools" frequently everytime I modified my grammar file. That was really challenging for my Alt-Tab action. So I wrote two small batch files and integrated them into VS2003.
1. copy antlr.jar to your source code folder
antlr.jar file is not big (435k). So I copied it to my source code folder to avoid all the classpath settings. If you really don't like this step, you can skip it and set your classpath as is described in the tutorials mentioned above.
2. create batch file for Parser/Lexer generation
Create the following batch file -- gen.bat
@echo off
cls
echo ---------------------------------------------
echo Generate using ANTLR
echo ---------------------------------------------
java antlr.Tool mygrammar.g
echo -------------------end-----------------------
pause
Save it as gen.bat in your source code folder. The bat file will popup a cmd window telling the result of the generation and will disappear on "any key pressed".
3. create batch file for running exe
By default, debug/release exe file will be built in the sub folder of the project source folder. So we create the following batch file -- run.bat (my exe filename is "sdscript.exe" and the test case script is "demo.script")
@echo off
cls
cd debug
sdscript demo.script
pause
4. run everything in VS2003
drag and drop these files into your project explorer tree:
- grammar file (*.g)
- test case scripts
- gen.bat
- run.bat
Then, right click on the gen.bat batch file item in the project tree --> open as...
In the popup dialog, choose the "gen.bat" item and "set default". (Repeat this step for run.bat)
We now can double click on the batch file item to run it!
If you did all the steps, the general compiling routine would be:
- edit your grammar file (*.g) in vs2003 ( and save )
- double click gen.bat item to refresh parser/lexer source code
- perform "build"
- edit your testing files in vs2003 ( and save)
- double click run.bat item to see the result
Isn't that cool?
There are also some other tricks helping us automating our build/generate works. You can found one of them here in antlr's wiki site.
Hope you enjoy it :)