[antlr-interest] How to write the rewrite rule for AST if I want to place an imaginary node as its root

Jim Idle jimi at temporal-wave.com
Tue Nov 25 08:17:06 PST 2008


On Tue, 2008-11-25 at 20:28 +0800, chain one wrote:

> The rule is 
> 
> simple_expression:  term  (  add_like_op  term  )* 
> If it doesn't need to place an imaginary as its root, I can simply
> write this rule like that: simple_expression:  term  (  add_like_op^
>  term  )* 
> 
> 
> How about if I wanna put an imaginary node on top of it ?
> 
> 
> simple_expression:  term  (  add_like_op  term  )*  ->
> ^(SIMPLE_EXPRESSION[] ?????????)


It isn't clear if you want SIMPLE_EXPRESSION to be the root node of ALL
the add_like_ops, or a SIMPLE_EXPRESSION for each add_like_op. In the
latter case, there is no need to do this, just let the add_like_op be
your node root. In the former case, it is better to separate this into
two rules, especially if you have precedence available in 'term' (to
avoid multiple SIMPLE_EXPRESSION nodes, unless you need them to put back
the parens in reformatting and so on):



simple_expression: se_term -> ^(SIMPLE_EXPRESSION $se_term) ;

se_term: term  (  add_like_op^  term  )* ;

term : INT
     | FLOAT
     | LPAREN se_term RPAREN
     ;

Jim

> 
> 
> I don't know how to write the ????? part.
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081125/6f853850/attachment.html 


More information about the antlr-interest mailing list