fork download
  1. %{
  2. #include<stdio.h> #include<ctype.h> #define YYSTYPE double
  3. %}
  4. %token num
  5. %left '+' '-'
  6. %left '*' '/'
  7. %right UMINUS
  8. %%
  9. lines:
  10. |lines exp '\n' {printf("%g\n",$2); return 1;}
  11. |lines '\n'
  12. ;
  13. exp:exp'+'exp {$$=$1+$3;}
  14. |exp'-'exp {$$=$1-$3;}
  15. |exp'*'exp {$$=$1*$3;}
  16. |exp'/'exp {if($3==0)
  17. {
  18. printf("Divide by zero error\n"); exit(0);
  19. }
  20. $$=$1/$3;
  21. }
  22. |'('exp')' {$$=$2;}
  23. |'-'exp {$$=-$2;}
  24. |num;
  25. %%
  26. int yylex()
  27. {
  28. int c; while((c=getchar())==' ');
  29. if((c=='.')||(isdigit(c)))
  30. {
  31. ungetc(c,stdin); scanf("%lf",&yylval); return num;
  32. }
  33. return c;
  34. }
  35. int main()
  36. {
  37. yyparse(); return 0;
  38. }
  39. int yyerror()
  40. {
  41. printf("Error\n"); return 0;
  42. }
Success #stdin #stdout #stderr 0.02s 6896KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/gh9gIc/prog:42:0: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit