%{ #include #include int yylex(); FILE *yyin; #define YYDEBUG 1 %} %token NUM %% E : E '+' T | E '-' T | T ; T : T '*' G | T '/' G | G ; G : F '^' G | F ; F : NUM | '(' E ')' ; %% int yylex() { int c = fgetc(yyin); switch(c) { case '+': case '-': case '*': case '/': case '^': case '(': case ')': return c; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': while(isdigit(c)) c=fgetc(yyin); ungetc(c, yyin); return NUM; case EOF: return 0; case ' ': case '\t': case '\n': return yylex(); default: { fprintf(stderr, "lexical error: %d '%c'\n", c, c); exit(1); } } } int yyerror(char*s) { fprintf(stderr, "%s\n", s); exit(1); }