我正在使用lex和yacc创建一个计算器,它显示乘法错误
这是我的yacc文件:sin.y
%{
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define YYSTYPE double
YYSTYPE last_value=0;
extern int yylex(void);
%}
%token NUM
%token LAST
%left '+' '-'
%left '*' '/'
%left '^'
%left NEG
%left COS EXP SIN SQRT TAN
%%
stmt:
|stmt '\n'
|stmt expr '\n' {printf("%.8g\n",last_value=$2);}
;
expr:text {$$=$1;}
|expr '+'expr{$$=$1+$3;}
|expr '-' expr {$$=$1-$3;}
|expr '*' expr {$$=$1*$3;}
|expr '/' expr {$$=$1/$3;}
|expr '^' expr {$$=pow($1,$3);}
|'-'expr %prec NEG {$$=-$2;}
|COS text {$$=cos($2*3.14/180);}
|EXP text{$$=exp($2);}
|SIN text{$$=sin($2*3.14/180);}
|SQRT text{$$=sqrt($2);}
|TAN text{$$=tan($2*3.14/180);}
;
text:NUM {$$=$1;}
|LAST{$$=last_value;}
|'(' expr ')' {$$=$2;}
;
%%
#include "unistd.h"
int ln;
char* fname="-stdin-";
int yyerror(const char *s)
{
fprintf(stderr,"%s(%d):%s\n",fname,ln,s);
exit(0);
}
main()
{
printf("ABILITIES OF CALCULATOR:\n");
printf("Sum MUL DIV SUB POWER COS SIN TAN SQRT \n");
printf("DO WHATEVER YOU WANT TO DO: \n");
yyparse();
}
和lex文件sin.l
%{
#include<stdio.h>
#include"y.tab.c"
#include<stdlib.h>
#include<unistd.h>
#include<math.h>
#define YYSTYPE double
extern ln;
extern YYSTYPE yylval;
%}
digit [0-9]
space [ \t]
%%
{space} {;}
{digit}+\.?|{digit}*\.{digit}+ {yylval=strtod(yytext,0);
return NUM; }
\*\* { return '^'; }
last { return LAST; }
cos { return COS; }
exp { return EXP; }
sin { return SIN; }
sqrt { return SQRT; }
tan { return TAN; }
pi { yylval = atan(1.0)*4;
return NUM; }
e { yylval = exp(1.0);
return NUM; }
\n { ln++; return '\n'; }
. { return yytext[0]; }
%%
它在编译时显示各种错误,例如
/tmp/ccd11cX5.o:(.bss+0x0): multiple definition of `last_value'
/tmp/ccO4t7Z8.o:(.bss+0x18): first defined here
/tmp/ccd11cX5.o: In function `yyparse':
y.tab.c:(.text+0x24): multiple definition of `yyparse'
/tmp/ccO4t7Z8.o:lex.yy.c:(.text+0x24): first defined here
/tmp/ccd11cX5.o: In function `yyerror':
y.tab.c:(.text+0xbb9): multiple definition of `yyerror'
/tmp/ccO4t7Z8.o:lex.yy.c:(.text+0xbb9): first defined here
/tmp/ccd11cX5.o:(.data.rel.local+0x0): multiple definition of `fname'
/tmp/ccO4t7Z8.o:(.data.rel.local+0x0): first defined here
/tmp/ccd11cX5.o: In function `main':
y.tab.c:(.text+0xbfe): multiple definition of `main'
/tmp/ccO4t7Z8.o:lex.yy.c:(.text+0xbfe): first defined here
/tmp/ccO4t7Z8.o: In function `yyparse':
lex.yy.c:(.text+0x60a): undefined reference to `pow'
lex.yy.c:(.text+0x667): undefined reference to `cos'
lex.yy.c:(.text+0x696): undefined reference to `exp'
lex.yy.c:(.text+0x6cf): undefined reference to `sin'
lex.yy.c:(.text+0x6fe): undefined reference to `sqrt'
lex.yy.c:(.text+0x734): undefined reference to `tan'
/tmp/ccd11cX5.o: In function `yyparse':
y.tab.c:(.text+0x60a): undefined reference to `pow'
y.tab.c:(.text+0x667): undefined reference to `cos'
y.tab.c:(.text+0x696): undefined reference to `exp'
y.tab.c:(.text+0x6cf): undefined reference to `sin'
y.tab.c:(.text+0x6fe): undefined reference to `sqrt'
y.tab.c:(.text+0x734): undefined reference to `tan'
collect2: error: ld returned 1 exit status