While verifying the semantic correctness of procedure and function calls and then during the
intermediate code generation for parameter passing in procedure/function calls, it is necessary
to check the type compatibility of the corresponding formal and actual parameters. Subsequently,
it may be necessary to generate the intermediate code to convert
the type of the actual parameter (i.e. an expression; passed by value) from
integer to real (INT to FLT).
In order to accomplish the above, you may find it useful to use the following grammar modifications.
The modified grammar will allow you to pass the
pointer to the symbol table entry for the procedure/function as an
attribute value, and make it available when the code for expression is
generated. Then you can add a suitable code fragment for the type conversion,
if needed.
Modifications for procedure call:
proc_call:
IDENT
|
proc_call_with_params
;
proc_call_with_params:
name_and_params RPAR
;
name_and_params:
IDENT { verify that $1 is a procedure/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 $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 in the symbol table (the one describing the procedure/function).
typedef struct ProcParamInfo {
Entry *eptr;
int cnt;
} ProcParamInfo;
%union {
...
ProcParamInfo ppi;
Entry *eptr;
...
}
%type <ppi> name_and_params
You need to use the notation $<eptr>$ to explicitly specify the type of the YACC/Bison grammar 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.
A function call (for graduate students) can be handled very similarly.
|