fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. extern int yylex();
  6. int yyerror(const char *s);
  7. %}
  8.  
  9. %token NUMBER ID
  10. %left '+' '-'
  11. %left '%' '*' '/'
  12. %left '(' ')'
  13.  
  14. %%
  15. expr:
  16. expr '+' expr { printf("Addition\n"); }
  17. | expr '-' expr { printf("Subtraction\n"); }
  18. | expr '*' expr { printf("Multiplication\n"); }
  19. | expr '/' expr { printf("Division\n"); }
  20. | expr '%' expr { printf("Modulo\n"); }
  21. | '-' NUMBER { printf("Negative Number\n"); }
  22. | '-' ID { printf("Negative Identifier\n"); }
  23. | '(' expr ')' { /* Parentheses handling */ }
  24. | ID
  25. ;
  26.  
  27. %%
  28.  
  29. // Main function
  30. int main() {
  31. printf("Enter the expression:\n");
  32. yyparse();
  33. printf("\nExpression is valid\n");
  34. return 0;
  35. }
  36.  
  37. // Error handling function
  38. int yyerror(const char *s) {
  39. printf("\nExpression is invalid\n");
  40. exit(1);
  41. }
Success #stdin #stdout #stderr 0.03s 6932KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/fRcfNR/prog:42:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit