为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS

Why bison still using `int yylex(void)` and cannot find `int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param)`?

本文关键字:quot yylex int 找不到 YYS void 为什么      更新时间:2023-10-16

我正在尝试将位置信息添加到flex和bison中。但我的野牛仍在使用int yylex(void),找不到int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param)

这是我的lex文件Token.l:

%option noyywrap nodefault yylineno 8bit
%option bison-locations bison-bridge
%{
#include ...
#include "Parser.tab.hpp"
#define T_SAVE_TOKEN            yylval->literal = strndup(yytext, yyleng)
#define T_SAVE_TOKEN_X(p, q)    yylval->literal = strndup(yytext+(p), yyleng-(p)-(q))
#define T_SAVE_NO_TOKEN         yylval->literal = nullptr
#define T_TOKEN(t)              (yylval->token = t)
#define YY_USER_ACTION                                             
yylloc->first_line = yylloc->last_line;                             
yylloc->first_column = yylloc->last_column;                         
if (yylloc->last_line == yylineno) {                                
yylloc->last_column += yyleng;                                  
} else {                                                            
yylloc->last_line = yylineno;                                   
yylloc->last_column = yytext + yyleng - strrchr(yytext, 'n');  
}
%}
%%
...
%%
...

这是我的野牛文件Parser.y:

%locations
%code top {
#include ...
}
%code {
extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param);
}
/* different ways to access data */
%union {
char *literal;
int token;
}

这是我生成C++代码的shell命令:

flex -o Token.yy.cpp Token.l
bison -d -o Parser.tab.cpp --defines=Parser.tab.hpp Parser.y

这是我的错误信息:

Parser.tab.cpp:1674:16: error: no matching function for call to 'yylex'
yychar = yylex ();
^~~~~
/Users/rolin/workspace/github/coli/src/./Token.h:16:12: note: candidate function not viable: requires 2 arguments, but 0 were provided
extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param);
^

请帮帮我。

您似乎需要一个可重入的解析器。

为了让野牛支持这一点,你必须使用标志

%pure-parser

野牛宣言中的某个地方。然后用两个参数lvallloc调用yylex()函数。