为什么野牛仍使用`int yylex(void)`并且找不到`int yylex(YYSTYPE * yylval_param,YYLTYPE * yylloc_param)`]

问题描述 投票:0回答:1

我正在尝试将位置信息添加到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);
           ^

请帮助我。

c++ c bison flex-lexer lex
1个回答
1
投票

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

要让野牛支持此,您必须使用标志

%pure-parser

在野牛宣言中的某个地方。然后,使用两个参数yylex()lval调用lloc函数。

© www.soinside.com 2019 - 2024. All rights reserved.