[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extending Functions from Manual Example
From: |
Jason Melbye |
Subject: |
Re: Extending Functions from Manual Example |
Date: |
Wed, 10 Sep 2008 22:17:35 -0500 |
On Wed, Sep 10, 2008 at 3:59 AM, Hans Aberg <address@hidden> wrote:
> On 10 Sep 2008, at 05:39, Jason Melbye wrote:
>
>> I have been reading through the Bison manual the past couple days. I
>> really
>> liked the three examples, especially the last - the multifunction
>> calculator. Since reading that I have been wondering if there was a
>> general
>> way to describe a function that can take on any number of arguments in the
>> grammer rules?
>>
>> I suppose that I could just do something like,
>>
>> | FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
>> | FNCT '(' exp ',' exp ')' { $$ = (*($1->value.fnctptr))($3,$5); }
>> | FNCT '(' exp ',' exp ',' exp ')' ...
>>
>>
>> I could go on like that to all for as many arguments as I wanted to /
>> needed
>> to. However, I would also like to create a "plug-in" system where users
>> can
>> create their own functions and add them to the application. Thus a
>> avoiding
>> a limit on arguments would be desirable.
>>
>
> You need to create recursive rules. If lists are non-empty, write:
>
> expr_list:
> expr {
> ...
> }
> | expr_list "," expr {
> ...
> }
> ;
>
> If empty lists are admitted, simply delete the first "expr" in the above.
>
> Then use it:
> FNCT '(' expr ')'
>
> Some prefer not using the '...' construct. So one can write:
>
> %token LP "("
> %token RP ")"
>
> FNCT "(" expr ")"
>
Should those FNCT '(' expr ')' / FNCT "(" expr ")" lines say FNCT '('
expr_list ')' (or "(", ")" ) instead? I'm still struggling to construct the
action that calls the function as well, passing all the arguments. So say I
had this:
expr: NUM
| expr '+' expr {$$ = $1 + $2; }
... other basic math ...
| FNCT '(' expr_list ')' { what goes here? }
and expr_list is as you suggest above.
I'm not sure how to actually call the function, because I'm passing it a
variable amount of parameters - as many as make up expr_list.
I appreciate the help,
Jason