Hello All:
I'm sorry if this is an obvious question, but I'm wondering about how synthetic
attributes
(synthetic attributes) can be accessed in ANTLR. Does anyone know how to pass
these up the tree or if my approach is wrong? I'm using the most recent version
of ANTLRWorks, 1.2.3 with ANTLR 3.1.2.
Please consider the following brief antlr program. It should take an infix
addition expression
and using the synthetic attributes produce prefix notation representation of it.
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?
Also I'm considering using the rewriting rules (I'm guessing I need the
AST functionality) but I'm looking for a good and current reference any
recommended (I don't own the definitive ANTLR book yet, but if it is still
current and covers this I might get it).
grammar simpleexpr;
options{ output=AST; }
@header {
}
@members{
}
// 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 + ">");
}
;
NUM : ('0'..'9')+;
NEWLINE:'\r'? '\n' ;
WS : (' '|'\t')+ {$channel=HIDDEN;} ;
Regards:
Bill M.
_________________________________________________________________
HotmailĀ® is up to 70% faster. Now good news travels really fast.
http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_HM_70faster_032009
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address