Search code examples
bisonyacclex

How to invoke yyerror() from flex?


I have yyerror() defined in my Bison file:

parser.y

...
%code {
  void yyerror(YYLTYPE* yyllocp, yyscan_t unused, const char** errorReturn, const char* msg);
}
...

void yyerror(YYLTYPE* yyllocp, yyscan_t unused, const char** errorReturn, const char* msg) {
    ...
}

And I'd like to invoke it in my flex file with a custom message if possible:

flex.l

%option reentrant bison-bridge bison-locations   
...


"]"                     return TOKEN(TCLOSEINDEX);
.                       {yyerror("Unknown token");}

%%

How can I achieve this?


Solution

  • If you put the declaration of yyerror in a %code provides block instead of a default %code block, it will be copied into the generated header file which will make the declaration available to your scanner implementation. (You need provides rather than requires because the declaration depends on the declaration of YYLTYPE.)

    Of course, you will need to call it with all of its required arguments.