Undefined symbols for architecture x86_64:"_yylval", referenced from _yylex on Mac OS
Asked Answered
F

2

5

I'm trying out some examples from O'Reilly Flex & Bison. The first Bison and Flex program I'm trying gives me next error when linking the sources:

Undefined symbols for architecture x86_64: "_yylval", referenced

from:

  _yylex in lex-0qfK1M.o

As I'm new to Mac and I'm just trying the examples, I have no clue what's wrong here.

l file:

/* recognize tokens for the calculator and print them out */
%{
#include "fb1-5.tab.h"
%}

%%
"+"     { return ADD; }
"-"     { return SUB; }
"*"     { return MUL; }
"/"     { return DIV; }
"|"     { return ABS; }
[0-9]+  { yylval = atoi(yytext); return NUMBER; }
\n      { return EOL; }
[ \t]   { /* Ignore whitespace */ }
.       { printf("Mystery character %c\n", *yytext); }
%%

y file:

/* simplest version of calculator */
%{
#include <stdio.h>
%}
/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%%
calclist: /* nothing */ matches at beginning of input
 | calclist exp EOL { printf("= %d\n", $1); } EOL is end of an expression
 ;
exp: factor default $$ = $1
 | exp ADD factor { $$ = $1 + $3; }
 | exp SUB factor { $$ = $1 - $3; }
 ;
factor: term default $$ = $1
 | factor MUL term { $$ = $1 * $3; }
 | factor DIV term { $$ = $1 / $3; }
 ;
term: NUMBER default $$ = $1
 | ABS term { $$ = $2 >= 0? $2 : - $2; }
 ;
%%
main(int argc, char **argv)
{
    yyparse();
}

yyerror(char *s)
{
    fprintf(stderr, "error: %s\n", s);
}

Command line:

bison -d fb1-5.y
flex fb1-5.l
cc -o $@ fb1-5.tab.c lex.yy.c -ll

I use -ll instead of -lfl because apparently on Mac os x, the fl library isn't there.

Output:

Undefined symbols for architecture x86_64:
  "_yylval", referenced from:
      _yylex in lex-0qfK1M.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any ideas?

Fortuneteller answered 11/3, 2013 at 20:53 Comment(3)
Your .y file is a copy of your .l file -- which will cause all kinds of errors from bison. yylval is defined in the code generated by bison...Systematics
Ow, my bad, will edit the post.Fortuneteller
I'll try this tonight #3299218 , maybe it solves the problem.Fortuneteller
F
1

Apparently the Flex & Bison book from O'Reilly is full of errors.

See http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596155988

Very weird that they do not even bother test their own examples...

Part of the question is solved in Undefined reference to yyparse (flex & bison) but not everything. See the errataunconfirmed page.

Fortuneteller answered 13/3, 2013 at 11:51 Comment(0)
S
6

I caused a similar error by compiling a lex file that began

%{
#include "y.tab.h"
%}

using the command

gcc -ll lex.yy.c

This worked:

gcc -ll y.tab.c lex.yy.c

What's going on? There's a declaration in y.tab.h

extern int yylval

which allows lex.yy.c to compile. However, lex.yy.o needs to be linked against an object file that includes yylval, such as y.tab.o

Strontian answered 3/12, 2013 at 5:11 Comment(1)
Thank you VERY much. This is so arcane and should be mentioned in the Lex and Yacc book I'm following.Autum
F
1

Apparently the Flex & Bison book from O'Reilly is full of errors.

See http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596155988

Very weird that they do not even bother test their own examples...

Part of the question is solved in Undefined reference to yyparse (flex & bison) but not everything. See the errataunconfirmed page.

Fortuneteller answered 13/3, 2013 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.