title: Include generated code by flex and bison
body: <p>I'm working with Flex and Bison in C++. I am learning to use these tools and the best way to start is by performing a simple calculator. Once generated the application (the executable) from my calc.y and calc.l files, I can run the .exe file and use it, but now I want to include it in a file c ++ to use it in my application but I can't. I think it's my fault because I'm including bad the generated file or generating bad code to import.</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  extern "C" {
      #include "y.tab.h"
  }
  
  int main ( int argc, char *argv[] ) {
      yyparse();
      printf(elementos);
      return 0;
  }
  </code></pre>
  
  <p><strong>calc.l</strong></p>
  
  <pre><code>%{
  #include "y.tab.h"
  #include &lt;stdlib.h&gt;
  void yyerror(char *);
  %}
  
  %%
  
  [0-9]+  {
      yylval = atoi(yytext);
      return INTEGER;
  }
  
  [-+()\n]    {
      return *yytext;
  }
  
  [ \t]   ;
  
  .       {
      yyerror("Invalid character.");
  }
  
  %%
  
  int yywrap(void) {
      return 1;
  }
  </code></pre>
  
  <p><strong>calc.y</strong></p>
  
  <pre><code>%{
      #include &lt;stdio.h&gt;
      int yylex(void);
      void yyerror(char *);
      int sym[26];
      int elementos = 0;
  %}
  
  %token INTEGER VARIABLE
  %left '+' '-'
  %left '*' '/'
  
  %%
  
  program:
          program expr '\n' { printf("%d\n", $2 ); }
      |
  ;
  
  statement:
          expr                { printf("%d\n", $1); }
      |   VARIABLE '=' expr   { sym[$1] = $3; }
  ;
  
  expr:
          INTEGER             { $$ = $1; }
      |   expr '+' expr       { $$ = $1 + $3; elementos = elementos + 1;}
      |   expr '-' expr       { $$ = $1 - $3; }
      |   expr '*' expr       { $$ = $1 * $3; }
      |   expr '/' expr       { $$ = $1 / $3; }
      |   '(' expr ')'        { $$ = $2; }
  ;
  
  %%
  
  void yyerror(char *s) {
      fprintf(stderr, "%s\n", s);
  }
  
  
  int main(void) {
      yyparse();
      return 0;
  }
  </code></pre>
  
  <p>The y.tab.h is generated by bison. When I'm trying to compile main.cpp I'm getting an error:</p>
  
  <p>command: <code>gcc main.cpp -o main.exe</code></p>
  
  <p>result: <code>main.cpp: In function 'int main(int, char**)':
  main.cpp:8:10: error: 'yyparse' was not declared in this scope
  main.cpp:9:9: error: 'elementos' was not declared in this scope</code></p>
  
  <p>How can I fix it?</p>
  
  <p><em>I'm using gcc version 4.7.2, bison 2.4.1 and 2.5.4 on windows 8.1.</em></p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>The y.tab.h file is:</p>
  
  <pre><code>/* Tokens.  */
  #ifndef YYTOKENTYPE
  # define YYTOKENTYPE
     /* Put the tokens into the symbol table, so that GDB and other debuggers
        know about them.  */
     enum yytokentype {
       INTEGER = 258,
       VARIABLE = 259
     };
  #endif
  /* Tokens.  */
  #define INTEGER 258
  #define VARIABLE 259
  
  
  
  
  #if ! defined YYSTYPE &amp;&amp; ! defined YYSTYPE_IS_DECLARED
  typedef int YYSTYPE;
  # define YYSTYPE_IS_TRIVIAL 1
  # define yystype YYSTYPE /* obsolescent; will be withdrawn */
  # define YYSTYPE_IS_DECLARED 1
  #endif
  
  extern YYSTYPE yylval;
  </code></pre>
  
  <p>There's not a "elementos" variable, but looking in the generated <strong>y.tab.c</strong> file, I found that there is defined!</p>