%{#include <stdlib.h>#include "entint.tab.h"extern long yylval;%}D [0-9]N [1-9]O [0-7]H [0-9a-fA-F]%%"+" {return(ADDOP);}"-" {return(SUBOP);}"*" {return(MULOP);}"/" {return(DIVOP);}"**" {return(POWOP);}"!" {return(FACTOP);}"(" {return(LP);}")" {return(RP);}end {return(END);}{N}{D}* |0{O}* |0(X|x){H}+ {//将字符串??成?整型数 yylval = strtol((char*)yytext,(char**)NULL, 0); return(NUMBER); }[T] ;n {return(NL);}. {return(yytext[0]);} %{#include <stdio.h>#define YYSTYPE long%} %token NUMBER%token NL%token END%token LP%token RP%left ADDOP SUBOP%left MULOP DIVOP%right POWOP%right FACTOP%right UMINUS%start s%%s :list {printf("bye-bye... ");} ; list :/*empty*/ {printf("welcome to ecalc. ");} |list line {printf("end expression or end... ");} ;line :expr NL {printf("%ld,0x%lx,0%lo ",$1,$1,$1);} |END NL { printf("bye-bye... "); YYACCEPT; } |error NL ;expr :expr ADDOP expr {$$ = $1 + $3;} |expr SUBOP expr {$$ = $1 - $3;} |expr MULOP expr {$$ = $1 * $3;} |expr DIVOP expr { if ($3 == 0){ printf("zero divide "); YYERROR; } $$ = $1 / $3; } |expr POWOP expr { if ($3 < 0){ printf("POWOP divide "); YYERROR; } $$ = lpower($1,$3); } |expr FACTOP { if ($1 < 0){ printf("FACTOP divide "); YYERROR; } lfact($1); } |SUBOP expr %prec UMINUS {$$ = -$2;} |LP expr RP {$$ = $2;} |NUMBER {$$ = $1;} ;%%int yyerror(char *str){ printf("%s ", str);} long lpower(long x, long y){ long i ,pw; if (x == 0){ return 0; } else{ pw = 1; for (i = y;i;--i){ pw *= i; } return (pw); }}long lfact(long x){ long i,fc; for (fc = 1,i = x;i;--i){ fc *= i; } return (fc);}