[antlr-interest] Tree rewrite for paranthesized expression
Kenneth Domino
kenneth.domino at domemtech.com
Mon Aug 23 10:58:43 PDT 2010
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
More information about the antlr-interest
mailing list