I recently completed two ANTLR3 assignments (I'm working on my Master's in Computer Science) using Eclipse. I found no single document that had a process for installing, configuring, writing, and debugging a grammar in Eclipse. So, after working through various issues, I found the easiest thing to do was to stay in Eclipse for testing.
To use the process I have come to use (outlined below) you must first have the ANTLR IDE v2.1.2 installed. Add it right from inside Eclipse Indigo: http://antlrv3ide.sourceforge.net/updates. This site also has some useful doc on using the ANTLR IDE. Once installed, the IDE has to be configured. Video tutorials are a bit out of date but helpful. See a detailed how to guide on configuring ANTLR IDE in Eclipse. The main configuration item is the java output folder. Do this in Eclipse by going to Windows, Preferences, ANTLR, Code Generator, check Project relative folder and in the Output folder name box type a folder name (mine is called "antlr-java", others use "generated").
Test/Debug Process for ANTLR in Eclipse Indigo with ANTLR IDE
- After a new project is created, right-click it, select Configure, Convert to
ANTLR Project...
- Create the grammar in a .g file and save it. Note: filename has to match grammar name.
- If there are significant errors, debug the grammar. Eclipse shows the ANTLR error(s)
and what line(s) are affected. At first, these errors seem hard to understand but
they can be worked through by using various resources:
- The Definitive ANTLR Reference by Terence Parr the guy who wrote ANTLR
- the ANTLR Reference Manual
- google the error; many times you will end up here at stackoverflow;
in particular, Bart Kiers is both knowledgeable and helpful (Bart: thx for
the help you didn't know you gave me)
- On the first save after the serious ANTLR errors are resolved, the java output folder you
configured in Eclipse will be created and a java file in that folder will also be created.
- Right-click on the java output folder, select Build Path, Use As a Source Folder. This
tells Eclipse where to look for the project's java source.
- There are likely to be errors in the new java file. Select it, then search through looking
for java errors. Go back to your grammar or java file(s), correct the errors, and re-save
the grammar until both grammar and java files are error free, then run it.
- From this point on, it's the usual modify-run-debug cycle.
- The only other Eclipse change I needed was to create a few Run Configurations for testing
command line parameters.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…