title: Class as Return Type in Bison: Where to define the class?
body: <p>I've found three tutorials that cover writing a parser in C++ using Bison: <a href="http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html" rel="nofollow">here</a>, <a href="http://aquamentus.com/flex_bison.html" rel="nofollow">here</a>, and <a href="http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/" rel="nofollow">here</a>. The first two don't cover how to use a class as the return type of a rule. The third does cover it, but it doesn't appear to clearly explain the stipulations.</p>
  
  <p>Here is a simple parser.yy file which uses a class in this manner.</p>
  
  <pre><code>%{
  
  #include &lt;stdio.h&gt;
  
  extern FILE * yyin; 
  int yyerror(char *s) 
  {
      fflush(stdout);
      printf("error\n");
  }
  
  int yywrap(void) {
      return 1;
  }
  
  int yyparse() { return 1; }
  
  int main() { 
      yyparse(); 
      return 1;  
  }
  
  class myclass { int x; };
  
  %}
  
  %union {
      int token;
      myclass * mc; 
  }
  
  %token &lt;token&gt; REGISTER  
  %type &lt;mc&gt; start_sym
  
  %start start_sym
  
  %%
  
  start_sym :
       REGISTER '+' { $$ = new myclass(); }
      ;
  </code></pre>
  
  <p>Bison runs with no problems using this input. I defined a simple input for flex to use with this. However, when I try to compile it, I get the error:</p>
  
  <pre><code>$ g++ parser.tab.cc lex.yy.cc
  
  In file included from lex.ll:11:0:
  parser.yy:31:5: error: ‘myclass’ does not name a type
       myclass * mc;
       ^
  </code></pre>
  
  <p>Where is the appropriate place to declare myclass?</p>