0

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?

1
  • 'Evoke' and 'invoke' don't mean the same thing. It would be better to return yytext[0]; to the parser and let it deal with the bad character as a syntax error.
    – user207421
    Apr 28, 2021 at 0:13

1 Answer 1

1

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.

1
  • Thanks again rici!
    – Tom
    Apr 27, 2021 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.