带十进制数的flex输出问题

问题描述 投票:0回答:1

我在Flex和Bison中编写了一个小型浮动数字计算器。到目前为止,我的代码如下:Flex代码

%{
# 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);
}

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

谢谢

bison flex-lexer
1个回答
2
投票

除非您明确声明语义值类型,否则bison / yacc假定语义值具有类型int。在flex文件中声明yylval不会改变任何内容,因为bison永远不会看到该文件。 (但是,它会导致未定义的行为,因为yylval最终会被声明为两种不同的类型。我会期望编译器会抱怨它。)

您可以在bison文件中声明一个语义值类型,如下所示:

%define api.value.type {double}

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.