[antlr-interest] rewriting tree such that children are at same level
David-Sarah Hopwood
david-sarah at jacaranda.org
Sun Jul 19 14:06:45 PDT 2009
hakan eryargi wrote:
> hello,
>
> below is the main part of my grammar. it's ok but it creates an AST
> from expression
>
> a & b & c -> (& (& a b) c) : an AND node with children c and another
> AND node with children a and b
>
> but i want: (& a b c) : an AND node with tree children
>
> how can i make that with a rewrite rule ?
>
> i've found following post in mail list but didnt helped me much :/
> http://markmail.org/message/ed3ncmfimu3xnczt
>
> program :
> expression EOF!
> ;
>
> expression
> : primary ((AND^ primary)+ | (OR^ primary)+)?
> ;
Change this to (tested):
expression
: primary ( -> primary
| (AND primary)+ -> ^(AND primary+)
| (OR primary)+ -> ^(OR primary+)
)
;
This works because the 'primary+' on the right-hand-side of
each '->' includes all primary nodes matched in the overall
production.
The other replies you got suggesting to use += may also work,
but the solution above is simpler; normally you only need to
use += if you need to refer to the list of nodes in an action.
Jim Idle wrote:
> Not sure WHY you would want that buy it is just:
>
> p+=primary (AND p+=primary)* -> ^(AND $p+)
I assume that the solution still needs to satisfy the requirement
in the recent thread with subject "forcing parenthesis":
<http://www.antlr.org/pipermail/antlr-interest/2009-July/035196.html>
<http://www.antlr.org/pipermail/antlr-interest/2009-July/035248.html>
--
David-Sarah Hopwood ⚥ http://davidsarah.livejournal.com
More information about the antlr-interest
mailing list