title: Assigning multiple types to non-terminals in bison
body: <p>I am trying to assign multiple data types to the non-terminal number and on googling I found this : <a href="https://stackoverflow.com/questions/4905819/assigning-multiple-data-types-to-a-non-terminal-in-yacc">Assigning multiple data types to a non-terminal in yacc</a> .
  However this syntax does not seem to be working for me as on running it I get the following error : </p>
  
  <pre><code>new11.y:53.54-55: $3 of `initialization' has no declared type
  new11.y:57.81-82: $5 of `increment' has no declared type
  new11.y:70.33-34: $4 of `ifelse' has no declared type
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>  %union{
      int ival;
      float fval;
      char *sval;
  }
  
  // define the terminal symbol token types
  %token &lt;ival&gt; INTEGER
  %token &lt;fval&gt; FLOAT
  %token &lt;sval&gt; BLOCK
  %token &lt;sval&gt; GOTO
  %token PRINT IF ELSE RETURN
  %token LESSTHAN LESSTHANEQUALTO GREATERTHAN GREATERTHANEQUALTO EQUALTO
  %token ADD SUB MUL DIV
  %token &lt;sval&gt; IDENTIFIER 
  %type &lt;sval&gt; comparison
  
  %%
  //grammar which bison will parse
  
  start:
      block statements { ; }
      | start block statements { ; }
      ;
  
  block:
      BLOCK { cout&lt;&lt;"Block : "&lt;&lt;$1&lt;&lt;endl; }
      ;
  
  number:
      INTEGER { $&lt;ival&gt;$=$1 }
      | FLOAT { $&lt;fval&gt;$=$1 }
      ;
  
  initialization:
      IDENTIFIER EQUALTO number { cout&lt;&lt;$1&lt;&lt;" = "&lt;&lt;$3&lt;&lt;endl; }
      ;
  
  increment:
      IDENTIFIER EQUALTO IDENTIFIER ADD number { cout &lt;&lt;$1&lt;&lt;" = "&lt;&lt;$3&lt;&lt;" + "&lt;&lt;$5&lt;&lt;endl; }
      ;
  
  goto:
      GOTO { cout&lt;&lt;"GOTO : "&lt;&lt;$1&lt;&lt;endl; }
      ;
  
  printing:
      PRINT { cout&lt;&lt;"printf(...)"&lt;&lt;endl; }
      ;
  
  ifelse:
      IF IDENTIFIER comparison number GOTO ELSE GOTO
      { cout &lt;&lt;"if "&lt;&lt;$2&lt;&lt;$3&lt;&lt;$4&lt;&lt;", "&lt;&lt;$5&lt;&lt;",else, "&lt;&lt;$7&lt;&lt;endl; }
      ;
  
  comparison:
      LESSTHAN { $$="&lt;"; }
      | LESSTHANEQUALTO { $$="&lt;=" }
      | GREATERTHAN { $$="&gt;" }
      | GREATERTHANEQUALTO { $$="&gt;=" }
      ;
  
  statement:
      initialization
      | printing
      | goto
      | increment
      | ifelse
      | RETURN
      ;
  
  statements:
      statements statement 
      | statement
      ;
  
  %%
  
  main() {
      // open a file handle to a particular file:
      FILE *myfile = fopen("test.cfg", "r");
      // make sure it is valid:
      if (!myfile) {
          cout &lt;&lt; "I can't open a.snazzle.file!" &lt;&lt; endl;
          return -1;
      }
      // set flex to read from it instead of defaulting to STDIN:
      yyin = myfile;
      // parse through the input until there is no more:
      do {
          yyparse();
      } while (!feof(yyin));
  }
  
  void yyerror(const char *s) {
      cout &lt;&lt; "EEK, parse error!  Message: " &lt;&lt; s &lt;&lt; endl;
      // might as well halt now:
      exit(-1);
  }
  </code></pre>
  
  <p>Where am I going wrong here? Did I not use the syntax right?</p>