Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <math.h>
- #include "p3.tab.h"
- double ctg(double x) { return 1/tan(x); }
- double sqr(double x) { return x*x; }
- %}
- %%
- [ \r\t] ;
- [0-9]+ { yylval.v = atoi(yytext); return NUMBER; }
- ([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+) { yylval.v = atof(yytext); return NUMBER; }
- M[0-9]{1,3} { yylval.m = atoi(yytext+1); return MEM; }
- sin { yylval.f = &sin; return FUNC; }
- cos { yylval.f = &cos; return FUNC; }
- tg { yylval.f = &tan; return FUNC; }
- ctg { yylval.f = &ctg; return FUNC; }
- sqrt { yylval.f = &sqrt; return FUNC; }
- sqr { yylval.f = &sqr; return FUNC; }
- "==" { return EQ; }
- "<=" { return LE; }
- ">=" { return GE; }
- "<>" { return NEQ; }
- (exit)|(bye)|(quit) { return EXIT; }
- \n { return EOL; }
- . { return yytext[0]; }
- ----------------------
- %{
- #define YYDEBUG 0
- #include <stdlib.h>
- #include <stdio.h>
- double mem[1000]={0};
- %}
- %union {
- double v;
- int m;
- double(*f)(double);
- }
- %token<v> NUMBER
- %token<m> MEM
- %token EOL
- %token<f> FUNC
- %token EXIT
- %nonassoc EQ LE GE NEQ '<' '>'
- %left '+' '-'
- %left '*' '/'
- %right '='
- %right NEG
- %type<v> Expr Exprs
- %%
- Lines: Line { printf("> "); }
- | Lines Line { printf("> "); }
- ;
- Line: Exprs EOL { printf("= %lf\n",$1); }
- | EXIT EOL { exit(0); }
- | error EOL
- | EOL
- ;
- Exprs: Expr
- | Exprs Expr
- | MEM '=' Expr { $$ = mem[$1] = $3; }
- ;
- Expr: FUNC '(' Expr ')' { $$ = $1($3); }
- | MEM { $$ = mem[$1]; }
- | '(' Expr ')' { $$ = $2; }
- | Expr '+' Expr { $$ = $1 + $3; }
- | Expr '-' Expr { $$ = $1 - $3; }
- | Expr '/' Expr { $$ = $1 / $3; }
- | '-' Expr { $$ = -$2; }
- | Expr EQ Expr { $$ = $1 == $3; }
- | Expr LE Expr { $$ = $1 <= $3; }
- | Expr GE Expr { $$ = $1 >= $3; }
- | Expr NEQ Expr { $$ = $1 != $3; }
- | Expr '>' Expr { $$ = $1 > $3; }
- | Expr '<' Expr { $$ = $1 < $3; }
- | NUMBER { $$ = $1; }
- ;
- %%
- int main()
- {
- #if YYDEBUG
- yydebug=1;
- #endif
- printf("uber kalkulator!\n> ");
- yyparse();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement