4

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

-lfl の代わりに -ll を使用しているのは、どうやら 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)

何か案は?

4

2 に答える 2

0

どうやら O'Reilly の Flex & Bison ブックには間違いがたくさんあります。

http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596155988を参照してください。

彼らが自分の例をテストすることすらしないのは非常に奇妙です...

質問の一部はyyparse (flex & bison) への未定義の参照で解決されますが、すべてではありません。正誤表未確認のページをご覧ください。

于 2013-03-13T11:51:28.297 に答える