fork download
  1. %{
  2. #include<stdio.h>
  3. #include "y.tab.h"
  4. %}
  5. %%
  6. [a-zA-Z0-9]+ { return NUM; }
  7. [\n] return 0;
  8. . return yytext[0];
  9. %%
  10. int yywrap()
  11. {
  12. return 1;
  13. }
  14. PROGRAM-YACC
  15. %{
  16. #include<stdio.h>
  17. int flag=0;
  18. %}
  19. %token NUM
  20. %left '+''-'
  21. %left '*''/''%'
  22. %left '('')'
  23. %%
  24. ArithmeticExpression:E{ printf("\nResult=%d\n",$$);return 0;}
  25. ;
  26. E:E'+'E
  27. |E'-'E
  28. |E'*'E
  29. |E'/'E
  30. |E'%'E
  31. |'('E')'
  32. |NUM
  33. ;
  34. %%
  35. void main()
  36. {
  37. printf("\nEnter an arithmetic expression:\n");
  38. yyparse();
  39. if (flag==0)
  40. printf("\nEntered arithmetic expression is Valid\n\n");
  41. }
  42. void yyerror()
  43. {
  44. printf("\nInvalid expression\n");
  45. flag=1;
  46. }
Success #stdin #stdout #stderr 0.02s 6884KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/8TE6Ut/prog:2:1: Syntax error: Operator expected
ERROR: /home/8TE6Ut/prog:46:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit