flex-bison-in-action/analyzers/polynomials/polynomials.y

38 lines
705 B
Plaintext

%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void yyerror(const char *s);
int yylex();
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%left UMINUS
%right '^'
%%
input:
| input expr { printf("= %d\n", $2); }
;
expr: NUMBER { $$ = $1; }
| '-' expr %prec UMINUS { $$ = -$2; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| expr '^' expr { $$ = pow($1, $3); }
| '(' expr ')' { $$ = $2; }
;
%%
void yyerror(const char *s) {
fprintf(stderr, "ERROR: %s\n", s);
}
int main() {
yyparse();
return 0;
}