Advertisement
hejmus

Untitled

Nov 27th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include "p3.tab.h"
  5.  
  6. double ctg(double x) { return 1/tan(x); }
  7. double sqr(double x) { return x*x; }
  8. %}
  9. %%
  10. [ \r\t] ;
  11. [0-9]+                              { yylval.v = atoi(yytext);   return NUMBER; }
  12. ([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+)   { yylval.v = atof(yytext);   return NUMBER; }
  13. M[0-9]{1,3}                         { yylval.m = atoi(yytext+1); return MEM;    }
  14. sin                                 { yylval.f = &sin;           return FUNC;   }
  15. cos                                 { yylval.f = &cos;           return FUNC;   }
  16. tg                                  { yylval.f = &tan;           return FUNC;   }
  17. ctg                                 { yylval.f = &ctg;           return FUNC;   }
  18. sqrt                                { yylval.f = &sqrt;          return FUNC;   }
  19. sqr                                 { yylval.f = &sqr;           return FUNC;   }
  20. "=="                                { return EQ; }
  21. "<="                                { return LE; }
  22. ">="                                { return GE; }
  23. "<>"                                { return NEQ; }
  24. (exit)|(bye)|(quit)                 { return EXIT; }
  25. \n                                  { return EOL;       }
  26. .                                   { return yytext[0]; }
  27.  
  28. ----------------------
  29. %{
  30. #define YYDEBUG 0
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33.  
  34. double mem[1000]={0};
  35. %}
  36. %union {
  37. double v;
  38. int m;
  39. double(*f)(double);
  40. }
  41. %token<v> NUMBER
  42. %token<m> MEM
  43. %token EOL
  44. %token<f> FUNC
  45. %token EXIT
  46.  
  47. %nonassoc EQ LE GE NEQ '<' '>'
  48. %left '+' '-'
  49. %left '*' '/'
  50. %right '='
  51. %right NEG
  52.  
  53. %type<v> Expr Exprs
  54.  
  55. %%
  56.  
  57. Lines: Line                 { printf("> "); }
  58.      | Lines Line           { printf("> "); }
  59.      ;
  60.  
  61. Line: Exprs EOL             { printf("= %lf\n",$1); }
  62.     | EXIT EOL              { exit(0); }
  63.     | error EOL
  64.     | EOL
  65.     ;
  66.  
  67. Exprs: Expr
  68.      | Exprs Expr
  69.      | MEM '=' Expr         { $$ = mem[$1] = $3; }
  70.      ;
  71.  
  72. Expr: FUNC '(' Expr ')'     { $$ = $1($3);   }
  73.     | MEM                   { $$ = mem[$1];  }
  74.     | '(' Expr ')'          { $$ = $2;       }
  75.     | Expr '+' Expr         { $$ = $1 + $3;  }
  76.     | Expr '-' Expr         { $$ = $1 - $3;  }
  77.     | Expr '/' Expr         { $$ = $1 / $3;  }
  78.     | '-' Expr              { $$ = -$2;      }
  79.     | Expr EQ Expr          { $$ = $1 == $3; }
  80.     | Expr LE Expr          { $$ = $1 <= $3; }
  81.     | Expr GE Expr          { $$ = $1 >= $3; }
  82.     | Expr NEQ Expr         { $$ = $1 != $3; }
  83.     | Expr '>' Expr         { $$ = $1 > $3;  }
  84.     | Expr '<' Expr         { $$ = $1 < $3;  }
  85.     | NUMBER                { $$ = $1;       }
  86.     ;
  87.    
  88. %%
  89. int main()
  90. {
  91.     #if YYDEBUG
  92.         yydebug=1;
  93.     #endif
  94.     printf("uber kalkulator!\n> ");
  95.     yyparse();
  96.     return 0;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement