> Le 18 févr. 2014 à 19:17, Johannes Dewender <savan...@jonnyjd.net> a écrit : > > Hello,
Hi Johannes, > I am building a reentrant parser with bison 3.0.2 and flex 2.5.37 > (also using name-prefixes, if that makes a difference). > I need to hand yylex a scanner (type yyscan_t) and yyparse some user-defined > context (which includes the scanner). > > So I don't want to give yyparse the scanner directly as a parameter but as > ctxt->yyscanner (which is of type yyscan_t). > Giving the scanner as an additional parameter would be redundant. > > What I would like to do: > > %name-prefix "dot_" > %define api.pure full > %lex-param {yyscan_t ctxt->yyscanner} > %parse-param {context_struct *ctxt} > ... > %code requires { > ... > struct context_struct { > yyscan_t yyscanner; > ... > }; > ... > dot_parse(&parser_context); > > However bison doesn't use "ctxt->yyscanner" and falls back to calling > yylex (&yylval, yyscanner) > and complains > error: 'yyscanner' was not declared in this scope > yychar = yylex (&yylval, yyscanner); > > So what I do is: > > %define api.pure full > %lex-param {yyscan_t ctxt_yyscanner} /* note _ instead of -> */ > %parse-param {context_struct *ctxt} > > %{ > ... > #define ctxt_yyscanner ctxt->yyscanner > > > However, this is probably a bit "hacky" compared to how it used to work with > #define YYLEX_PARAM ctxt->yyscanner > (without additional %lex-param) > > > Another option I have found is using an intermediate lex function accepting a > context and calling the "real" yylex with ctxt->yyscanner: > http://cgit.freedesktop.org/mesa/mesa/commit/?id=6d2a9220b832d9a0c0cf35fcc5b9de1542af267d > > Though that doesn't sound much better as an option. > > Is there a reason why %lex-param only accepts identifiers and no expressions? Yes, the generated signature of the lex function. Actually, that's because we use the same code for yylex and yyparse, and we really need to get the name of the variable. Would it be bad for you to pass the whole ctx to the scanner? That's what I usually do. In that case %param allows to factor %lex-param and %parse-param.