fork download
  1. %{
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include "y.tab.h"
  5. #include<math.h>
  6. %}
  7. %%
  8. "sin" { return SIN; }
  9. "cos" { return COS; }
  10. "tan" { return TAN; }
  11. "cosec" { return COSEC; }
  12. "sec" { return SEC; }
  13. "cot" { return COT; }
  14. "log" { return LOG; }
  15. "ln" { return LN; }
  16. "sqrt" { return SQRT; }
  17. "cube" { return CUBE; }
  18. "sa" { return SA; }
  19. [0-9]+ { yylval.p = atof(yytext); return NUM; }
  20. [ \t\n]+ ;
  21. . { return yytext[0]; }
  22. %%
  23. int yywrap() {
  24. return 1;
  25. }
  26.  
  27. %{
  28. #include<stdio.h>
  29. #include<stdlib.h>
  30. #include<math.h>
  31. %}
  32. %token SIN COS TAN COSEC SEC COT LOG LN SQRT CUBE SA NUM
  33. %union { double p; }
  34. %left '+' '-'
  35. %left '*' '/'
  36. %type <p> E NUM
  37. %%
  38. stmt: E { printf("Answer is %g\n", $1); }
  39. ;
  40. E: E '+' E { $$ = $1 + $3; }
  41. | E '-' E { $$ = $1 - $3; }
  42. | E '*' E { $$ = $1 * $3; }
  43. | E '/' E { $$ = $1 / $3; }
  44. | '(' E ')' { $$ = $2; }
  45. | SIN '(' E ')' { $$ = sin($3); }
  46. | COS '(' E ')' { $$ = cos($3); }
  47. | TAN '(' E ')' { $$ = tan($3); }
  48. | COSEC '(' E ')' { $$ = 1 / sin($3); }
  49. | SEC '(' E ')' { $$ = 1 / cos($3); }
  50. | COT '(' E ')' { $$ = 1 / tan($3); }
  51. | LOG '(' E ')' { $$ = log10($3); }
  52. | LN '(' E ')' { $$ = log($3); }
  53. | SQRT '(' E ')' { $$ = sqrt($3); }
  54. | SA '(' E ')' { $$ = 4 * M_PI * $3 * $3; }
  55. | CUBE '(' E ')' { $$ = $3 * $3 * $3; }
  56. | NUM { $$ = $1; }
  57. ;
  58. %%
  59. int main() {
  60. printf("Enter the string: ");
  61. yyparse();
  62. return 0;
  63. }
  64. void yyerror(char *s) {
  65. printf("Not accepted: %s\n", s);
  66. exit(0);
  67. }
  68.  
Success #stdin #stdout #stderr 0.02s 7016KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/JVh6La/prog:2:1: Syntax error: Operator expected
ERROR: /home/JVh6La/prog:67:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit