I am trying to learn some flex / bison and I am reading Flex and Bison from John Levin (O'Reilly). There is an example that I need to run, however I cannot get it to work, as I get the following error:
/tmp/ccKZcRYB.o: In function `yylex':
fb3-1.lex.c:(.text+0x2bd): undefined reference to `yylval'
/tmp/cclBqnOk.o: In function `main':
fb3-1funcs.c:(.text+0x420): undefined reference to `yyparse'
collect2: ld returned 1 exit status
I have four source files:
fb3-1.h
extern int yylineno;
void yyerror(char *s, ...);
struct ast {
int nodetype;
struct ast *l;
struct ast *r;
};
struct numval {
int nodetype;
double number;
};
struct ast *newast(int nodetype, struct ast *l, struct ast *r);
struct ast *newnum(double d);
double eval(struct ast *);
void treefree(struct ast *);
fb3-1.l
%option noyywrap nodefault yylineno
%{
#include "fb3-1.h"
#include "fb3-1.tab.h"
%}
EXP ([Ee][-+]?[0-9]+)
%%
"+" |
"-" |
"*" |
"/" |
"|" |
"(" |
")" { return yytext[0]; }
[0-9]+"."[0-9]*{EXP}? |
"."?[0-9]+{EXP}? { yylval.d = atof(yytext); return NUMBER; }
\n { return EOL; }
"//".*
[ \t] { }
. { yyerror("Mystery character %c\n", *yytext); }
%%
fb3-1.y
%{
#include <stdio.h>
#include <stdlib.h>
#include "fb3-1.h"
%}
%union {
struct ast *a;
double d;
}
%token <d> NUMBER
%token EOL
%type <a> exp factor term
%%
calclist:
| calclist exp EOL {
printf("=%4.4g\n",eval($2));
treefree($2);
printf("> ");
}
| calclist EOL { printf("> "); }
;
exp: factor
| exp '+' factor { $$ = newast('+', $1, $3); }
| exp '-' factor { $$ = newast('-', $1, $3); }
;
factor: term
| factor '*' term { $$ = newast('*', $1, $3); }
| factor '/' term { $$ = newast('/', $1, $3); }
;
term: NUMBER { $$ = newnum($1); }
| '|' term { $$ = newast('|', $2, NULL); }
| '(' term { $$ = $2; }
| '-' term { $$ = newast('M', $2, NULL); }
;
%%
fb3-1funcs.c
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "fb3-1.h"
struct ast * newast(int nodetype, struct ast *l, struct ast *r)
{
struct ast *a = malloc(sizeof(struct ast));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = nodetype;
a->l = l;
a->r = r;
return a;
}
struct ast * newnum(double d)
{
struct numval *a = malloc(sizeof(struct numval));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'K';
a->number = d;
return (struct ast *)a;
}
double eval (struct ast *a)
{
double v;
switch(a->nodetype) {
case 'K': v = ((struct numval *)a)->number; break;
case '+': v = eval(a->l) + eval(a->r); break;
case '-': v = eval(a->l) + eval(a->r); break;
case '*': v = eval(a->l) + eval(a->r); break;
case '/': v = eval(a->l) + eval(a->r); break;
case '|': v = eval(a->l); if(v < 0) v = -v; break;
case 'M': v = -eval(a->l); break;
default: printf("internal error: bad node %c\n", a->nodetype);
}
}
void treefree(struct ast *a)
{
switch(a->nodetype)
{
case '+':
case '-':
case '*':
case '/':
treefree(a->r);
case '|':
case 'M':
treefree(a->l);
case 'K':
free(a);
break;
default: printf("internal error: free bad node %c\n", a->nodetype);
}
}
void yyerror(char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "%d: error: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
int main ()
{
printf("> ");
return yyparse();
}
To build:
bison -d fb3-1.y
flex -ofb3-1.lex.c fb3-1.l
cc -o $@ fb3-1.tab.c fb3-1.lex.c fb3-1funcs.c
I am running Ubuntu 10.04 x64 with the flex and bison packages installed. Does anyone know why this error occurs, and how to fix it? Thanks in advance:)