如何从 flex 调用 yyerror()?

问题描述

我在 Bison 文件中定义了 yyerror()

解析器.y

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

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

如果可能的话,我想在我的 flex 文件中使用自定义消息调用它:

flex.l

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


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

%%

我怎样才能做到这一点?

解决方法

如果您将 yyerror 的声明放在 %code provides 块而不是默认的 %code 块中,它将被复制到生成的头文件中,这将使其定义可用于您的扫描仪实现。 (您需要 provides 而不是 requires,因为声明依赖于 YYLTYPE 的声明。)

当然,您需要使用其所有必需的参数来调用它。