|
Hints on processing function callsYou may find it useful to introduce the following grammar modifications. The modified grammar will allow you to pass the pointer to the symbol table entry for the function as an attribute value, and make it available for the semantic verification of function calls and later, when the code for the parameter expressions is generated. The modification for the function call:
function_call:
IDENT LPAR RPAR
{ verify that $1 is a function name and that it takes no parameters }
|
func_call_with_params
;
func_call_with_params:
name_and_params RPAR
{ verify that the function was called with a correct number of parameters, using
$1.cnt and $1.eptr }
;
name_and_params:
IDENT { verify that $1 is a function name
$<eptr>$ = lookup( $1 ); }
LPAR
expression { verify the first parameter: check type compatibility using $2.eptr and $4;
if ( $4.type requires conversion -- use
$2 to get proc. symtab record )
gen_code_for_conversion();
$$.eptr = $<eptr>2; $$.cnt = 1; }
|
name_and_params COMMA expression
{ verify parameter number $1.cnt + 1: check type compatibility using $1.eptr and $3;
if ( $3.type requires conversion -- use
$1 to get proc. symtab record )
gen_code_for_conversion();
$$.eptr = $1.eptr; $$.cnt = $1.cnt + 1; }
;
In the above code, the name eptr should be one of the data members defined in the %union and should be of a type suitable to hold a reference to an entry to the symbol table (the one describing the function).
typedef struct FuncParamInfo {
Entry *eptr;
int cnt;
} FuncParamInfo;
%union {
...
FuncParamInfo fpi;
Entry *eptr;
...
}
%type <fpi> name_and_params
You need to use the notation $<eptr>$ to explicitly specify the type of the YACC/Bison variable, since you don't know its name (YACC/Bison generates the name automatically and you don't know it in advance). These actions are called mid-rule actions. Please note, that as you are generating code for passing parameters in a function call, it may be necessary to generate the code to convert the type of the actual parameter (expression; passed by value) from INT to FLOAT. |