[antlr-interest] How to duplicate nodes using a subrule when doing AST rewriting?

Eric researcher0x00 at gmail.com
Tue Dec 20 08:19:53 PST 2011


Hi Stefan,

My suggestion would be to avoid doing tree rewrites in the grammar files
and instead drop down to the API level and call CommonTree and
ITreeAdaptor methods directly.

For the last two days I have been doing major changes to an AST that can't
be done, or I can't figure out, with the the tree rewrite rules in a
grammar.

Most of the API methods for tree manipulation are in ITreeAdaptor, so you
will have create an adaptor to call the methods. i.e. ITreeAdaptor adaptor
= new CommonTreeAdaptor();

Since I am doing all of mine in C#, I can't give you working Java examples
here.

For your duplicate of a node problem I would look at the
ITreeAdaptor.DupTree method.

To replace nodes in a tree I would look at ITreeAdaptor.ReplaceChildren.

Also, I personally find it easier to set the book aside and just look at
the trees like any standard tree with parents and multiple children. Don't
try and think in rewrite rules and recreate the process using the methods,
it is just confusing. And I also find that cutting and pasting rewrite code
from the parser gets me into more problems than it's worth.

Hope this helps,  Eric



On Tue, Dec 20, 2011 at 10:47 AM, Stefan Mätje <
Stefan.Maetje at esd-electronics.com> wrote:

> Hi,
>
> I would like to duplicate AST nodes as it is described in chapter 7 of the
> ANTLR
> reference book.
>
> The following simplified rules are part of my grammar. The "oneIdOrList"
> rule
> collects one or multiple identifiers into one list. At some points in my
> grammar
> I want to generate/duplicate a subtree for each identifier found. But
> using the
> "oneIdOrList" rule does not work (see NOT WORKING variant of
> "structComponent"
> below).
>
> To get the "structComponent" rule working I have to write the
> "( ID | '(' ID (',' ID )* ')' )" directly into the "structComponent" rule.
>
> Is there a way to use (and or change) the "oneIdOrList" rule in the
> "structComponent" rule and still get the wanted node and tree duplication?
>
> Or to I have to replace the "oneIdOrList" rule anywhere in my grammar with
> the
> direct code to get the duplication working.
>
> Anybody there to give me a hint to solve this problem?
>
> Thanks,
>        Stefan
>
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
> /** "OneIdentifierOrList"
> */
> oneIdOrList:
>        ( ID | '(' ID (',' ID )* ')' )  -> ID+
>        ;
>
> /** "TypeStructure"
>        */
> typeStruct
>        :       'STRUCT'^ LBRACK! structComponent ( ','! structComponent )*
> RBRACK!
>        ;
>
> /** "StructureComponent"
>        */
> // WORKING variant
> structComponent
>        :       ( ID | '(' ID (',' ID )* ')' ) oneIdOrList simpleType
>                -> ^(FIELD_DCL ID simpleType)+
>        ;
> // NOT WORKING variant
> structComponent
>        :       oneIdOrList simpleType
>                -> ^(FIELD_DCL oneIdOrList simpleType)+
>        ;
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
>
> Example input
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
> MODULE ( structsmpl );
>
> PROBLEM;
>     DCL toast   STRUCT [
>         (zing, zang, zong)  FIXED,
>         (ring, rang)  CLOCK
>     ] GLOBAL;
> MODEND;
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
>
> Pretty printed AST result of NOT WORKING variant using the "oneIdOrList"
> rule:
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
> (MODULE structsmpl
>     (PROBLEM
>         (VAR_DCL
>             (STRUCT
>                 (FIELD_DCL zing zang zong FIXED 15)
>                 (FIELD_DCL ring rang CLOCK)
>             )
>             (MOD_LIST GLOBAL)
>             toast
>         )
>     )
> )
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
>
> Pretty printed AST result of the WORKING variant using "ID" directly in the
> "structComponent" rule:
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
> (MODULE structsmpl
>     (PROBLEM
>         (VAR_DCL
>             (STRUCT
>                 (FIELD_DCL zing FIXED 15)
>                 (FIELD_DCL zang FIXED 15)
>                 (FIELD_DCL zong FIXED 15)
>                 (FIELD_DCL ring CLOCK)
>                 (FIELD_DCL rang CLOCK)
>             )
>             (MOD_LIST GLOBAL)
>             toast
>         )
>     )
> )
>
> -------------8<-------------8<-------------8<-------------8<-------------8<----
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list