[antlr-interest] Strings as Synthetic Attributes in ANTLR 3.1.2 and ANTLRWorks 1.2.3.

Foolish Ewe foolishewe at hotmail.com
Mon Mar 9 13:45:58 PDT 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090309/ebb9a553/attachment.html 


More information about the antlr-interest mailing list