[antlr-interest] Re: Trouble modifying Java source w/ ANTLR

cliftonccraig ccc at icsaward.com
Fri Feb 20 08:59:35 PST 2004


Thank you everyone,

The thing is I'm new to both grammar engines and sed. So to me it's
the lesser of two evils. I've gotten a little further in this as I've
been able to figure out the Java grammar a little more. So far I've
narrowed where my mods go to the variableDeclarator rule. This is what
I have so far:
variableDeclarator![AST mods, AST t]
	:	id:IDENT d:declaratorBrackets[t] v:varInitializer
		{
		    //*CCC- scan here for version indentifiers and replace the
initializer with the new value.
		    if(id.getText().equals("VERSION"))
		    {
		        int start = ((TokenWithIndex)id).getIndex() + 1,
		        stop = start + #d.getNumberOfChildren() +
#v.getNumberOfChildren() - 1;
		        //System.out.println("Starting from " + start + " ending at
" + stop);
		        //System.out.println("prevToken ==" + prevToken);
		        engine.replace(start,stop,"\"MODIFIED!\"");
		    }
		    #variableDeclarator = #(#[VARIABLE_DEF,"VARIABLE_DEF"], mods,
#(#[TYPE,"TYPE"],d), id, v);
		}
	;

It gets a little hairy because the "stuff" I want to replace is not
defined as tokens in the grammar, rather they're defined as parser
rules. The rewrite engine example shows replacment using tokens but I
don't know how to get an accurate token count from the two AST objects
 at this point. I know the one (#d) is really just a place holder as
there will be no array declaration here but the other does not seem to
be yielding the right amount of child nodes. I think I'm at a
misunderstanding here (child-nodes != tokens)? When I run everything
with my above mods I get something like the following:
public final static String VERSION"MODIFIED!"= "2.3";
 where it clearly looks like the node count is evaluating to one and
the token count is much higher (accomodating for whitespace and such).
I don't want to hard code this because the version assignment may not
always be a simple assignment. It could be an expression and I want to
be able to replace the whole expression. Could you steer me in the
right direction here?

Cliff

--- In antlr-interest at yahoogroups.com, Terence Parr <parrt at c...> wrote:
> 
> On Feb 19, 2004, at 1:13 PM, Robert Colquhoun wrote:
> 
> > Hello,
> >
> > At 07:08 AM 20/02/2004, cliftonccraig wrote:
> >> I just picked up ANTLR and I'm trying to determine the easiest way to
> >> build a Java Parser that can insert and replace VERSION strings in my
> >> Java source files. I have the Java grammar available on the ANTLR web
> >> site and I have the source files from the TokenRewriting article also
> >> on the site. Could someone walk me through where in the Java
grammar I
> >> should put logic to do the insert? I really need help on this and I
> >> appreciate any and all help provided.
> >
> > Have you considered using ant(see the replace task) or sed or 
> > something to
> > do this?
> >
> > IF you you antlr possibly you could just lex it provided you knew the
> > version string you were looking for was unique enough  Otherwise you 
> > will
> > need to parse it and then search through the resulting AST, do the 
> > replace
> > then print the AST  out again....remembering to preserve
whitespace etc
> 
> Yep, sed would work if the variable was always on the same line 
> completely specified.
> 
> The rewrite engine works great.  Just add an action to the rule(s) that 
> match a member variable that replaces the expr token(s) with the text 
> of what you want. :)  If you're new to parsers and grammars, it may be 
> easier to just go the sed route.
> 
> Ter
> --
> Professor Comp. Sci., University of San Francisco
> Creator, ANTLR Parser Generator, http://www.antlr.org
> Cofounder, http://www.jguru.com
> Cofounder, http://www.knowspam.net enjoy email again!
> Cofounder, http://www.peerscope.com pure link sharing



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list