title: Flex and Bison does not seem to match
body: <p>I am trying to build a simple calculator, as in you type an arithmetic expression and press Enter to have your expression parsed and result calculated.</p>
  
  <p>The following code works (just bison) which a friend gave to me:</p>
  
  <pre><code>%{                
     #include&lt;stdio.h&gt;                                                                   
     #include &lt;math.h&gt;                                                       
     #define YYSTYPE double
  
     extern int line_number;                                                            
     int line_number=0;                                                                     
  void yyerror(char *err)
  {
     printf("Error: %s\n", err);
  }
  %}                                                                                   
  
  %token NUMBER        /* define token type for numbers */
  %token PLUS
  %token MINUS
  %token MULT
  %token DIV
  %token NEWLINE
  %token LPAR
  %token RPAR
  
  %%     /* Bison grammar rules */
  input   : /* empty production to allow an empty input */
          | line input  
          ;
  line    : expr NEWLINE   { printf("Result is %f\n", $1); }
  expr    : expr PLUS term   { $$ = $1 + $3; }
          | expr MINUS term   { $$ = $1 - $3; }
          | term            { $$ = $1; }
          ;
  term    : term MULT factor { $$ = $1 * $3; }
          | term DIV factor { $$ = $1 / $3; }
          | factor          { $$ = $1; }
          ;
  factor  : LPAR expr RPAR    { $$ = $2; }
          | NUMBER          { $$ = $1; }
          ;
  ;                                                                                    
  %%                                                                                   
  
  int yylex( void ) {
      int c = getchar();      /* read from stdin */
      if (c &lt; 0) return 0;        /* end of the input*/
      while ( c == ' ' || c == '\t' ) c = getchar( );
      if ( isdigit(c) || c == '.' ) {
      ungetc(c, stdin);    /* put c back into input */
      scanf_s ("%lf", &amp;yylval); /* get value using scanf */
      return NUMBER;       /* return the token type */
      }
      if(c=='+')
          return PLUS;
      if(c=='-')
          return MINUS;
      if(c=='*')
          return MULT;
      if(c=='/')
          return DIV;
      if(c=='\n')
          return NEWLINE;
      if(c=='(')
          return LPAR;
      if(c==')')
          return RPAR;
      return c; /* anything else... return char itself */
  }
  
  main ()                                                                              
  {
    yyparse ();
  }
  </code></pre>
  
  <p>Now when I try to split it into two files it just calls the main function and does seems to accept input but nothing is shown on the screen:</p>
  
  <p>calc.l:</p>
  
  <pre><code>%{
  #include "calc.tab.h"
  #include "stdlib.h"
  #define YYSTYPE double
  %}
  %option noyywrap
  %option never-interactive
  
  NUM  [0-9]*\.?[0-9]+
  white [ \r\t]+ 
  
  %%
  {white} { }
  {NUM}  {yylval=atof(yytext);return NUM;}
  "+"    {return PLUS;}
  "-"    {return MINUS;}
  "/"    {return DIV;}
  "*"    {return MUL;}
  "("    {return LP;}
  ")"    {return RP;}
  "\n"   {return NL;}
  %%
  </code></pre>
  
  <p>calc.y :</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdlib.h&gt;
  #define YYSTYPE double
  
  %}
  
  %token NUM
  %token PLUS
  %token MINUS
  %token MUL
  %token DIV
  %token LP
  %token RP
  %token NL
  
  %%
  
  Input   :
      | Line Input
      ;
  
  Line    : Exp NL        { printf("Result = %f\n", $1); }
  
  
  Exp : Exp PLUS Term     { $$ = $1 + $3; }
      | Exp MINUS Term    { $$ = $1 - $3; }
      | Term          { $$ = $1; }
      ;
  
  Term    : Term MUL Fact     { $$ = $1 * $3; }
      | Term DIV Fact     { $$ = $1 / $3; }
      | Fact          { $$ = $1; }
      ;
  
  Fact    : LP Exp RP         { $$ = $2; }
      | NUM           { $$ = $1; }
      ;
  
  %%
  int yyerror(char *err)
  {
     printf("Error: %s\n", err);
  }
  
  int main()
  {
      printf("Hello\n");
      yyparse();
  }
  </code></pre>
  
  <p>I compile and run with the following command
  <code>bison -d calc.y &amp;&amp; flex calc.l &amp;&amp; gcc -o calculator calc.tab.c lex.yy.c -lfl &amp;&amp; ./calculator</code></p>
  
  <p>This is a sample output (the 5+5 is my input, no result appears):</p>
  
  <pre><code>Hello
  5+5
  </code></pre>
  
  <p>Where did I go wrong ?</p>