Macos 架构x86_64的未定义符号_“yylval”;,引用自Mac OS上的yylex

Macos 架构x86_64的未定义符号_“yylval”;,引用自Mac OS上的yylex,macos,bison,flex-lexer,Macos,Bison,Flex Lexer,我正在尝试一些来自O'Reilly Flex&Bison的例子。我尝试的第一个Bison和Flex程序在链接源代码时给出了下一个错误: 架构x86_64的未定义符号:“_yylval”,引用 发件人: _yylex in lex-0qfK1M.o 因为我是Mac的新手,我只是在尝试这些例子,我不知道这里出了什么问题 l文件: /* recognize tokens for the calculator and print them out */ %{ #include "fb1-5.tab

我正在尝试一些来自O'Reilly Flex&Bison的例子。我尝试的第一个Bison和Flex程序在链接源代码时给出了下一个错误:

架构x86_64的未定义符号:“_yylval”,引用

发件人:

  _yylex in lex-0qfK1M.o
因为我是Mac的新手,我只是在尝试这些例子,我不知道这里出了什么问题

l文件:

/* 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文件:

/* 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);
}
我使用-ll而不是-lfl,因为在MacOSX上显然没有fl库

输出:

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)

有什么想法吗?

显然,奥雷利的《Flex&Bison》一书中充满了错误

很奇怪,他们甚至懒得测试自己的例子


问题的一部分已经解决了,但不是全部。请参阅勘误表确认页。

我编译了一个从

%{
#include "y.tab.h"
%}
使用命令

gcc -ll lex.yy.c
这起到了作用:

gcc -ll y.tab.c lex.yy.c
发生什么事了?在y.tab.h中有一个声明

extern int yylval

它允许lex.yy.c编译。但是,lex.yy.o需要针对包含yylval的对象文件进行链接,例如y.tab.o

您的.y文件是您的.l文件的副本--这将导致bison的各种错误。yylval是在bison生成的代码中定义的…哦,我的错,将编辑此帖子。今晚我将尝试此功能,也许它可以解决问题。非常感谢。这是如此神秘,应该在我下面的书中提到。
extern int yylval