How can I resolve the "undefined reference to 'yylval'" error in the simplest program ever?
This is my lexer.l file
%{
#include "parser.tab.h"
%}
%%
[0-9]+ {
yylval = atoi(yytext);
return NUMBER; }
"-" {return MINUS;}
%%
int main ()
{
yylex();
return 0;
}
and this is my parser file
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(const char* msg);
int n=0;
%}
%token MINUS
%token NUMBER
%%
program : NUMBER MINUS NUMBER { n=$1-$3 ; printf("%d\n",n);}
%%
void yyerror(const char* msg){
printf("%s\n", msg);
}
int main() {
printf("Enter ");
yyparse(); // Start parsing
return 0;
}
And these are the commands I run to build this program:
C:\Users\User\Desktop\workbench\prevodiocip\nnn>bison -d parser.y
C:\Users\User\Desktop\workbench\prevodiocip\nnn>flex lexer.l
C:\Users\User\Desktop\workbench\prevodiocip\nnn>gcc -o parser.tab.c lex.yy.c -lfl
C:\Users\User\AppData\Local\Temp\ccOvWXJo.o:lex.yy.c:(.text+0x1b1): undefined reference to `yylval'
collect2.exe: error: ld returned 1 exit status
I have tried everything, but nothing works for me.
-o parser.tab.cyou're saying thatparser.tab.cshould be the executable file, so you're overwriting the original.extern intis insufficient to create a proper definition (e.g.,extern int yylval;is not a definition), and header files are generally inappropriate places to define objects. A definition should be placed in a C file, possiblyYYSTYPE yylval;in modern versions of Bison.parser.tab.cwhich contains the definition. You've tried to name it as the linker output file, which doesn't make any sense at all. NB (1) flexbox isn't for questions about flex(1): be accurate. (2) You don't need the definition forMINUS: does return ad use'-', which is simpler all round.libfl, except possibly in support of a program built entirely from a scanner definition. This library provides mainly amain()that you definitely do not want when driving the scanner from a separate parser, and a dummy-ishyywrap()that you would do better to obviate by adding%option noyywrapto your scanner definition.