%{ #include #include "calculator.h" int yylex (void); int yyerror (char *); %} %language "C" %union { char * svalue; double dvalue; class Expression * evalue; } %token ENDLINE %token DOUBLE %token INTEGER %token MATHPI %token FCESIN %token FCECOS %token FCEEXP %token FCELOG %token IDENT %token TEXT %left '+' '-' %left '*' '/' %left '^' %type ENDLINE DOUBLE INTEGER IDENT line lines %type expr %type constnt %start input %% input: /* empty */ | lines ; lines: line | lines line ; line: ENDLINE | IDENT '=' expr ENDLINE { addVariable ($1, $3->eval()); free ($1); } | IDENT '=' expr ',' expr ',' expr ENDLINE { addRange ($1, $3->eval(), $5->eval(), $7->eval()); free ($1); } | expr ENDLINE { addExpression ($1); } | error ENDLINE { printf ("what:%s\n", $$); } ; constnt: DOUBLE { $$ = str_to_double ($1); free ($1); } | INTEGER { $$ = str_to_long ($1); free ($1); } | MATHPI { $$ = 3.14159265359; } ; expr: constnt { $$ = new Constant ($1); } | IDENT { $$ = new Variable ($1); free ($1); } | '-' expr { $$ = new Unary ($2, UNARY_MINUS); } | expr '+' expr { $$ = new Binary ($1, $3, BINARY_PLUS); } | expr '-' expr { $$ = new Binary ($1, $3, BINARY_MINUS);} | expr '*' expr { $$ = new Binary ($1, $3, BINARY_MULT); } | expr '/' expr { $$ = new Binary ($1, $3, BINARY_DIV); } | expr '^' expr { $$ = new Binary ($1, $3, BINARY_POW); } | '(' expr ')' { $$ = $2; } | FCESIN '(' expr ')' { $$ = new Unary ($3, UNARY_SIN); } | FCECOS '(' expr ')' { $$ = new Unary ($3, UNARY_COS); } | FCEEXP '(' expr ')' { $$ = new Unary ($3, UNARY_EXP); } | FCELOG '(' expr ')' { $$ = new Unary ($3, UNARY_LOG); } ; %% int yyerror(char * err) { printf("Error:\"%s\"\n", err); return 0; }