fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. %}
  5.  
  6. %%
  7.  
  8. // Patterns to recognize
  9. [ \t\n]+ ; // Ignore whitespace
  10. "int"|"float"|"char" { printf("Keyword: %s\n", yytext); }
  11. [a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
  12. [0-9]+ { printf("Integer: %s\n", yytext); }
  13. "+"|"-"|"*"|"/" { printf("Operator: %s\n", yytext); }
  14. "=" { printf("Assignment: %s\n", yytext); }
  15. "(" { printf("Delimiter: %s\n", yytext); }
  16. ")" { printf("Delimiter: %s\n", yytext); }
  17. "{" { printf("Delimiter: %s\n", yytext); }
  18. "}" { printf("Delimiter: %s\n", yytext); }
  19. "//".* ; // Ignore single line comments
  20. "/*".* "*/" ; // Ignore multi-line comments
  21. . { printf("Unknown: %s\n", yytext); }
  22.  
  23. %%
  24.  
  25. // Main function
  26. int main(int argc, char **argv) {
  27. yylex();
  28. return 0;
  29. }
  30.  
  31. // Error handling function
  32. void yyerror(const char *s) {
  33. fprintf(stderr, "Error: %s\n", s);
  34. }
  35.  
Success #stdin #stdout #stderr 0.03s 6964KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/mlDsxJ/prog:2:1: Syntax error: Operator expected
ERROR: /home/mlDsxJ/prog:34:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit