[antlr-interest] Re: Another newbie question

lgcraymer lgc at mail1.jpl.nasa.gov
Fri Dec 3 15:29:28 PST 2004



--- In antlr-interest at yahoogroups.com, "derek_kusiak"
<derek_kusiak at y...> wrote:
[SNIP]
>   command
>   : INTFUNC i:INTEGER // So far so good
>     {
>       cout << "result = "
>            << intfunc(atoi(i->getText().c_str()))
>            << endl;
>     }
>   |
>     REALFUNC f:(FLOAT | INTEGER) // Not allowed
>     {
>       cout << "result = "
>            << realfunc(atof(f->getText().c_str()))
>            << endl;
>     }
>   ;

ANTLR doesn't do labeled subrules; what you need instead is something like
command
{ Token f = null; }
   : INTFUNC i:INTEGER // So far so good
     {
       cout << "result = "
            << intfunc(atoi(i->getText().c_str()))
            << endl;
     }
   |
     REALFUNC (e:FLOAT { f = e; } | i:INTEGER { f = i} )
     {
       cout << "result = "
            << realfunc(atof(f->getText().c_str()))
            << endl;
     }
   ;

or have an action for each of the subrule alternatives (instead of the
single action for both).

> How do I write a rule for realfunc() that accepts a FLOAT
> or INTEGER and extracts the value?  In my real program
> realfunc() takes several parameters, so I can't just
> write explicit rules for every permutation.  Thanks.

If the parameters are really varied, you may want to add a tree
construction phase to more easily sort out the argument alternatives
for computation.

--Loring





 
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