fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. // Function prototype for yyerror
  7. void yyerror(const char *s);
  8. int yylex();
  9. int yywrap() { return 1; }
  10. %}
  11.  
  12. %%
  13. [0-9]+ {
  14. printf("Number: %s\n", yytext);
  15. return atoi(yytext);
  16. }
  17. [+\-*/] {
  18. printf("Operator: %s\n", yytext);
  19. return yytext[0];
  20. }
  21. \n { return 0; }
  22. [ \t] { /* Ignore whitespace */ }
  23. . { printf("Invalid character: %s\n", yytext); }
  24. %%
  25.  
  26. int main() {
  27. printf("Enter an arithmetic expression:\n");
  28. yylex();
  29. return 0;
  30. }
  31.  
  32. void yyerror(const char *s) {
  33. fprintf(stderr, "Error: %s\n", s);
  34. }
  35.  
Success #stdin #stdout #stderr 0.03s 6828KB
stdin
10 + 20 * 3
stdout
Standard output is empty
stderr
ERROR: /home/KQnBew/prog:2:1: Syntax error: Operator expected
ERROR: /home/KQnBew/prog:34:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ?    Exception: (3) program ? EOF: exit