fork download
  1. Lex Program: cal.l
  2. %{
  3. #include"y.tab.h"
  4. #include<stdio.h>
  5. %}
  6. %%
  7. [0-9]+ { yylval.d=atof(yytext);
  8. return DIGIT;}
  9. \n | . {return yytext[0];}
  10. %%
  11. cal.y
  12. %{
  13.  
  14. #include<stdio.h>
  15. #include<math.h>
  16. %}
  17. %union
  18. {
  19. double d;
  20. }
  21. %token <d> DIGIT;
  22. %type <d> E;
  23. %type <d> T;
  24. %type <d> F;
  25. %left <d> '+''-';
  26. %left <d> '*''/';
  27. %%
  28. start: E'\n'{printf("Answer is%g\n",$1);exit(0);}
  29. ;
  30. E: E'+'T {$$=$1+$3;}
  31. | E'-'T {$$=$1-$3;}
  32. | T{$$=$1;}
  33. ;
  34. T: T'*'F{$$=$1*$3;}
  35. | T'/'F{$$=$1/$3;}
  36. | F{$$=$1;}
  37. ;
  38. F: '('E')'{$$=$2;}
  39. | DIGIT{$$=$1;}
  40. ;
  41. %%
  42. int main()
  43. {
  44. printf("Enter an expression");
  45. yyparse();
  46. }
  47. int yyerror(char *error)
  48. {
  49. printf("%s",error);
  50. }
Success #stdin #stdout #stderr 0.03s 6940KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/c7pJVc/prog:1:4: Syntax error: Operator expected
ERROR: /home/c7pJVc/prog:50:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit