fork download
  1. %{
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. int result=0;
  5. int curr=0;
  6. char op='+';
  7. void apply_operation();
  8. %}
  9. %option noyywrap
  10. digit [0-9]+
  11. space [ \t]
  12. %%
  13. {space};
  14. {digit}+ {
  15. curr=atoi(yytext);
  16. apply_operation();
  17. }
  18. "+" {op='+';}
  19. "-" {op='-';}
  20. "*" {op='*';}
  21. "/" {op='/';}
  22. \n {
  23. printf("Result:%d",result);
  24. result=0;
  25. curr=0;
  26. op='+';
  27. }
  28. . {printf("Invalid character\n");
  29. exit(1);
  30. }
  31. %%
  32. void apply_operation(){
  33. switch(op){
  34. case '+': result+=curr;break;
  35. case '-': result-=curr;break;
  36. case '*':result*=curr;break;
  37. case '/':{
  38. if(curr==0){
  39. printf("Error division by 0\n");
  40. exit(1);
  41. }
  42. result/=curr;break;
  43. }
  44.  
  45. }
  46. }
  47. int main(){
  48. printf("Enter expression: ");
  49. yylex();
  50. return 0;
  51. }
Success #stdin #stdout #stderr 0.02s 7004KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/2Ci9jL/prog:2:3: Syntax error: Operator expected
ERROR: /home/2Ci9jL/prog:51:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit