title: Lex and Yacc segmentation fault
body: <p>I'm trying to write a parser for mini-c using lex and yacc. Incase of single line inputs it work pretty well in case of multi line inputs it is throwing a segmentation fault. Running GDB did not help.</p>
  
  <p>c.l file</p>
  
  <pre><code>alpha [a-zA-Z]
  digit [0-9]
  %{
  #include &lt;stdlib.h&gt;
  void yyerror(char *);
  #include "y.tab.h"
  %}
  
  
  %%
  [ \t]       ;
  [ \n]   { yylineno = yylineno + 1;}
  int return INT;
  float return FLOAT;
  char return CHAR;
  void return VOID;
  double return DOUBLE;
  for     return FOR;
  while   return WHILE;
  if  return IF;
  else    return ELSE;
  printf  return PRINTF;
  struct  return STRUCT;
  ^"#include ".+ ;
  {digit}+       return NUM;
  {alpha}({alpha}|{digit})* return ID;
  "&lt;="    return LE;
  "&gt;="    return GE;
  "=="    return EQ;
  "!="    return NE;
  "&gt;" return GT;
  "&lt;" return LT;
  "."     return DOT;
  \/\/.* ;
  \/\*(.*\n)*.*\*\/ ;
  .       return yytext[0];
  %%
  </code></pre>
  
  <p>c.y file</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int yylex(void);
  extern FILE *fp;
  extern FILE *yyin;
  extern int yylineno;
  extern char* yytext;
  %}
  %token INT FLOAT CHAR DOUBLE VOID
  %token FOR WHILE 
  %token IF ELSE PRINTF 
  %token STRUCT 
  %token NUM ID
  %token INCLUDE
  %token DOT
  %right '='
  %left AND OR
  %left '&lt;' '&gt;' LE GE EQ NE LT GT
  %%
  start:  Function 
      | Declaration
      ;
  /* Declaration block */
  Declaration: Type Assignment ';' 
      | Assignment ';'    
      | FunctionCall ';'  
      | ArrayUsage ';'    
      | Type ArrayUsage ';'   
      | StructStmt ';'    
      | error 
      ;
  /* Assignment block */
  Assignment: ID '=' Assignment
      | ID '=' FunctionCall
      | ID '=' ArrayUsage
      | ArrayUsage '=' Assignment
      | ID ',' Assignment
      | NUM ',' Assignment
      | ID '+' Assignment
      | ID '-' Assignment
      | ID '*' Assignment
      | ID '/' Assignment 
      | NUM '+' Assignment
      | NUM '-' Assignment
      | NUM '*' Assignment
      | NUM '/' Assignment
      | '\'' Assignment '\''  
      | '(' Assignment ')'
      | '-' '(' Assignment ')'
      | '-' NUM
      | '-' ID
      |   NUM
      |   ID
      ;
  
  /* Function Call Block */
  FunctionCall : ID'('')'
      | ID'('Assignment')'
      ;
  
  /* Array Usage */
  ArrayUsage : ID'['Assignment']'
      ;
  
  /* Function block */
  Function: Type ID '(' ArgListOpt ')' CompoundStmt 
      ;
  ArgListOpt: ArgList
      |
      ;
  ArgList:  ArgList ',' Arg
      | Arg
      ;
  Arg:    Type ID
      ;
  CompoundStmt:   '{' StmtList '}'
      ;
  StmtList:   StmtList Stmt
      |
      ;
  Stmt:   WhileStmt
      | Declaration
      | ForStmt
      | IfStmt
      | PrintFunc
      | ';'
      ;
  
  /* Type Identifier block */
  Type:   INT 
      | FLOAT
      | CHAR
      | DOUBLE
      | VOID 
      ;
  
  /* Loop Blocks */ 
  WhileStmt: WHILE '(' Expr ')' Stmt  
      | WHILE '(' Expr ')' CompoundStmt 
      ;
  
  /* For Block */
  ForStmt: FOR '(' Expr ';' Expr ';' Expr ')' Stmt 
         | FOR '(' Expr ';' Expr ';' Expr ')' CompoundStmt 
         | FOR '(' Expr ')' Stmt 
         | FOR '(' Expr ')' CompoundStmt 
      ;
  
  /* IfStmt Block */
  IfStmt : IF '(' Expr ')' 
          Stmt 
      ;
  
  /* Struct Statement */
  StructStmt : STRUCT ID '{' Type Assignment '}'  
      ;
  
  /* Print Function */
  PrintFunc : PRINTF '(' Expr ')' ';'
      ;
  
  /*Expression Block*/
  Expr:   
      | Expr LE Expr 
      | Expr GE Expr
      | Expr NE Expr
      | Expr EQ Expr
      | Expr GT Expr
      | Expr LT Expr
      | Assignment
      | ArrayUsage
      ;
  %%
  #include&lt;ctype.h&gt;
  int count=0;
  
  int main(int argc, char *argv[])
  {
      yyin = fopen(argv[1], "r");
  
     if(!yyparse())
          printf("\nParsing complete\n");
      else
          printf("\nParsing failed\n");
  
      fclose(yyin);
      return 0;
  }
  
  yyerror(char *s) {
      printf("%d : %s %s\n", yylineno, s, yytext );
  }
  </code></pre>