title: How to use declared c++ variable inside bison grammar
body: <p>I am trying to keep an error count in a variable called 'mismatches', in which i declare in the first part of the bison file.</p>
  
  <p>In my bison grammar, i set a value to that variable.</p>
  
  <p>Then in the 3rd part of the bison file, in the main() function i cout its value, and it's 0.</p>
  
  <p>A very modified/cut down version of my bison file:</p>
  
  <pre><code>%{
  extern "C" FILE *yyin;
  extern int yylineno;
  extern int yynerrs;
  
  int yylex();
  
  // Declare 'mismatches'
  int mismatches;
  
  %}
  
  %error-verbose
  
  
  %%
  
  expression:
            expression ADDOP term
            {
                       cout &lt;&lt; "Parser is now here. Going to set `mismatches` to 6";
                       mismatches = 6;
            }
            | term
            ;
  
  %%
  
  int  main()
  {         
            // Outputs 0
            cout &lt;&lt; mismatches;
  
            yyparse();
  
            return 1;
  
  }
  </code></pre>
  
  <p>What should i do so that the variable 'mismatches' can be used in all parts of the bison file?</p>