带有十进制数字的flex输出问题

时间:2018-11-15 14:59:13

标签: bison flex-lexer

我正在Flex和Bison中编写一个小的浮点数计算器。到目前为止,我的代码如下: 弹性代码

%{
# include "prb1.tab.h"
float yylval;
%}

%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }
[0-9]+(\.[0-9]+)? { yylval = atof(yytext); return NUMBER; }
\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. { printf("Mystery character %c\n", *yytext); }
%%
yywrap()
{
}
/*main(int argc, char **argv)
{
 int tok;
 while(tok = yylex()) {
 printf("%d", tok);
 if(tok == NUMBER) printf(" = %f\n", yylval);
 else printf("\n");
 }

} * /

野牛代码

/* 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("= %f\n", $2); }
;
exp: factor
| exp ADD factor { $$ = $1 + $3; }
| exp SUB factor { $$ = $1 - $3; }
;
factor: term
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1 / $3; }
;
term: NUMBER
| ABS term { $$ = $2 >= 0? $2 : - $2; }
;
%%
main(int argc, char **argv)
{
    yyparse();
}
yyerror(char *s)
{
    fprintf(stderr, "error: %s\n", s);
}

我遇到的问题是,当我运行程序时,答案仍然是整数。如何更改它以将答案显示为浮点数?

谢谢

1 个答案:

答案 0 :(得分:2)

除非您明确声明语义值类型,否则bison / yacc假定语义值的类型为int。在flex文件中声明yylval不会更改任何内容,因为bison从未看到该文件。 (不过,由于yylval最终被声明为两种不同的类型,因此导致行为未定义。我希望编译器对此有所抱怨。)

您可以像这样在野牛文件中声明语义值类型:

%define api.value.type {double}

(我使用double是因为几乎可以肯定它是您想要的; float是一种低精度的数据类型,只有在有充分理由的情况下才应使用。)

您还应该从flex文件中删除yylval的声明,因为它将在bison生成的头文件中声明。

有关更多详细信息和代码示例,请参见bison manual