fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. %}
  5.  
  6. %option noyywrap
  7.  
  8. %%
  9. "int" { printf("Keyword: int\n"); }
  10. "float" { printf("Keyword: float\n"); }
  11. "if" { printf("Keyword: if\n"); }
  12. "else" { printf("Keyword: else\n"); }
  13.  
  14. [0-9]+\.[0-9]+ { printf("Float: %s\n", yytext); }
  15. [0-9]+ { printf("Integer: %s\n", yytext); }
  16. [a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
  17.  
  18. "+" { printf("Operator: +\n"); }
  19. "-" { printf("Operator: -\n"); }
  20. "*" { printf("Operator: *\n"); }
  21. "/" { printf("Operator: /\n"); }
  22. "=" { printf("Assignment Operator: =\n"); }
  23. ";" { printf("Semicolon: ;\n"); }
  24. "," { printf("Comma: ,\n"); }
  25.  
  26. [\t\n ]+ { /* Ignore whitespace */ }
  27. . { printf("Unknown Character: %s\n", yytext); }
  28.  
  29. %%
  30.  
  31. int main() {
  32. printf("Enter code to analyze:\n");
  33. yylex();
  34. return 0;
  35. }
  36.  
Success #stdin #stdout #stderr 0.03s 6820KB
stdin
*
stdout
Standard output is empty
stderr
ERROR: /home/j07qpv/prog:2:5: Syntax error: Operator expected
ERROR: /home/j07qpv/prog:35:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? EOF: exit