title: Find Where Syntax Error in Flex Yacc Happened
body: <p>I am pretty new to lex and yacc. </p>
  
  <p>I'm designing a compiler which can make three-address-code.</p>
  
  <p>How can I found where syntax error happen in my code ?</p>
  
  <p>after entering :</p>
  
  <pre><code>flex lexer.l
  bison -dy parser.y
  gcc lex.yy.c y.tab.c -o program.exe
  </code></pre>
  
  <p>I try this input : </p>
  
  <pre><code>{ int abc = 234 ; }
  </code></pre>
  
  <p>and then it gives me syntax error!</p>
  
  <p>How can i Fix it?</p>
  
  <p>This is my Lexer</p>
  
  <p>lexer.l:</p>
  
  <pre><code>%{
  
  #include "y.tab.h"
  #include &lt;string.h&gt;
  int yyerror(char *errormsg);
  
  %}
  
  letter  [a-zA-z]
  digit   [0-9]
  id      {letter}({letter}|{digit})*
  ws      [ \t]
  
  
  %%
  {ws}        ;
  \{          { return 300; }
  \}          { return 301; }
  \;          { return SEMICOLON; }
  "if"        { return IF; }
  "int"       { return INT; }
  "float"     { return FLOAT; }
  "char"      { return CHAR; }
  \=          { return ASSIGN; }      
  {id}        {strcpy(yylval.str,yytext) ; return ID; }
  {digit}+    {yylval.ival=atoi(yytext); return NUMBER; }
  .           {yyerror("Invalid Command");}
  %%
  
  
  
  int main(void)
  {
     yyparse();
     printf("DONE");
     return 0;
  }
  
  int yywrap(void)
  {
     return 0;
  }
  
  int yyerror(char *errormsg)
  {
      fprintf(stderr, "hey!%s\n", errormsg);
      exit(1);
  }
  </code></pre>
  
  <p>This is my Parser</p>
  
  <p>parser.y:</p>
  
  <pre><code>%{
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  int yylex(void);
  int yyerror(const char *s);
  
  %}
  
  
  %union{int ival; double dval; char str[120]; }
  
  %token INT ASSIGN NUMBER IF SEMICOLON
  %token FLOAT
  %token ID CHAR
  
  %%
  
  Program: 
          Block
          ;
  
  Block:
          '{' Stmts '}'
          ;
  
  Stmts:
          Stmts Stmt
          | Stmt
          ;
  
  Stmt:
          Block
          |IfStmt
          |AssignStmt
          |DeclStmt
          ;
  
  
  IfStmt:
          IF '(' Expr ')' Stmt  { printf("if found"); }
          ;
  
  
  AssignStmt:     
          Type ID ASSIGN Expr SEMICOLON { printf("int found!"); }
          ;
  
  DeclStmt:
          Type ID SEMICOLON
          ;
  
  
  Type:
          INT
          |FLOAT
          |CHAR
          ;
  
  
  Expr:
      NUMBER
      ;
  </code></pre>