user8763792
user8763792

Reputation:

Yacc error: undefined reference to `line_num'

I am assigned to implement a new programming language in lex and yacc. Below are some of my yacc code, it should print error and the line error occurs

//rest of the code
%%
#include "lex.yy.c"
extern int line_num;

main() {
  return yyparse();
}
void yyerror( char *s )
{   
    fprintf(stderr,"Syntax Error in line: %d\n%s\n",line_num, s);
}

The compiler gives the following error message:

/tmp/cclW8fn4.o: In function `yyerror':
y.tab.c:(.text+0x200f): undefined reference to `line_num'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

How to fix it?

Upvotes: 0

Views: 661

Answers (1)

user8763792
user8763792

Reputation:

As it was discussed in the comments extern int line_num; just declares line_num, which has to exist in another file.

Therefore the problem was solved when int line_num was declared in the lex file:

%{
   int line_num = 1;    
%}

Upvotes: 1

Related Questions