fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. %}
  5.  
  6. %%
  7.  
  8. // Define tokens
  9. [0-9]+ { printf("NUMBER: %s\n", yytext); }
  10. [ \t\n]+ { /* ignore whitespace */ }
  11. "if" { printf("KEYWORD: %s\n", yytext); }
  12. "else" { printf("KEYWORD: %s\n", yytext); }
  13. [a-zA-Z_][a-zA-Z0-9_]* { printf("IDENTIFIER: %s\n", yytext); }
  14. "+" { printf("OPERATOR: %s\n", yytext); }
  15. "-" { printf("OPERATOR: %s\n", yytext); }
  16. "*" { printf("OPERATOR: %s\n", yytext); }
  17. "/" { printf("OPERATOR: %s\n", yytext); }
  18. "=" { printf("ASSIGNMENT: %s\n", yytext); }
  19. ";" { printf("SEMICOLON: %s\n", yytext); }
  20. . { printf("UNKNOWN: %s\n", yytext); }
  21.  
  22. %%
  23.  
  24. // Main function
  25. int main(int argc, char **argv) {
  26. yylex();
  27. return 0;
  28. }
  29.  
  30. // Error handling
  31. void yyerror(const char *s) {
  32. fprintf(stderr, "Error: %s\n", s);
  33. }
Success #stdin #stdout #stderr 0.02s 6976KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Dctx2G/prog:2:1: Syntax error: Operator expected
ERROR: /home/Dctx2G/prog:33:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit