title: segmentation fault in my code in bison-generated parser
body: <p>I am tring to build a calculator which can do bitewise operations. My code can be compiled correctly. But when I run it ,and type in any value, Segementation Fault occurs. I wonder whether you may help me. </p>
  
  <p>.l file </p>
  
  <pre><code>%}
  
  %option noyywrap
  
  %%
  [0-9]+  {
              yylval.dval=atof(yytext);
              return INTEGER;
          }
  
  
  ([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
                                                 yylval.dval = atof(yytext);
                                                 return NUMBER;
                                              }
  
  [ \t    ]  ;
  
  
  
  [A-Za-z][A-Za-z0-9]*   {
                        struct symtab *sp = symlook(yytext);
                        yylval.dval = 0.0;
                        return NAME;
                     }
  
  "**"    {
      return POW;
      }
  
  "&lt;&lt;"    {
      return LSHIFT;
      }
  
  "&gt;&gt;"    {
      return RSHIFT;
      }
  
  "$"      { return 0; }
  
  \n    |
  .     return yytext[0];
  %%
  </code></pre>
  
  <p>.h file</p>
  
  <pre><code>    #define NSYMS 20
  
  struct symtab {
    char *name;
    double (*funcptr)();
    double dval;
    enum constype {integer, floatingpoint} constype;
  } symtab[NSYMS];
  
  
  struct symtab *symlook();
  </code></pre>
  
  <p>.y file</p>
  
  <pre><code>%{ 
  #include "zcalc.h"
  
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  
  int flag = 0;
  
  void yyerror( char *mesg ); /* yacc error checker */
  
  %}
  
  %union {
    double dval;
    struct symtab *symp;
  }
  
  %token &lt;symp&gt; NAME
  %token &lt;dval&gt; NUMBER
  %token &lt;dval&gt; INTEGER
  %token POW
  %token LSHIFT
  %token RSHIFT
  
  %left '-' '+'
  %left '*' '/'
  
  %type &lt;symp&gt; expression
  %%
  
  statement_list: statement '\n'
              | statement_list statement '\n'
  
      statement: NAME '=' expression {$1-&gt;dval = $3-&gt;dval;
                              if($3-                 &gt;constype==integer)
                                  $1-            &gt;constype=integer;
                              else
                                  $1-           &gt;constype=floatingpoint;}
              | expression { printf("= %g\n", $1-&gt;dval); }
  
   expression: '+' expression expression { $$-&gt;dval = $2-&gt;dval + $3-&gt;dval; 
                                      if($2-&gt;constype==integer &amp;&amp; $3-&gt;constype)
                                          $$-&gt;constype=integer;
                                      else $$-&gt;constype=floatingpoint;
                                      }
          | '-' expression expression { $$-&gt;dval = $2-&gt;dval - $3-&gt;dval; 
                                          if($2-&gt;constype==integer &amp;&amp; $3-&gt;constype)
                                          $$-&gt;constype=integer;
                                      else $$-&gt;constype=floatingpoint;}
          | '*' expression expression { $$-&gt;dval = $2-&gt;dval * $3-&gt;dval; 
                                          if($2-&gt;constype==integer &amp;&amp; $3-&gt;constype)
                                          $$-&gt;constype=integer;
                                      else $$-&gt;constype=floatingpoint;}
          | '/' expression expression { $$-&gt;dval = $2-&gt;dval / $3-&gt;dval;
                                          if($2-&gt;constype==integer &amp;&amp; $3-&gt;constype)
                                          $$-&gt;constype=integer;
                                      else $$-&gt;constype=floatingpoint;}
          | '(' expression ')' { $$-&gt;dval = $2-&gt;dval; 
                                          if($2-&gt;constype==integer)
                                          $$-&gt;constype=integer;
                                      else $$-&gt;constype=floatingpoint;}
          | '&amp;' expression expression { 
                                          if($2-&gt;constype!=integer||$3-&gt;constype!=integer)
                                          $$-&gt;dval = (int)($2-&gt;dval) &amp; (int)($3-&gt;dval);
                                          else
                                          printf("Error: bitewise operations cannot be done to floating point vals./n")}
          | '|' expression expression { 
                                          if($2-&gt;constype!=integer||$3-&gt;constype!=integer)
                                          $$-&gt;dval = (int)($2-&gt;dval) | (int)($3-&gt;dval);
                                          else
                                          printf("Error: bitewise operations cannot be done to floating point values./n")}
          | '^' expression expression { 
                                          if($2-&gt;constype!=integer||$3-&gt;constype!=integer)
                                          $$-&gt;dval = (int)($2-&gt;dval) ^ (int)($3-&gt;dval);
                                          else
                                          printf("Error: bitewise operations cannot be done to floating point values./n")}
          | '~' expression { 
                                          if($2-&gt;constype!=integer)
                                          $$-&gt;dval = ~(int)($2-&gt;dval);
                                          else
                                          printf("Error: bitewise operations cannot be done to floating point values./n")}
          | LSHIFT expression expression { 
                                          if($2-&gt;constype!=integer||$3-&gt;constype!=integer)
                                          $$-&gt;dval = (int)$2-&gt;dval &lt;&lt; (int)$3-&gt;dval;
                                          else
                                          printf("Error: bitewise operations cannot be done to floating point values./n")}
          | RSHIFT expression expression { 
                                          if($2-&gt;constype!=integer||$3-&gt;constype!=integer)
                                          $$-&gt;dval = (int)$2-&gt;dval &gt;&gt; (int)$3-&gt;dval;
                                          else
                                          printf("Error: bitewise operations cannot be done to floating point values./n")}    
          | POW   expression expression { $$-&gt;dval=pow($2-&gt;dval,$3-&gt;dval);}
          | NUMBER {$$-&gt;dval=$&lt;dval&gt;1;
                  $$-&gt;constype=floatingpoint;
                   }
          | INTEGER {
                  $$-&gt;dval=$&lt;dval&gt;1;
                      $$-&gt;constype=integer;}
          | NAME {
                  $$-&gt;dval = $1-&gt;dval;
                  if($1-&gt;constype==integer )
                      $$-&gt;constype=integer;
                  else $$-&gt;constype=floatingpoint;}
   %%
  
   struct symtab * symlook( char *s ) {
      char *p;
      struct symtab *sp;
  
      for(sp = symtab; sp &lt; &amp;symtab[NSYMS]; sp++) {
        /* is it already here? */
   if (sp-&gt;name &amp;&amp; !strcmp(sp-&gt;name, s))
     return sp;
  
   /* is it free */
   if (!sp-&gt;name) {
     sp-&gt;name = strdup(s);
     return sp;
   }
        /* otherwise continue to the next */
      }
      yyerror("Too many symbols...\n");
      exit(1);
   }
  
   void addfunc( char *name, double (*func)() ) {
     struct symtab *sp = symlook(name);
     sp-&gt;funcptr = func;
   }
  
  
   /* yacc error function */
   void yyerror( char *mesg ) {
         flag = 1;
         printf("%s \n" , mesg);  
       }
  
  
  int main() {
  
    yyparse();
  
    return 0;
  }
  </code></pre>
  
  <p>Any help will be appreciated!</p>