title: Yacc: Invalid characters: ' '
body: <p>I am writing a program using flex/bison to evaluate an arithmetic expression. I can't figure out the mistake in the lex/yacc file. It says ' ' is an invalid character. How to add the spaces in a lex file as a rule so that it isn't considered as invalid?</p>
  
  <p>Lex program</p>
  
  <pre><code>%{ 
  #include&lt;math.h&gt; 
  extern yylval; 
  %} 
  %%
   
  [0-9]+ {yylval=atoi(yytext);return NUM;} 
  [+] {return '+';} 
  [-] {return '-';} 
  [*] {return '*';} 
  [/] {return '/';}  
  . {return yytext[0];}
  \n return 0; 
  %%
  </code></pre>
  
  <p>Yacc:</p>
  
  <pre><code>%{
  #include&lt;stdio.h&gt; 
  %}
  
  %token NUM 
  %left '-' '+'
  %right '*' '/' 
  %start S
  %%
  S   : exp{printf("%d\n",$$);} 
  ;
  exp :   exp '+' exp{$$=$1+$3;} 
      |   exp'-'exp{$$=$1-$3;} 
      |   exp'*'exp{$$=$1*$3;} 
      |   exp'/'exp {if($3==0) exit(0);
  else 
  $$=$1/$3; 
   }
  |NUM 
  ;
  %%
   
  #include "lex.yy.c"
  void main() 
  { 
  printf("Enter the Expr. in terms of integers\n"); 
  yyparse(); 
  printf("Valid");
  }
  yywrap(){} 
  yyerror() 
  { 
  printf("Error\n"); 
  } 
  </code></pre>
  
  <p>This is the error:</p>
  
  <pre><code>arith.y:5.11: error: invalid characters: ‘ ’
  arith.y:7.15: error: invalid characters: ‘ ’
  arith.y:10.34: error: invalid characters: ‘ ’
  arith.y:12.39: error: invalid characters: ‘ ’
  arith.y:13.37: error: invalid characters: ‘ ’
  arith.y:14.37: error: invalid characters: ‘ ’
  </code></pre>