|
楼主 |
发表于 2003-7-3 22:32:31
|
显示全部楼层
又出问题了,我想哭了!
- [sandy@localhost lex&yacc]$ gcc -o calc y.tab.c lex.yy.c -ll -lm
- /tmp/ccIGLRqu.o: In function `yyparse':
- /tmp/ccIGLRqu.o(.text+0x4d6): undefined reference to `yyerror'
- /tmp/ccIGLRqu.o(.text+0x637): undefined reference to `yyerror'
- /tmp/ccIGLRqu.o(.text+0x77b): undefined reference to `yyerror'
- /tmp/cchsnUMc.o: In function `yylex':
- /tmp/cchsnUMc.o(.text+0x34c): undefined reference to `yywrap'
- /tmp/cchsnUMc.o: In function `input':
- /tmp/cchsnUMc.o(.text+0xa8e): undefined reference to `yywrap'
- collect2: ld returned 1 exit status
复制代码
lex文件
%{
#include"y.tab.h"
#include<math.h>
extern double vbltable[26];
%}
%%
[ \t] ;
[0-9]+(\.[0-9]+)? {
yylval.dval=atof(yytext);
return NUMBER;}
[a-z] {yylval.vblno=yytext[0]-'a';
return NAME;}
"//".*\n ;
. |
\n return yytext[0];
%%
yacc文件
%{
extern double vbltable[26];
double vbltable[26];
%}
%union{
double dval;
int vblno;
}
%token <vblno> NAME
%token <dval> NUMBER
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS
%type <dval> exp
%%
input: stmt '\n'
| input stmt '\n'
;
stmt: NAME '=' exp {vbltable[$1]=$3;}
| exp {printf("=%g\n",$1);}
;
exp: exp '+' exp {$$=$1+$3;}
| exp '-' exp {$$=$1-$3;}
| exp '*' exp {$$=$1*$3;}
| exp '/' exp {if($3==0.0)yyerror("零不能作除数的嘛!");
else $$=$1/$3;}
| '-' exp %prec UMINUS {$$=-$2;}
| '(' exp ')' {$$=$2;}
| NUMBER
| NAME {$$=vbltable[$1];}
;
%% |
|