[antlr-interest] Strings as Synthetic Attributes in ANTLR 3.1.2 and ANTLRWorks 1.2.3.
Gavin Lambert
antlr at mirality.co.nz
Tue Mar 10 01:00:21 PDT 2009
At 09:45 10/03/2009, Foolish Ewe wrote:
>When I run it I get the output:
>op=<+> (Hardwired!), lhs=<2>, rhs=<3>, result_attr=<+23>
>e.text = <2 + 3>
>
>Note that e.text shows the infix order from the inputs, so I'm
>guessing the synthetic attribute isn't being successfully
>transmitted to stat from expr, have I goofed or is this a bug in
>ANTLR?
[...]
>// Here we should see the prefix version of the synthetic
>attribute of expr
>stat: e=expr NEWLINE {System.out.println("e.text = <" + $e.text
>+ ">");};
>
>// Creates a synthetic string attribute (bottom up).
>// given an infix order sum operation the attribute string
>reflects a prefix order expression
>expr returns [String result_attr]: lhs=NUM '+' rhs=NUM
> { $result_attr = '+' + $lhs.text + $rhs.text;
> System.out.println("op=<+> (Hardwired!), lhs=<" +
> $lhs.text +
> ">, rhs=<" + $rhs.text + ">, result_attr=<" +
> $result_attr + ">");
> }
> ;
You are assigning your custom text to a return value called
"result_attr". This will, naturally enough, appear in the parent
rule as $e.result_attr, not as $e.text.
More information about the antlr-interest
mailing list