[antlr-interest] AST Tree generation question
Gavin Lambert
antlr at mirality.co.nz
Wed Dec 12 02:59:46 PST 2007
At 21:26 12/12/2007, Roberto Attias wrote:
>In an expression grammar derived from one of the examples I have
>the following rule:
>
>additive_expression
> : (multiplicative_expression) ('+^'
multiplicative_expression |
>'-'^
>multiplicative_expression)*
> ;
>
>When parsing a+b", this generates a tree as expected. Now ,I
want
>to have OP_ADD or OP_SUB as radix of the tree when evaluating
a+b
>or a-b.
The easiest way to do this would be to declare OP_ADD as a token
consisting of '+', and use that in place of the constant
above. (And that ^ should be outside the quotes; I'm assuming
that's a typo.)
If you don't want to do that for some reason, then you can use
rewrites. I'm actually not completely sure of the exact syntax
myself, but I think you can do something like this:
additive_expression
: (a=multiplicative_expression -> $a)
(
(b='+' c=multiplicative_expression
-> ^(OP_ADD[$b] $additive_expression $c))
| (d='-' e=multiplicative_expression
-> ^(OP_SUB[$d] $additive_expression $e))
)*
;
The key points are that a rewrite arrow sets the tree output for
the entire rule, no matter where it appears, and once set you can
refer to the previously-set tree when building another tree. The
other useful bit is the square brackets after the OP_* virtual
tokens; those aren't required, but if you use them it'll associate
the corresponding source token (the operator) with the tree node,
so it'll still have text and a source location.
More information about the antlr-interest
mailing list