[antlr-interest] Apparently equivalent code refactoring doesn't work

Jim Idle jimi at temporal-wave.com
Fri Feb 1 07:14:39 PST 2008


Steve,

When you translate your grammar, use the -debug option. This generates 
code that causes the parser to start and wait for a connect from a 
debugging tool (in this case ANTLRWorks). When you start ANTLRWorks 
debugger, you will see that there is a Remote Debug option on the menu. 
Use localhost as your name after starting your parser.

I use netbeans, but people have preferences for other IDEs of course. 
Netbeans uses ANT, which means that it can use the ANT task for ANTLR, 
which you can download from the site (somewhere, I forget where but a 
search should find it). Follow the instructions for making the ANT task 
available to ANT then create a new project in Netbeans. It will create a 
file called build.xml, which you can edit within netbeans. You usually 
put source code under the project directory in a ./src sub directory. 
Then you use the usual java directory structure for packages. Use the 
library manager to create a library that contains the ANTLR jar, the 
ANTLR 2.7.7 jar and the stringtemplate jar (at appropriate versions). I 
use ANTLRLatest as a library name for instance. Add this library to your 
project.

Now place the .g{3pl} or whatever suffix you use in the directory where 
you want the .java files to generate. So, if you have added 
@lexer::header and @parser::@header to the grammar and placed a package 
statement in there, then place the grammar where ever the .java files 
should be. 

Now, edit the build.xml file and add a -pre-compile target, where you 
can put the ANTLR tasks for each of your grammars. Now you can build and 
debug.

Here is an example layout and build.xml from one of my projects:

Directories:

vbdotnet\  - project dir
vbdotnet\java\src  - source package root
vbdotnet\java\src\com\temporalwave\parsers\vbdotnet  - grammar location

When it builds you get:

Vbdotnet\build - output classes
Vbdotnet\dist - location of output.jar
Vbdotnet\lib - copies of any libraries it depends on such as ANTLR

Then my build.xml looks like this (comments excluded):


<?xml version="1.0" encoding="UTF-8"?>
<project name="vbdotnet" default="default" basedir=".">
    <description>Builds, tests, and runs the project 
vbdotnet.</description>
    <import file="nbproject/build-impl.xml"/>
    
    <property name="grammar.dir" 
location="java/com/temporalwave/parsers/vbdotnet/" />
    <property name="antlr.libdir" location="C:/antlrsrc/lib" />

     <patternset id="antlr.libs">
        <include name="antlr-2008-01-10.16.jar" />
        <include name="stringtemplate-3.1b1.jar" />
        <include name="antlr.jar" />
    </patternset>

    <path id="antlr.path">
        <fileset dir="${antlr.libdir}" casesensitive="yes">
           <patternset refid="antlr.libs" />
        </fileset>
    </path>

    <target name="-pre-compile">
        <antlr:antlr3 xmlns:antlr="antlib:org/apache/tools/ant/antlr" 
            target="${grammar.dir}/vbdotnet.g3pl" 
               depend="true">

            <classpath>
                <path refid="antlr.path" />
            </classpath>
        </antlr:antlr3>
    </target>
    
</project>


Most of that text is the same for every project, with just the locations 
of things changed. Though I like Netbeans, I am not a fan of ANT really, 
so there may be better ways to configure the script, but that works 
nicely. 

If you want a debug version of the output, then change the task to add 
-debug to the ANTLR tool invocation when in debug mode (see Netbeans 
docs and ANTLR ant task readme). Then you can debug the grammar with 
ANTLRWorks and the output java with Netbeans at the same time. 


Remember a parser compiled with -debug will block at runtime until a 
debugger connects to it!

Jim


> -----Original Message-----
> From: Steve Bennett [mailto:stevagewp at gmail.com]
> Sent: Thursday, January 31, 2008 7:23 PM
> To: Jim Idle
> Subject: Re: [antlr-interest] Apparently equivalent code refactoring
> doesn't work
> 
> On 2/1/08, Jim Idle <jimi at temporal-wave.com> wrote:
> > The easy way is just to compile your grammar in a separate place 
such
> as
> > Netbeans, then run it with the debug option and tell ANTLRWorks to
> > connect remotely.
> 
> Ok, any tutorials on that? I'm not well-versed on Netbeans or even the
> ANTLR API for that matter. I do have a testbed java program up and
> running though...perhaps you mean that I just need to pass some kind
> of debug flag to the .Parse(...) call?
> 
> Thanks,
> Steve




More information about the antlr-interest mailing list