以下 flex 和 bison 程序中的错误是什么?

what is the error in the following flex and bison program?

我一直在关注 O'Reilly 的 Flex and Bison 一书中的计算器示例,到目前为止我已经完成了以下操作。

我正在为 Windows 使用 Flex 和 Bison,为了使用 Flex 编译它,我使用以下命令进行编译:

flex f4.l

为了获取所需的 .exe 文件,我将:

gcc lex.yy.c -L "C:\Program\GnuWin32\lib" -lfl

到目前为止,生成的 .exe 文件工作正常。

现在书上说要让bison编译.y文件,我的代码是:

文件:f5.y

    /* simplest version of calculator */
%{
#include <stdio.h>
%}
/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%%
calclist: /* nothing */ 
 | calclist exp EOL { printf("= %d\n", ); } 
 ;
exp: factor 
 | exp ADD factor { $$ =  + ; }
 | exp SUB factor { $$ =  - ; }
 ;
factor: term 
 | factor MUL term { $$ =  * ; }
 | factor DIV term { $$ =  / ; }
 ;
term: NUMBER 
 | ABS term { $$ =  >= 0?  : - ; }
;
%%
main(int argc, char **argv)
{
 yyparse();
}
yyerror(char *s)
{
 fprintf(stderr, "error: %s\n", s);
}

Bison 生成了两个文件:f5.tab.c 和 f5.tab.h

我的 f4.l 文件如下:

%{
# include "f5.tab.h"
int yylval;
%}
/* recognize tokens for the calculator and print them out */
%{
 int yylval;
%}

%%
"+" { 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); }
%%
/*main(int argc, char **argv)
{
 int tok;
 while(tok = yylex()) {
 printf("%d", tok);
 if(tok == NUMBER) printf(" = %d\n", yylval);
 else printf("\n");
 }
}*/

我用以下代码编译我的程序:

gcc f5.tab.c lex.yy.c -L "C:\Program\GnuWin32\lib" -lfl

问题是当我 运行 编译程序时,例如如果我输入:

2 + 3 * 4
= 1968511970
20/2
= 1968511970

为什么我有这些答案?好像是在显示内存位置,我想,有什么帮助吗?

| calclist exp EOL { printf("= %d\n", ); } 

你打印错了。应该是:

| calclist exp EOL { printf("= %d\n", ); }