[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extending Functions from Manual Example
From: |
Hans Aberg |
Subject: |
Re: Extending Functions from Manual Example |
Date: |
Thu, 11 Sep 2008 13:44:08 +0200 |
On 11 Sep 2008, at 05:17, Jason Melbye wrote:
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.
It depends on what you return from the lexer (typically generated
using Flex). If the lexer says
return '(';
use that in the grammar. If it says
return LP;
use LP or (if defined using %token as 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.
You need to create a data type that can hold the expr list. Then
write code to compute the function application. Such data structures
are called "closures", that is, they represent code that should be
computed later. This is necessary for doing loops, too.
So the action code should look something like in pseudocode:
expr_list:
expr {
$$ = list($1);
}
| expr_list "," expr {
$$ = append($1, $3);
}
;
Hans