fork download
  1. %{
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #define YYSTYPE double
  5. int yylex(void);
  6. void yyerror(const char *s) { fprintf(stderr, "error: %s\n", s); }
  7. %}
  8.  
  9. %token NUMBER
  10. %left '+' '-'
  11. %left '*' '/'
  12. %right UMINUS
  13.  
  14. %%
  15. lines : lines expr '\n' { printf("%g\n", $2); }
  16. | lines '\n'
  17. | /* empty */
  18. ;
  19. expr : expr '+' expr { $$ = $1 + $3; }
  20. | expr '-' expr { $$ = $1 - $3; }
  21. | expr '*' expr { $$ = $1 * $3; }
  22. | expr '/' expr { $$ = $1 / $3; }
  23. | '-' expr %prec UMINUS { $$ = -$2; }
  24. | '(' expr ')' { $$ = $2; }
  25. ;
  26. %%
  27.  
  28. int yylex(void)
  29. {
  30. int c;
  31. /* skip spaces and tabs */
  32. while ((c = getchar()) == ' ' || c == '\t')
  33. ;
  34. if (c == EOF) return 0;
  35. if (c == '.' || isdigit(c)) {
  36. ungetc(c, stdin);
  37. if (scanf("%lf", &yylval) != 1) return 0;
  38. return NUMBER;
  39. }
  40. return c; /* return character as token (e.g., '+', '\n', etc.) */
  41. }
  42.  
Success #stdin #stdout #stderr 0.03s 6908KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/r7CPSf/prog:42:1: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit