急问yacc一问题(高分)
nevil 2005-03-29 02:51:17 lex代码如下
%{
#include "y.tab.h"
%}
chars [A-Za-z]
number [0-9]+
end [\n]
words {chars}+
%%
[ \t] ;
{words} {yylval.sval = strdup(yytext);return NAME;}
{number} {yylval.dval = atof(yytext);return NUMBER;}
{end} {return 0;}
%%
yacc代码如下
%{
#include <stdio.h>
%}
%union{
double dval;
char * sval;
}
%token <sval>NAME
%token <dval>NUMBER
%left '-' '+'
%type <sval>statement
%type <dval>expression
%%
statement: NAME '=' expresson{printf("%s=%d"$1,$3);}
| expression {printf("=%f\n",$1);}
;
expression: NUMBER '+' NUMBER {$$=$1+$3;}
| NUMBER '-' NUMBER {$$=$1-$3;}
| NUMBER {$$=$1;}
;
%%
int main() {
yyparse();
return 0;
}
int yyerror(char* msg) {
printf("Error: %s\n", msg);
}
为何将执行的时候总是提示语法错语
输入"a=2+3" 提示"="语法错
输入"2+3" 提示"+"语法错