Fwd: [antlr-interest] New User help!

Bans VGLab bans.vglab at gmail.com
Fri Jun 30 06:32:33 PDT 2006


From: Bans VGLab <bans.vglab at gmail.com>
To: Walter Schilling <walter.schilling at computer.org>

Yes.

I think, I'll be able to help you out here.

See, you can easily think of writing-back (or code-generation, in precise
compiler parlance) as tree-traversal, which can be pre-order, post-oder or
in-order. This is a general rule followed.

I you don't understand what are pre-order, post-oder or in-order traversal,
look into any 'datastructures' book :)

In your case, operators "==" and "&&" will be written In-order to generate
the code. The nodes like 'if ' and '{' are to be written pre-order.

Writing Pre-order is simple, you just have to write in the order the tree is
walked.

Writing In-order is a bit tricky because, for instance, you can not print ==
before the operands are printed. So, the usual way is to return the String
form of operands, say for instance, "x" and "0" to the parent node "==" and
then print the concatenation "x==0" in the action block of parent node, that
is "==".

So, if your current tree walker is something like this:

root: #( "==",  op, op)
        {
        }
       ;

op
     : IDENTIFIER
     | LITERAL
     | #(EXPR ....)
     ;



alter it like this (a crude code, just pay attention to the action block):
root returns [String ret]
    : #( "==",  ret1=op, ret2=op)
       {
          ret = ret1 + "==" + ret2;

          // This will print "op1==op2"
          System.out.println(ret);
       }
    ;

op returns [String ret]
     : iden:IDENTIFIER  {ret = iden.getText();}
     | lit:LITERAL {ret = lit.getText();}
     | #(EXPR....)
           {
               /*  write code similar to that in the "root" action block
                *  and return it through the ret.
                */
            }
     ;

--------

Please ask if you have further queries.

Cheers
Sujeet

----------------

On 6/30/06, Walter Schilling <walter.schilling at computer.org> wrote:
>
> I'm just starting to use ANTLR and I've got a question that I think
> should be obvious but I have yet to see an answer for.  I'm sure I'm
> just looking in the wrong spot.  I'm writing a Java analysis tool that
> uses the Java ANTLR grammar.  On various occasions in my design, I need
> to take the AST that I have generated and translate / walk that back to
> the original text.  For example, if I had the java construct:
>
> if ((x==0) && (y==true))
>
> the AST would be represented as :
> if
>   EXPR
>     &&
>       ==
>         x
>         0
>       ==
>         y
>         true
>   {
> ...
>
> Given that a portion of my data structure is an AST starting with the
> EXPR node, is there any easy way I can walk the tree and regenerate my
> original text?  I'm thinking this should be something easy and obvious,
> but short of hand coding a java recursive loop (which so far has not
> achieved quite what I want, I haven't found an easy way to do this.)  It
> feels like there should be some way to use the existing tree walked I
> have to do this, but I'm thus far drawing a blank.
>
> Thanks for your assistance,
>
> Walt Schilling
>
> --
> Walter W. Schilling, Jr.
> 2004 - 2007 Ohio Space Grant Consortium PhD. Fellowship Recipient
> University of Toledo PhD Doctoral Candidate
> MSES 1998 University of Toledo
> BSEE 1997 Ohio Northern University
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060630/8362406d/attachment.html


More information about the antlr-interest mailing list