Search code examples
c++cbisonflex-lexerlex

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


I'm trying to add location information into flex and bison. But my bison still using int yylex(void) and cannot find int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param).

Here's my lex file 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');  \
    }

%}

%%
...
%%

...

Here's my bison file 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;
}

Here's my shell command for generating C++ code:

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

Here's my error message:

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);
           ^

Please help me.


Solution

  • You seem to need a reentrant parser.

    To have bison support this, you have to use the flag

    %pure-parser
    

    somewhere in the bison declarations. Then the yylex() function is called with the two arguments lval and lloc.