fork download
  1. %{
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7. %}
  8.  
  9.  
  10.  
  11. %%
  12.  
  13. [0-9]+ { yylval = atoi(yytext); return NUMBER; } // Recognize numbers
  14.  
  15. [-+*/\n] { return yytext[0]; } // Recognize operators and newline
  16.  
  17. [ \t] ; // Ignore whitespace
  18.  
  19. . { printf("Invalid character: %s\n", yytext); } // Handle invalid characters
  20.  
  21. %%
  22.  
  23.  
  24.  
  25. int main() {
  26.  
  27. int token;
  28.  
  29. while (1) {
  30.  
  31. token = yylex();
  32.  
  33. if (token == 0) {
  34.  
  35. break;
  36.  
  37. }
  38.  
  39. if (token == NUMBER) {
  40.  
  41. printf("Number: %d\n", yylval);
  42.  
  43. } else if (token == '+' || token == '-' || token == '*' || token == '/') {
  44.  
  45. printf("Operator: %c\n", token);
  46.  
  47. }
  48.  
  49. }
  50.  
  51. return 0;
  52.  
  53. }
  54.  
  55.  
Success #stdin #stdout #stderr 0.02s 6816KB
stdin
8-8
stdout
Standard output is empty
stderr
ERROR: /home/OWGQg0/prog:3:1: Syntax error: Operator expected
ERROR: /home/OWGQg0/prog:54:0: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit