[antlr-interest] simple tree transformation
mzukowski at yci.com
mzukowski at yci.com
Wed May 8 08:47:02 PDT 2002
I made an error in this example. There is always the possibility of an if
without an else. So this:
> | #(! ifst:"if"
> e:expr s1:statement
> ( el:"else" s2:statement )?
> ) {##=#(ifst, e, s2, el, s1);}
Should have the action in the else closure:
| #(! ifst:"if"
e:expr s1:statement
( el:"else" s2:statement {##=#(ifst, e, s2, el, s1);})?
)
Monty
www.codetransform.com
> -----Original Message-----
> From: mzukowski at yci.com [mailto:mzukowski at yci.com]
> Sent: Tuesday, May 07, 2002 8:13 AM
> To: antlr-interest at yahoogroups.com
> Subject: [antlr-interest] simple tree transformation
>
>
> > From: Sebastian Danicic [mailto:mas01sd at gold.ac.uk]
> > Would it be possible for you to show me a little example of a simple
> > program transformation?
> >
> > A good one would be something that converts:
> >
> > if (B) s1 else s2 => if (B) s2 else s1
> >
> > or something equally simple. Just so I understand how to
> > manipulate the AST.
>
> I would make an antlr "grammar subclass" of GnuCTreeGrammar
> with just this
> method in it, with the "if" section modified as below. The
> subclass must be
> marked with the "buildAST=true" option.
>
> The entire statementBody method must be here, you can't just
> subclass an
> alternative. Of course if you had a need to, you could refactor
> GnuCTreeGrammar into one more suited to your needs. For instance
> statementBody could reference a few rules, one for plain
> statements, one for
> iterations, one for jumps, one for labeled statments, one for
> selection
> statements. So if you were doing lots of different passes
> that just looked
> at iteration statements you would only need to subclass the
> iterationStatement rule.
>
> For the "if" alternative I turned off the automatic tree
> building since I
> want to construct the tree in a different order. If I were
> simply modifying
> the nodes in place (changing their text) then I would let
> antlr build the
> tree. Once a tree is built, however, it is hard to swap
> those nodes around
> because nodes contain within them links to their firstChild
> and nextSibling.
> A common mistake is to let antlr build the tree and then swap
> two nodes
> accidently creating a circular structure.
>
> Then I labeled all the elements I want for my tree. Note
> that antlr does
> not have any positional notation for nodes--you can't say #1, #2, etc.
> Finally I construct the tree as I want it, in this case with
> the statements
> switched.
>
> statementBody
> : SEMI // Empty statements
>
> | compoundStatement // Group of statements
>
> | #(NStatementExpr expr) //
> Expressions
>
> // Iteration statements:
>
> | #( "while" expr statement )
> | #( "do" statement expr )
> | #( "for"
> expr expr expr
> statement
> )
>
>
> // Jump statements:
>
> | #( "goto" expr )
> | "continue"
> | "break"
> | #( "return" ( expr )? )
>
>
> // Labeled statements:
> | #( NLabel ID (statement)? )
> | #( "case" expr (statement)? )
> | #( "default" (statement)? )
>
>
>
> // Selection statements:
>
> | #(! ifst:"if"
> e:expr s1:statement
> ( el:"else" s2:statement )?
> ) {##=#(ifst, e, s2, el, s1);}
> | #( "switch" expr statement )
>
>
>
> ;
>
> Monty
> www.codetransform.com
>
> > -----Original Message-----
> > From: Sebastian Danicic [mailto:mas01sd at gold.ac.uk]
> > Sent: Tuesday, May 07, 2002 1:53 AM
> > To: mzukowski at bco.com
> > Subject: cgram
> >
> >
> > Dear Monty,
> >
> > I'm a member of the VASTT (http://www.brunel.ac.uk/~csstmmh2/vast/).
> > We would like to write a slicer and other program
> > transformations for C
> > using your system.
> >
> > Would it be possible for you to show me a little example of a simple
> > program transformation.
> >
> > A good one would be something that converts:
> >
> > if (B) s1 else s2 => if (B) s2 else s1
> >
> > or something equally simple. Just so I understand how to
> > manipulate the
> > AST.
> >
> > Thank you very much in advance for your help.
> >
> > Sebastian Danicic (http://www.mcs.gold.ac.uk/~mas01sd/)
> >
> >
>
>
>
> Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list