title: Undefined reference to `_yyerror' when compiling with Flex and Bison
body: <p>I'm trying to make a compiler for a mini Pascal-like language. I'm using Flex and Bison for this and I came up with this error.</p>
  
  <p>My Flex file:</p>
  
  <pre><code>%{
  #include "y.tab.h"
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  void yyerror(char *);
  %}
  
  %%
  
  [1-9][0-9]*     {
                  yylval.i = atoi(yytext);
                  return INT;
  }
  
  program     return PROGRAM;
  or          return OR;
  and         return AND;
  not         return NOT;
  if          return IF;
  else        return ELSE ;
  while       return WHILE;
  "+"         return PLUS;
  "-"         return MINUS;
  "*"         return MUL;
  "/"         return DIV;
  "["         return LSB;
  "]"         return RSB;
  "{"         return LCB;
  "}"         return RCB;
  "("         return LEFTPAR;
  ")"         return RIGHTPAR;
  ":="        return ASSIGN;
  "=="        return ISEQUAL;
  "&lt;"         return LTHAN;
  "&gt;"         return GTHAN;
  "&lt;&gt;"        return NOTEQUAL;
  "&lt;="        return LESSEQUAL;
  "&gt;="        return GREATEREQUAL;
  
  [a-zA-z][a-z0-9]*   {
                      yylval.s = (char*)malloc(strlen(yytext)*sizeof(char));
                      strcopy(yylval.s,yytext);
                      return ID;
  }               
  
  [ \t\n]+                            /* eat up whitespace */         
  
  .                                   yyerror("Unknown Character");
  
  %%
  int yywrap(void) {
      return 1;
  }
  </code></pre>
  
  <p>My Bison file:</p>
  
  <pre><code>%{
      #include &lt;stdio.h&gt;
      #include &lt;string.h&gt;
      int yylex(void);
      void yyerror(char *s);
  %}
  
  %union {
      int i;
      char *s;
  };
  
  %token &lt;i&gt; INTEGERNUM
  
  %token PROGRAM;
  %token OR;
  %token AND;
  %token NOT;
  %token IF;
  %token ELSE;
  %token WHILE;
  %token PLUS;
  %token MINUS;
  %token MUL;
  %token DIV;
  %token LSB;
  %token RSB;
  %token LCB;
  %token RCB;
  %token LEFTPAR;
  %token RIGHTPAR;
  %token ID;
  %token INT;
  %token ASSIGN;
  %token ISEQUAL;
  %token LTHAN;
  %token GTHAN;
  %token NOTEQUAL;
  %token LESSEQUAL;
  %token GREATEREQUAL;
  
  %%
  
  program:
          PROGRAM ID block
          ;
  
  block:
          LCB sequence RCB
          ;
  
  sequence:
          statement ';' sequence
          | statement ';' 
          ;
  
  bracketsSeq:
          LCB sequence RCB
          ;
  
  brackOrStat:        
          bracketsSeq
          | statement
          ;
  
  statement:
          assignmentStat
          |ifStat
          |whileStat
          |
          ;
  
  assignmentStat:
          ID ':=' expression
  
  ifStat:
          IF LEFTPAR condition RIGHTPAR brackOrStat elsepart
          ;
  
  elsepart:
          ELSE brackOrStat
          |
          ;
  
  whileStat:
          WHILE LEFTPAR condition RIGHTPAR brackOrStat
          ;
  
  expression:
          optionalSign expression
          |expression addOper expression
          |term
          ;
  
  term:
          term mulOper term
          |factor
          ;
  
  factor:
          INT
          |LEFTPAR expression RIGHTPAR
          |ID
          ;
  
  condition:
          condition AND condition
          |boolterm
          ;
  
  boolterm:
          boolterm OR boolterm
          |boolfactor
          ;
  
  boolfactor:
          NOT LSB condition RSB
          |LSB condition RSB
          |expression relationalOper expression
          ;
  
  relationalOper:
          ISEQUAL
          |LTHAN
          |GTHAN
          |NOTEQUAL
          |LESSEQUAL
          |GREATEREQUAL
          ;
  
  addOper:
          PLUS
          |MINUS
          ;
  
  mulOper:
          MUL
          |DIV
          ;
  
  optionalSign:
          addOper
          |
          ;
  
  %%
  
  int main( int argc, char **argv )
               {
               printf("TEST\n");
  
               }      
  </code></pre>
  
  <p>The series of steps I executed was:</p>
  
  <pre><code>$ ./bison.exe -dy comp.y
  $ ./flex.exe comp.l
  $ gcc -c -w lex.yy.c 
  $ gcc -c -w comp.tab.c
  $ gcc comp.tab.o lex.yy.o -o ex
  comp.tab.o:comp.tab.c:(.text+0x4cd): undefined reference to `_yyerror'
  comp.tab.o:comp.tab.c:(.text+0x61c): undefined reference to `_yyerror'
  lex.yy.o:lex.yy.c:(.text+0x34a): undefined reference to `_strcopy'
  lex.yy.o:lex.yy.c:(.text+0x362): undefined reference to `_yyerror'
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: lex.yy.o: bad reloc address                0x828 in section `.rdata'
  /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid                operation
  collect2: ld returned 1 exit status
  $
  </code></pre>
  
  <p>Any advice as to what to declare and where cause there seems to be that I have declared something the wrong way!</p>