我正在尝试 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);
}
命令行:
bison -d fb1-5.y
flex fb1-5.l
cc -o $@ fb1-5.tab.c lex.yy.c -ll
我使用 -ll 而不是 -lfl 因为显然在 Mac os x 上, 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)
有任何想法吗?