在bison和flex中,的小数点后的数字替换为0
我是bison和flex的新手。我假设a=12.34分配浮点值。但是当我打印值时,我发现12.000000。请帮助我。提前谢谢。lex文件非常好地打印浮点值。我在代码的不同位置打印值,这是可以的。但是当在变量中分配值时,问题就出现了,然后替换后面的值小数点(按) 这是我的野牛档案在bison和flex中,的小数点后的数字替换为0,bison,flex-lexer,yacc,Bison,Flex Lexer,Yacc,我是bison和flex的新手。我假设a=12.34分配浮点值。但是当我打印值时,我发现12.000000。请帮助我。提前谢谢。lex文件非常好地打印浮点值。我在代码的不同位置打印值,这是可以的。但是当在变量中分配值时,问题就出现了,然后替换后面的值小数点(按) 这是我的野牛档案 /*C declarations (types, variables, functions, preprocessor commands)*/ %{ #include<stdio
/*C declarations (types, variables, functions, preprocessor commands)*/
%{
#include<stdio.h>
# include <stdlib.h>
# include <stdarg.h>
# include <string.h>
# include <math.h>
int yylex(void);
int check[100],int_val[1000],var_type[1000];
float float_val[1000];
char char_val[1000];
int caseD,caseV;
%}
/* Bison declarations (grammar symbols, operator precedence decl., attribute data type) */
%error-verbose
%union{
int intVal;
char* variable;
char* strVal;
float floatVal;
char charVal;
}
%token Begin INTEGER FLOAT CHAR END COLON SEMICOLON ASSIGN COMMA FORWARD_ARROW SHOW
%token BACKWARD_ARROW TAKE PLUS SUB MULT DIV MOD OTB CTB POW SIN COS TAN LOG LOG10 OP CP
%token LESS_THAN GREATER_THAN LESS_EQUAL GREATER_EQUAL IF ELSE_IF ELSE EQUAL NOT_EQUAL INC
%token DEC FOR IN FOR_INC FOR_DEC EVEN_ODD FACTORIAL SUM GCD LCM SWITCH
%token DEFAULT WHILE OSB CSB
%token<intVal>INT_VAL
%token<floatVal>FLOAT_VAL
%token<charVal>CHAR_VAL
%token<intVal>VARIABLE
%token<strVal>STRING_VAL
%type<intVal>expression statements cdeclare condition cstatements number
%%
program:Begin COLON cstatements END {printf("\n\t\t\t\t\tCompilation Done Succesfully!!!\n");
}
;
cstatements:/* Empty */ {}
|SEMICOLON {printf("\n\t\t\t\tEmpty statement\n");}
|cstatements cdeclare SEMICOLON
|cstatements statements
;
cdeclare:FLOAT float_id {}
;
float_id:float_id1 COMMA float_id
|float_id1
;
float_id1:VARIABLE {
if(check[$1]==1)
{
printf("\n\t\t\t\tCompilation error-> %c is redeclared \n",$1+97);
}
else{
var_type[$1]=1;
check[$1]=1;
printf("\n\t\t\t\t%c is declared successfully\n",$1+97);
}
}
|VARIABLE ASSIGN expression {
if(check[$1]==1)
{
printf("\n\t\t\t\tCompilation error-> %c is redeclared \n",$1+97);
}
else{
var_type[$1]=1;
check[$1]=1;
float_val[$1]=$3;
printf("\n\t\t\t\t%c is declared and assigned by %f successfully\n",$1+97,(float)($3));
}
}
;
statements:SHOW FORWARD_ARROW VARIABLE SEMICOLON {
if(check[$3] == 1)
{
if(var_type[$3]==0)
printf("\n\t\t\t\tValue of %c is: %d\n",$3+97,int_val[$3]);
else if(var_type[$3]==1)
printf("\n\t\t\t\tValue of %c is: %f\n",$3+97,float_val[$3]);
else if(var_type[$3]==2)
printf("\n\t\t\t\tValue of %c is: %c\n",$3+97,char_val[$3]);
}
else
{
printf("\n\t\t\t\tCompilation error-> %c was not declared\n",$3+97);
}
}
|VARIABLE ASSIGN expression SEMICOLON {
if(check[$1] == 1)
{
if(var_type[$1]==0)
{
int_val[$1]=$3;
printf("\n\t\t\t\t%c is assigned by %d successfully\n",$1+97,$3);
}
else if(var_type[$1]==1)
{
float_val[$1]=$3;
printf("\n\t\t\t\t%c is assigned by %f successfully\n",$1+97,(float)($3));
}
else if(var_type[$1]==2)
{
float_val[$1]=$3;
printf("\n\t\t\t\t%c is assigned by %c successfully\n",$1+97,$3);
}
}
else
{
printf("\n\t\tCompilation error-> %c was not declared\n",$1+97);
}
}
;
expression: number{ $$ = $1;
//printf("%f\n",$1);
}
number:INT_VAL {$$=$1;}
|FLOAT_VAL {$$=$1;
//printf("%f\n",$1);
}
|CHAR_VAL {$$=$1;}
;
%%
int yywrap(){
return 1;
}
int yyerror(char *s)
{
fprintf(stderr,"%s\n",s);
}
int main()
{
freopen("input.txt","r",stdin);
yyparse();
}
/*C声明(类型、变量、函数、预处理器命令)*/
%{
#包括
#包括
#包括
#包括
#包括
int yylex(无效);
整数检查[100],整数值[1000],变量类型[1000];
浮动值[1000];
char_val[1000];
int caseD,caseV;
%}
/*Bison声明(语法符号、运算符优先级声明、属性数据类型)*/
%错误详细
%联合{
int intVal;
char*变量;
char*strVal;
浮球浮球;
查尔瓦尔;
}
%标记开始整数浮点字符结束冒号分号分配逗号前进箭头显示
%令牌后向箭头取加子多分区MOD OTB CTB POW SIN COS TAN LOG 10 OP CP
%令牌小于大于小于等于大于等于等于大于等于如果否则如果否则等于不等于INC
%令牌DEC FOR IN FOR_INC FOR_DEC奇偶阶乘和GCD LCM交换机
%OSB CSB时的令牌默认值
%代币
%代币
%标记字符
%标记变量
%标记字符串
%类型表达式语句cdeclare条件cstatements编号
%%
程序:开始冒号cstatements结束{printf(“\n\t\t\t\t编译成功!!!\n”);
}
;
cstatements:/*空*/{}
|分号{printf(“\n\t\t\t\testy语句\n”);}
|cstatements是分号
|C声明声明
;
cdeclare:FLOAT_id{}
;
float\u id:float\u id1逗号float\u id
|浮点数id1
;
float_id1:变量{
如果(勾选[$1]==1)
{
printf(“\n\t\t\t\t编译错误->%c已重新声明”\n“,$1+97);
}
否则{
var_类型[$1]=1;
支票[$1]=1;
printf(“\n\t\t\t\t%c已成功声明”\n“,$1+97);
}
}
|变量赋值表达式{
如果(勾选[$1]==1)
{
printf(“\n\t\t\t\t编译错误->%c已重新声明”\n“,$1+97);
}
否则{
var_类型[$1]=1;
支票[$1]=1;
浮动值[$1]=$3;
printf(“\n\t\t\t\t%c已由%f成功声明和分配”,$1+97,(float)($3));
}
}
;
语句:显示前进箭头变量分号{
如果(勾选[$3]==1)
{
如果(变量类型[$3]==0)
printf(“\n\t\t\t\t%c的值为:%d\n”,$3+97,整数[$3]);
else if(变量类型[$3]==1)
printf(“\n\t\t\t\t%c的值为:%f\n”,$3+97,浮点值[$3]);
否则如果(变量类型[$3]==2)
printf(“\n\t\t\t\t%c的值为:%c\n”,$3+97,字符值[$3]);
}
其他的
{
printf(“\n\t\t\t\t编译错误->%c未声明”\n“,$3+97);
}
}
|变量赋值表达式分号{
如果(勾选[$1]==1)
{
如果(变量类型[$1]==0)
{
整数[$1]=$3;
printf(“\n\t\t\t\t%c被%d成功分配\n”,$1+97,$3);
%type<intVal>expression … number