[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 06:18:29 -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 ")"
>
> In this variation, LP and RP are macro names that he lexer d
> should return, and "(" and ")" are just symbols used in the grammar code to
> make it readable, and in parser error messages.
>
> Hans
>
>
>
Thanks! I must have paged through the section of the manual on recursive
rules without paying close enough attention. This is perfect.
Jason