fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. %}
  5. %%
  6. // Regular Expressions (token definitions)
  7. int [a-zA-Z_][a-zA-Z0-9_]*
  8. float [0-9]+\.[0-9]+
  9. digit [0-9]+
  10. keyword int|float|if|else|for|while|return
  11. %%
  12. // Rules and actions
  13. {keyword} {printf("Token: %s | Type: KEYWORD\n", yytext); }
  14. {int} {printf("Token: %s | Type: IDENTIFIER\n", yytext); }
  15. {float} {printf("Token: %s | Type: CONSTANT\n", yytext); }
  16. {digit} {printf("Token: %s | Type: CONSTANT\n", yytext); }
  17. "+"|"-"|"*"|"/" { printf("Token: %s | Type: OPERATOR\n", yytext); }
  18. "(" | ")" | ";" | "," { printf("Token: %s | Type: PUNCTUATION\n", yytext); }
  19. "//".* {printf("Token: %s | Type: COMMENT\n", yytext); }
  20. "/*".* "*/" {printf("Token: %s | Type: COMMENT\n", yytext); }
  21. [ \t\n\r]+ { /* Ignore whitespace */ }
  22.  
  23. . { printf("Token: %s | Type: UNKNOWN\n", yytext); }
  24. %%
  25.  
  26. int main() {
  27. yylex(); // Invoke the lexical analyzer
  28. return 0;
  29. }
  30.  
Success #stdin #stdout #stderr 0.03s 6992KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/yphyYi/prog:2:1: Syntax error: Operator expected
ERROR: /home/yphyYi/prog:29:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit