fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. extern int yylex();
  5. extern int yylval;
  6. extern int yyparse();
  7. %}
  8.  
  9. %token NUMBER
  10.  
  11. %%
  12.  
  13. expr:
  14. expr '+' expr { $$ = $1 + $3; }
  15. | expr '-' expr { $$ = $1 - $3; }
  16. | '+' expr { $$ = $2; } // Unary plus does nothing, just returns the number
  17. | '-' expr { $$ = -$2; } // Unary minus negates the number
  18. | '*' expr { $$ = -$2; } // Treat * as unary minus
  19. | '/' expr { $$ = -$2; } // Treat / as unary minus
  20. | NUMBER { $$ = $1; }
  21. ;
  22.  
  23. %%
  24.  
  25. int main() {
  26. printf("Enter an expression: ");
  27. yyparse();
  28. return 0;
  29. }
  30.  
  31. int yyerror(const char *s) {
  32. fprintf(stderr, "%s\n", s);
  33. return 0;
  34. }
  35.  
Success #stdin #stdout #stderr 0.02s 6992KB
stdin
29-12 = 17
50+16 = 66
stdout
Standard output is empty
stderr
ERROR: /home/IpfTBg/prog:34:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ?    Exception: (3) program ? EOF: exit