Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional arguments to yylex (in bison/flex)?

I am trying to create a reentrant parser using flex and bison. I want to add a parameter to save some state, but I failed to add it to yylex().

Here is the example, it is not expected to compile, just shows the generated code.

foo.l

%option reentrant
%option bison-bridge
%option header-file="foo.tab.h"
%{
#include "foo.tab.h"
%}
%%
"{" { return "{"; }
")" { return '}'; }
%%

foo.y

%define api.pure full
%define parse.error verbose
%parse-param {void *scanner}
%parse-param {int *pint}
%lex-param {void *scanner}
%lex-param {int *pint}
%token '(' ')'
%%
foo : '(' | ')' ;
%%

run with:

bison -d -b foo foo.y
flex foo.l
gcc -E lex.yy.c | less

We can see int yylex (YYSTYPE * yylval_param , yyscan_t yyscanner) {...} So pint is gone. But I think I have specified it at foo.y. So what I need to do more to make yylex accept pint?

Environment: Gentoo Linux stable with Bison-3.0.4 and Flex 2.5.39

like image 614
OstCollector Avatar asked Dec 19 '22 13:12

OstCollector


1 Answers

%lex-param says for bison to invoke yylex with extra parameters, but does not say anything to flex.

The default definition of the yylex() function can be changed by defining the YY_DECL macro in your foo.l file's definition part. In order to have only the int *pint as argument, it looks like this:

#define YY_DECL int yylex(int *pint)

If yylval_param and yyscanner are also needed, then:

#define YY_DECL int yylex(YYSTYPE * yylval_param, yyscan_t yyscanner, int *pint)

like image 117
xsnpdngv Avatar answered Dec 28 '22 23:12

xsnpdngv