[antlr-interest] Tree building question

Bart Kiers bkiers at gmail.com
Sun Feb 26 23:34:33 PST 2012


Yes, do something like this:

grammar T;

tokens {
  ROOT;
}

postfix_expression
   :   primary_expression
       (   '[' expression ']' -> ^(ROOT expression)
       |   '(' ')' -> ^(...)
       |   '(' argument_expression_list ')' -> ^(...)
       |   '.' IDENTIFIER -> ^(...)
       |   '->' IDENTIFIER -> ^(...)
       |   '++'
       |   '--'
       )*
   ;

Note that you must use either inline tree operators ^ and !, or use rewrite
rules, not both in 1 rule. You can leave '--' and '++' as they are: no need
to specify they're the root of their own tree.

Also see: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction

Particularly the paragraphs "Rewrite rules" and "Imaginary nodes".

Regards,
Bart.


On Mon, Feb 27, 2012 at 2:55 AM, Leah Perlmutter <lrperlmu at colby.edu> wrote:

> I am using antlr 3.4 and I have the following rule:
>
> postfix_expression
>    :   primary_expression
>        (   '['^ expression ']'!
>        |   '('^ ')'!
>        |   '('^ argument_expression_list ')'!
>        |   '.'^ IDENTIFIER
>        |   '->'^ IDENTIFIER
>        |   '++'^
>        |   '--'^
>        )*
>    ;
>
> It works almost the way I want, except in the first 3 cases of the or I
> want the root to be an imaginary node with a name of my choosing instead of
> the opening punctuation symbol. Is there a good way to do this?
>
> Thanks,
> -Leah
>
> 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