Bison derivation order
Hi I'm using bison for a school project, but I'm having an problem i got the next rules:
callsubrotina : callfunction | callprocedure;
callprocedure:
T_IDENTIFICADOR
{identifier_check( GOTO, $1, TIPO_PROCEDIMENTO );}
| T_IDENTIFICADOR T_PARENTESE_E listaexpressoes T_PARENTESE_D
{identifier_check( GOTO, $1, TIPO_PROCEDIMENTO );}
;
callfunction:
T_IDENTIFICADOR
{identifier_check( LD_VAR, $1, TIPO_FUNCAO );}
| T_IDENTIFICADOR T_PARENTESE_E lis开发者_运维百科taexpressoes T_PARENTESE_D
{identifier_check( LD_VAR, $1, TIPO_FUNCAO );} ;
The problem is bison always goes to "callprocedure" even if this trigger an error. How can I force it to check both the rules? Or how I can create a rule to treat properly?
PS: Sorry for the bad English folks.
Since the syntax of the a 'callprocedure' and a 'callfunction' is identical in the rules shown, there is no way for Bison (or any other analogous tool) to distinguish between them.
You must have a conceptual difference - maybe a procedure returns no value and a function does return a value. But unless there is some syntactic way to identify that a particular call belongs to a function and not to a procedure, you will always get the first written rule executed. You should also be getting a warning about an unused rule.
One possibility: if your functions and procedures must be declared before they can be called, so you can tell whether a given name belongs to a function or procedure, then you could modify the lexical analyzer to say 'this is a FUNCTION NAME' and 'that is a PROCEDURE NAME', and then the grammar could be:
callsobrotina: callprocedure | callfunction ;
callprocedure:
T_PROCEDURE_NAME
| T_PROCEDURE_NAME T_PARETESE_E listaexpressoes T_PARENTES_D
;
callfunction:
T_FUNCTION_NAME
| T_FUNCTION_NAME T_PARETESE_E listaexpressoes T_PARENTES_D
;
精彩评论