title: Calc with bison and flex doesn't work operation negative
body: <p>I have a problem with this Bison program. I don't know why Reuse does not work. For the operation with the negative numbers, I just use the same line for the get the first number and to operate with more operations. I just changed the first number to negative.</p>
  
  <p><strong><code>calc.y</code></strong></p>
  
  <pre><code>%{
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  extern int yylex();
  extern int yyparse();
  extern FILE* yyin;
  
  void yyerror(const char* s);
  %}
  
  %union {
      int ival;
      float fval;
  }
  
  %token&lt;ival&gt; T_INT
  %token&lt;fval&gt; T_FLOAT
  %token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LEFT T_RIGHT
  %token T_NEWLINE T_QUIT
  %left T_PLUS T_MINUS
  %left T_MULTIPLY T_DIVIDE
  
  %type&lt;ival&gt; expression
  %type&lt;fval&gt; mixed_expression
  
  %start calculation
  
  %%
  
  calculation: 
         | calculation line
  ;
  
  line: T_NEWLINE
      | mixed_expression T_NEWLINE { printf("\tResult: %f\n", $1);}
      | expression T_NEWLINE { printf("\tResult: %i\n", $1); } 
      | T_QUIT T_NEWLINE { printf("bye!\n"); exit(0); }
  ;
  
  mixed_expression: T_FLOAT                        { $$ = $1; }
        | T_MINUS T_FLOAT  { $$ = -$2; }    
        | mixed_expression T_PLUS mixed_expression     { $$ = $1 + $3; }
        | mixed_expression T_MINUS mixed_expression    { $$ = $1 - $3; }
        | mixed_expression T_MULTIPLY mixed_expression { $$ = $1 * $3; }
        | mixed_expression T_DIVIDE mixed_expression   { $$ = $1 / $3; }
        | T_LEFT mixed_expression T_RIGHT      { $$ = $2; }
        | T_MINUS mixed_expression T_RIGHT         { $$ = -$2; }
        | expression T_PLUS mixed_expression       { $$ = $1 + $3; }
        | expression T_MINUS mixed_expression      { $$ = $1 - $3; }
        | expression T_MULTIPLY mixed_expression   { $$ = $1 * $3; }
        | expression T_DIVIDE mixed_expression     { $$ = $1 / $3; }
        | mixed_expression T_PLUS expression       { $$ = $1 + $3; }
        | mixed_expression T_MINUS expression      { $$ = $1 - $3; }
        | mixed_expression T_MULTIPLY expression   { $$ = $1 * $3; }
        | mixed_expression T_DIVIDE expression     { $$ = $1 / $3; }
        | expression T_DIVIDE expression       { $$ = $1 / (float)$3; }
  ;
  
  expression: T_INT               { $$ = $1; }
  
        | expression T_PLUS expression    { $$ = $1 + $3; }
        | expression T_MINUS expression   { $$ = $1 - $3; }
        | expression T_MULTIPLY expression    { $$ = $1 * $3; }
        | T_LEFT expression T_RIGHT       { $$ = $2; }
        | T_MINUS T_INT   { $$ = -$2; }
  ;
  
  %%
  
  int main() {
      yyin = stdin;
  
      do { 
          yyparse();
      } while(!feof(yyin));
  
      return 0;
  }
  
  void yyerror(const char* s) {
      fprintf(stderr, "Parse error: %s\n", s);
      exit(1);
  }
  </code></pre>
  
  <p><strong><code>calclex.l</code></strong></p>
  
  <pre><code>%option noyywrap
  
  %{
  #include &lt;stdio.h&gt;
  
  #define YY_DECL int yylex()
  
  #include "calc.tab.h"
  
  %}
  
  %%
  
  [ \t]   ; // ignore all whitespace
  [0-9]+\.[0-9]+  {yylval.fval = atof(yytext); return T_FLOAT;}
  [0-9]+      {yylval.ival = atoi(yytext); return T_INT;}
  \n      {return T_NEWLINE;}
  "+"     {return T_PLUS;}
  "-"     {return T_MINUS;}
  "*"     {return T_MULTIPLY;}
  "/"     {return T_DIVIDE;}
  "("     {return T_LEFT;}
  ")"     {return T_RIGHT;}
  "exit"      {return T_QUIT;}
  "quit"      {return T_QUIT;}
  
  %%
  </code></pre>