I believe the way this is done is to create a subrule for the parenthesized expression and assign that to the variable.
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Kenneth Domino Sent: August 23, 2010 1:59 PM To: [email protected] Subject: [antlr-interest] Tree rewrite for paranthesized expression Hi All, Is there a way to create a variable to reference the nodes for a parenthesized expression, and use it in a tree rewrite rule? E.g., i_abs : i=KI_ABS t=( ( K_S16 | K_S32 | K_S64 ) | ( K_FTZ? K_F32 ) | ( K_F64 ) ) o=( opr_register T_COMMA opr_register_or_constant ) -> ^($i ^(TREE_TYPE $t) ^(TREE_OPR $o)) ; Unfortunately, while Antlr parses and generates a recognizer for the grammar, this does not work because an exception is generated (org.antlr.runtime.tree.RewriteEmptyStreamException). I can rewrite this to bypass the exception and generate a tree, but it feels more like a hack, since it clutters the grammar with additional productions, making it harder to read as well: i_abs : i=KI_ABS t=xxx o=yyy -> ^($i ^(TREE_TYPE $t) ^(TREE_OPR $o)) ; xxx : ( ( K_S16 | K_S32 | K_S64 ) | ( K_FTZ? K_F32 ) | ( K_F64 ) ) ; yyy : ( opr_register T_COMMA opr_register_or_constant ) ; BTW, if there isn't anything like this currently in Antlr, it would be nice to add the syntax in a future release. The syntax for tree construction needs some polishing. The syntax for tree rewrite requires me to duplicate the rule r.h.s. in the rewrite part of the rule. So, I have to change the syntax in two places. My proposal is much cleaner. Antlr also currently forces one to make a choice between tree rewrite rules and AST operators. It would be better if they could be used in combination. For example, if I wanted to construct a tree without the T_COMMA, it could be done using the AST "!" operator after the T_COMMA: i_abs ... o=( opr_register T_COMMA! opr_register_or_constant ) -> ^($i ^(TREE_TYPE $t) ^(TREE_OPR $o)) ; Ken List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
