If you want to stick with the standard (POSIX) lex / yacc, then your only option is to switch to global variables / functions. If you are fine using the Bison and Flex extensions, there are several ways to pass variables, mostly related to adding extra parameters to yyparse () and yylex ().
In Bison, this is done through% lex-param and% parse-param.
%parse-param { struct somestruct *mystruct } %lex-param { struct somestruct *mystruct }
There are two different mechanisms in Flex, depending on whether you need a reenter lexer or not. Assuming you are using the default option (no reuse), you need to override YY_DECL:
%{ #define YY_DECL int yylex(struct somestruct *mystruct) %}
In reentrant Flex lexer, additional arguments can be added through the scanner structure that Flex supports in order to maintain its state. You want to define YY_EXTRA_TYPE; additional data can be obtained through yyget / set_extra ().
source share