[antlr-interest] Please help a newbie!
Richard Clark
rd_clark at sbcglobal.net
Tue Aug 3 08:13:48 PDT 2004
Hi Peter,
The short answer is that your tree parser has to match what comes out
of your parser. SInce you added "result = " parsing to the parser, you
have to add the same to your tree parser.
try something like this:
class MyTreeParser extends TreeParser;
options {
importVocab=MyParser;
}
script
: (line)*
;
line
{ float r; }
: #( ASSIGN RESULT r=expr)
{ printf("result = %f\n", r); }
;
expr returns [float r=0]
{ float a,b; }
> : #(PLUS a=expr b=expr) {r = a+b;}
> | #(MINUS a=expr b=expr) {r = a-b;}
> | #(MULT a=expr b=expr) {r = a*b;}
> | #(DIV a=expr b=expr) {r = a/b;}
> | n:NUMBER {r = (float)
> atof(n->getText().c_str());
> ;
// --- cut here ---
I would also suggest two changes to your parser:
1) You don't need to keep the EOL tokens in your tree, so you can use
"!" to leave them out.
2) You also may want to elevate that EQ to the head of its subtree to
have your trees all be in a consistent format
#( OP arg1 arg2 )
> line : EOL!
> | RESULT EQ expr EOL! // generates #( RESULT EQ expr )
> ;
or, to rearrange the tree (and I'm collapsing it all to one line):
line
: (RESULT EQ^ expr)? EOL! // generates #( EQ RESULT expr )
;
Hope this helps :)
...Richard
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list