title: Bison and Flex code works every other time
body: <p>I am creating a basic Lisp interpreter by utilizing Bison and Flex, so as of now I am trying to make an infix adder that also reads a set of parenthesis. For some reason my code only seems to work every other time</p>
  
  <p>lisp.l</p>
  
  <pre><code> %option noyywrap
  %{
      #include &lt;stdlib.h&gt;
      #include &lt;stdio.h&gt;
      #include "lisp.tab.h"
  //  #include "lisp.h"
      #include &lt;string.h&gt;
      #define YYSTYPE double
  
      #define YY_DECL int yylex()
  %}
  
  %%
  
  [ \t]    ;  {/* eat up whitespace */}
  [0-9]+(\.[0-9]+)?   {yylval.dval = atof(yytext); return DOUBLE;}
  "+"             {return ADD; }
  "-"         {return SUB; }
  "/"         {return DIVIDE; }
  "*"         {return MULTIPLY; }
  "let"           {return LET;}
  "print"         {return PRINT;}
  "EQ"            {return EQUAL;}
  "LT"            {return LESSTHAN;}
  "LE"            {return LESSTHANEQUAL;}
  "GT"            {return GREATERTHAN;}
  "GE"            {return GREATERTHANEQUAL;}
  "NE"            {return NOTEQUAL;}
  "if"            {return IF;}
  ";"             {return IGNORE;}
  [a-zA-Z]+       {return identifier;}
  "("         {return LEFTPAR;}
  ")"         {return RIGHTPAR;}
  
  
  %%
  </code></pre>
  
  <p>lisp.y</p>
  
  <pre><code>%{
  
  
      #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
  //  #include &lt;map&gt;
      #include &lt;string.h&gt;
      #include &lt;math.h&gt;
      #include "lisp.tab.h"
  
  
  //  static std::map&lt;std::string, double&gt; symbolsMap;
  
  //  double symbolValMap(std::string symbol);
  //  void updateSymbolValMap(std::string symbol, double val);
  
      extern int yylex();
  
      extern FILE* yyin;
  
      extern int yyparse();
  
      void yyerror(const char* msg);
  %}
  
  %union {
      double dval;
  }
  %token&lt;dval&gt; DOUBLE
  %token&lt;id&gt; identifier
  %token LET PRINT EQUAL LESSTHAN LESSTHANEQUAL GREATERTHAN GREATERTHANEQUAL
  %token NOTEQUAL IF IGNORE LEFTPAR RIGHTPAR MULTIPLY DIVIDE SUB ADD
  
  
  %type&lt;dval&gt; mixed_expression
  %type&lt;id&gt; varName
  
  %left SUB ADD MULTIPLY DIVIDE
  
  %start program
  
  
  %%
  program:
      | program line
      ;
  
  line: '\n'
      | mixed_expression '\n'   {printf("\tResult: %f\n", $1);}
      | '(stop)\n'          {printf("bye!\n"); exit(0); }
      ;
  
  mixed_expression: DOUBLE        {$$ = $1;}
      | LEFTPAR ADD  mixed_expression mixed_expression RIGHTPAR     {$$ = $3 + $4; printf("Result: %g","%1f", $$);}
      | LEFTPAR SUB  mixed_expression mixed_expression RIGHTPAR     { $$ = $3 - $4; }
      | LEFTPAR MULTIPLY mixed_expression mixed_expression RIGHTPAR {$$ = $3 * $4;}
      | LEFTPAR DIVIDE mixed_expression mixed_expression RIGHTPAR   {$$ = $3 / $4;}
      | LEFTPAR  mixed_expression RIGHTPAR                { $$ = $2; }
      ;
  
  
  %%
  int main() {
      yyin = stdin;
      do { 
          yyparse();
      } while(!feof(yyin));
      return 0;
  }
  /*void updateSymbolValMap(std::string symbol, double val)
  {
  //  symbolsMap[symbol] = val;
  }
  double symbolValMap(std::string symbol)
  {
  // return symbolsMap[symbol];
      return 0;
  }*/
  void yyerror(char const* msg)
  {
      fprintf(stderr, "Parse error: %s\n", msg);
      yyparse();
  }
  </code></pre>
  
  <p>When I run the code and pass in arguments the following is outputted:</p>
  
  <pre><code>(- 4 5)
  Result: -1
  (* 8 9)
  Parse error: syntax error
  Parse error: syntax error
  Parse error: syntax error
  Parse error: syntax error
  </code></pre>
  
  <p>Makefile</p>
  
  <pre><code>all = lisp
  
  lisp.tab.c lisp.tab.h: lisp.y
      bison -d lisp.y
  
  lex.yy.c: lisp.l lisp.tab.h
      flex lisp.l
  
  lisp:   lex.yy.c lisp.tab.c lisp.tab.h
      gcc lisp.tab.c lex.yy.c -lm -o lisp
  
  clean:
      rm lisp lisp.tab.c lex.yy.c lisp.tab.h
  </code></pre>
  
  <p>No matter what, every other time I am given a parse error. Any help would be greatly appreciated.</p>