title: Bison infix calculator always evaluates as 0
body: <p>I'm reading a book I found on the Internet about GNU Flex/Bison. And it's pretty cool. One of the examples involves writing an infix calculator. And that goes fine. </p>
  
  <p>The problem is, the book uses <code>int</code> as <code>YYSTYPE</code>, which creates obvious issues when, say, dividing 1 by 2. So I decided I'd modify the program to use <code>float</code> instead. So far, so good. The program (source is given below) compiles fine, but <strong>always gives an answer of 0</strong>, no matter what the calculation is. I've no idea how to debug it either, as it's obviously generated code.</p>
  
  <p><em>calc.l</em></p>
  
  <pre><code> %{
   #include "parser.h"
   %}
  
   %%
  
   "+"                        { return ADD; }
   "-"                        { return SUB; }
   "*"                        { return MUL; }
   "/"                        { return DIV; }
   [0-9]+                     { 
                                  yylval = atof(yytext); 
                                  return NUMBER; 
                              }
   \n                         { return EOL; }
   [ \t]                      { ; }
   .                          { yyerror("Unknown symbol"); }
  
   %%
  </code></pre>
  
  <p><em>calc.y</em></p>
  
  <pre><code> %{
   #include &lt;stdio.h&gt;
  
   #define YYSTYPE float
   %}
  
   %token NUMBER
   %token ADD SUB MUL DIV
   %token EOL
  
   %%
  
   /* List of expressions */
   calclist: 
      | calclist AS_result EOL { printf("%f\n-&gt; ", $2); }
      ;
  
   /* Add/subtract result. Evaluated after multiply/divide result */
   AS_result: MD_result
      | AS_result ADD MD_result       { $$ = $1 + $3; }
      | AS_result SUB MD_result       { $$ = $1 - $3; }
      ;
  
   /* Multiply/divide result. Evaluated first. */
   MD_result: NUMBER
      | MD_result MUL NUMBER          { $$ = $1 * $3; }
      | MD_result DIV NUMBER          { $$ = $1 / $3; }
      ;
   %%
  
   int yyerror(char *msg)
   {
      printf("Error: '%s'\n", msg);
      return 0;
   }
  
   int main(int argc, char **argv)
   {
      printf("-&gt; ");
      yyparse();
      return 0;
   }
  </code></pre>
  
  <p><em>Makefile</em></p>
  
  <pre><code> make: calc.l calc.y
      bison -Wall -o parser.c --defines=parser.h calc.y
      flex -o scanner.c calc.l
      cc -ggdb -o calc scanner.c parser.c -lfl
  
   clean:
      rm -f parser.c parser.h scanner.c calc.c calc
  </code></pre>
  
  <p><em>Example run</em></p>
  
  <pre><code>michael@michael-desktop:~/code/calculator$ ./calc 
  -&gt; 1 + 2
  0.000000
  -&gt; ^C
  michael@michael-desktop:~/code/calculator$
  </code></pre>
  
  <p>Feedback on any part of the code is appreciated as well as the actual problem. Cheers!</p>