title: Configuring Bison and Flex without global or static variable
body: <p>i am working in a small language/IDE. And I need to know how to configure flex and bison to work together but without using any global or static variable. I need to pass to bison my AST pointer. I also need that bison pass my AST to flex as well. It's a thread environment but i dont need any thread sync. And I need a separete yylineno variable for each yyparse() call. I read about %define api.pure , %parse-param and %option reentrant. But i don't know how to put them to work together... thx in advance...</p>
  
  <p>I tried this:</p>
  
  <p>scanner.l:</p>
  
  <pre><code>%{
  
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include "parser.tab.h"
  #define YY_DECL extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner)
  extern void yyerror(yyscan_t scanner, NBloco * bloco, const char *s);
  
  %}
  
  %option noyywrap
  %option yylineno
  %option reentrant 
  %option bison-bridge
  
  %%
  //...scanner code
  </code></pre>
  
  <p>parser.y:</p>
  
  <pre><code>%{
  #include &lt;iostream&gt;
  #include "AST.h"
  
  #ifndef YY_TYPEDEF_YY_SCANNER_T
  #define YY_TYPEDEF_YY_SCANNER_T
  typedef void* yyscan_t;
  #endif
  
  extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
  extern "C" FILE *yyin;
  extern int yylineno;
  void yyerror(yyscan_t scanner, NBloco * bloco, const char *s);
  %}
  
  %union{ 
  //union code
  }
  
  %define api.pure full
  %lex-param   { yyscan_t scanner }
  %parse-param { yyscan_t scanner }
  %parse-param { NBlock* block}
  
  //tokens...
  //types...
  
  %%
  
  //parser code...
  </code></pre>
  
  <p>And I got this:</p>
  
  <pre><code>parser.y:13:22: warning: 'yylex' initialized and declared 'extern' [enabled by default] extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
  
  
  parser.y:13:22: error: 'YYSTYPE' was not declared in this scope
  
  
  parser.y:13:32: error: 'lvalp' was not declared in this scope extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
  
  
  parser.y:13:48: error: expected primary-expression before 'scanner'
   extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
  
                                              ^
  parser.y:13:55: error: expression list treated as compound expression in initializer [-fpermissive]
   extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
  
                                                     ^
  parser.tab.c: In function 'int yyparse(yyscan_t, NBloco*)':
  parser.tab.c:932:39: error: 'yylex' cannot be used as a function
   # define YYLEX yylex (&amp;yylval, scanner)
  
  
  parser.tab.c:1618:16: note: in expansion of macro 'YYLEX'
         yychar = YYLEX;
  </code></pre>
  
  <p>// more errors...</p>
  
  <p>I also to fix the yylineno to be per file...
  i am basing on <a href="http://benlynn.blogspot.com.br/2013/12/reentrant-parsers-with-flex-and-bison.html" rel="noreferrer">http://benlynn.blogspot.com.br/2013/12/reentrant-parsers-with-flex-and-bison.html</a> and <a href="http://www.lemoda.net/c/reentrant-parser/index.html" rel="noreferrer">http://www.lemoda.net/c/reentrant-parser/index.html</a></p>