title: Accept both integers and floats in a bison grammar
body: <p>This question is attached to this post <a href="https://stackoverflow.com/questions/42848197/bison-flex-cannot-print-out-result?noredirect=1#comment72805876_42848197">https://stackoverflow.com/questions/42848197/bison-flex-cannot-print-out-result?noredirect=1#comment72805876_42848197</a></p>
  
  <p>this time I try to make my calculator program accepts both integers and floats numbers.</p>
  
  <p>Thank you.</p>
  
  <p>Here is my code</p>
  
  <p>Flex:</p>
  
  <pre><code>%{
      #include &lt;stdio.h&gt;
      #include "f1.tab.h"
  %}
  
  integer [1-9][0-9]*|0
  float [0-9]+\.[0-9]+
  
  %%
  
  {integer}   { yylval.ival = atoi(yytext); return INT; }
  {float}     { yylval.fval = atof(yytext); return FLOAT; }
  .           { return yytext[0]; }
  
  %%
  </code></pre>
  
  <p>Bison :</p>
  
  <pre><code>%{
      #include &lt;stdio.h&gt;
  %}
  
  %union {
      int ival;
      float fval;
  }
  %token &lt;ival&gt; INT
  %token &lt;fval&gt; FLOAT
  
  
  %type &lt;fval&gt; exp
  %type &lt;fval&gt; fac
  %type &lt;fval&gt; f
  
  %%
  input: line
      | input line
  ;
  
  line: exp ';'  { printf("%d\n", $1); };
  
  exp: fac             { $$ = $1;         }
    | exp '+' fac     { $$ = $1 + $3;    }
    | exp '-' fac     { $$ = $1 - $3; }
  ;
  fac: f
      | fac '*' f     { $$ = $1 * $3; }
      | fac '/' f     { $$ = $1 / $3; }
  ;
  f: INT | FLOAT;
  %%
  main(int argc, char **argv) {
      yyparse();
  }
  yyerror(char *s) {
      fprintf(stderr, "error: %s\n", s);
  }
  </code></pre>