fork download
  1. %{
  2. /* Definition section */
  3. #include <stdio.h>
  4. %}
  5.  
  6. %token NUMBER ID
  7. // setting the precedence
  8. // and associativity of operators
  9. %left '+' '-'
  10. %left '*' '/'
  11.  
  12. /* Rule Section */
  13. %%
  14. E : T {
  15. printf("Result = %d\n", $$);
  16. return 0;
  17. }
  18.  
  19. T :
  20. T '+' T { $$ = $1 + $3; }
  21. | T '-' T { $$ = $1 - $3; }
  22. | T '*' T { $$ = $1 * $3; }
  23. | T '/' T { $$ = $1 / $3; }
  24. | '-' NUMBER { $$ = -$2; }
  25. | '-' ID { $$ = -$2; }
  26. | '(' T ')' { $$ = $2; }
  27. | NUMBER { $$ = $1; }
  28. | ID { $$ = $1; };
  29. % %
  30.  
  31. int main() {
  32. printf("Enter the expression\n");
  33. yyparse();
  34. }
  35.  
  36. /* For printing error messages */
  37. int yyerror(char* s) {
  38. printf("\nExpression is invalid\n");
  39. }
  40.  
Success #stdin #stdout #stderr 0.04s 6876KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/ofXmxa/prog:39:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit