fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int lineno = 1;
  6. %}
  7.  
  8. %%
  9. "int" { printf("Keyword: %s\n", yytext); }
  10. "float" { printf("Keyword: %s\n", yytext); }
  11. "if" { printf("Keyword: %s\n", yytext); }
  12. "else" { printf("Keyword: %s\n", yytext); }
  13. "return" { printf("Keyword: %s\n", yytext); }
  14.  
  15. [0-9]+ { printf("Constant: %s\n", yytext); }
  16. [a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
  17.  
  18. "==" { printf("Operator: %s\n", yytext); }
  19. "=" { printf("Operator: %s\n", yytext); }
  20. "+" { printf("Operator: %s\n", yytext); }
  21. "-" { printf("Operator: %s\n", yytext); }
  22. "*" { printf("Operator: %s\n", yytext); }
  23. "/" { printf("Operator: %s\n", yytext); }
  24.  
  25. ";" { printf("Delimiter: %s\n", yytext); }
  26. "," { printf("Delimiter: %s\n", yytext); }
  27. "(" { printf("Delimiter: %s\n", yytext); }
  28. ")" { printf("Delimiter: %s\n", yytext); }
  29. "{" { printf("Delimiter: %s\n", yytext); }
  30. "}" { printf("Delimiter: %s\n", yytext); }
  31.  
  32. [\n] { lineno++; }
  33. [ \t] { /* skip whitespace */ }
  34. . { printf("Unknown token: %s\n", yytext); }
  35. %%
  36.  
  37. int main() {
  38. yylex();
  39. return 0;
  40. }
  41.  
  42. int yywrap() {
  43. return 1;
  44. }
  45.  
Success #stdin #stdout #stderr 0.03s 6892KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/h1ixin/prog:2:1: Syntax error: Operator expected
ERROR: /home/h1ixin/prog:44:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit