Flex/Bison 'yylex' : 函数不接受 3 个参数

Flex/Bison 'yylex' : function does not take 3 arguments

我有以下 Lexer.l 和 Parser.y 个文件。

Lexer.l

%{
#include "Parser.h"
%}

%option yylineno
%option outfile="Lexer.cpp" header-file="Lexer.h"
%option warn nodefault
%option reentrant noyywrap never-interactive nounistd
%option bison-bridge

Parser.y

%{
#include "Parser.h"
#include "Lexer.h"

extern int yyerror(yyscan_t scanner, const char *msg) 
{printf("\r\nError: %s", msg); return 1;}

%}
 
%code requires {     
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
}
 
%output  "Parser.cpp"
%defines "Parser.h"     
%define api.pure
%pure-parser
%lex-param   { yyscan_t scanner }
%parse-param {yyscan_t scanner }
 

一切正常。

现在我正在尝试获取令牌的列和行;当我使用 @1.first_line 时,出现以下错误:

'yylex' : function does not take 3 arguments

'yyerror' : function does not take 3 arguments

对于 yyerror,我查看了它的编译器要求并实现了它。

但是,对于 yylex,我不知道 return 是什么。 我试图查看带有 2 个参数的 yylex 实现来制作类似的东西,但它似乎根本没有 yylex 的实现。

有什么想法吗?

如果您使用 option bison-bridge 并且您的解析器有 @ 个引用,您需要添加

%option bison-locations

到你的 flex 文件。 (您可以使用它代替 bison-bridge,但我认为同时使用两者更整洁。)来自 flex manual:

  • --bison-locations, %option bison-locations
    • 指示 flex 使用 GNU bison %locations。这个 意味着 yylex 将传递一个附加参数 yylloc。 此选项意味着 %option bison-bridge.