[antlr-interest] rewriting stream or generate own trees

Bryan Ewbank ewbank at gmail.com
Tue Feb 22 04:10:40 PST 2005


One solution is to delay the problem until you emit the code, and then
the output rule for increment emits the XML cruft:

    | #( PLUSPLUS id:identifier }
           { cout << id->getText() << " = 1 + " << id->getText(); }

Delaying it this far also allows a very simply rewrite if assembler is
extended to support increment (and shows you planned ahead :-)

Another option is to have a tree rewrite that converts "++" into
"a=a+1", but you need to make sure and generate a copy of the "a"
node, rather than attempt to reuse the same node twice (kaboom; segv).

I wrote a trivial little tree walker that I call COPY(node) for just
that reason.  It's a handy beastie to have around:

    | #(PLUSPLUS id:identifier }
      {  ## = #( #[ASGN],
                   COPY(#id),
                   #( #[PLUS],
                        #id,
                        COPY(GlobalOneTree) ) ); }


On Tue, 22 Feb 2005 11:55:06 +0100 (CET), Gregor Pardella
<kruemmel99 at yahoo.de> wrote:
> the source C-like language allows code like this:
>      int a=0;
>      a++;
> 
> The problem is now, that the target language (a XML
> assembler like one) has no
> OP-Code for INCREMENT :(


More information about the antlr-interest mailing list