fork download
  1. %option noyywrap
  2.  
  3. %{
  4. #include <stdio.h>
  5.  
  6. // Start condition states
  7. %}
  8.  
  9. %x PREINC PREDEC POSTINC POSTDEC
  10.  
  11. DIGIT [0-9]
  12. ID [a-zA-Z_][a-zA-Z0-9_]*
  13. KEYWORD "for"|"int"|"float"|"char"|"if"|"else"|"while"|"return"|"void"
  14. PUNCT ";"|"{"|"}"|","
  15. INT_CONST {DIGIT}+
  16. FLOAT_CONST {DIGIT}+"."{DIGIT}+
  17. STR_LITERAL \"([^\\\"]|\\.)*\"
  18. CHAR_LITERAL \'([^\\\']|\\.)\'
  19. PARANTHESIS "("
  20. CLOS ")"
  21.  
  22. %%
  23.  
  24. {KEYWORD} { printf("Keyword: %s\n", yytext); }
  25.  
  26. "++" { BEGIN(PREINC); return 0; }
  27. "--" { BEGIN(PREDEC); return 0; }
  28. {ID}"++" { printf("Post Increment: %s\n", yytext); return 0; }
  29. {ID}"--" { printf("Post Decrement: %s\n", yytext); return 0; }
  30.  
  31. <PREINC>{ID} { printf("Pre Increment: ++%s\n", yytext); BEGIN(INITIAL); return 0; }
  32. <PREDEC>{ID} { printf("Pre Decrement: --%s\n", yytext); BEGIN(INITIAL); return 0; }
  33.  
  34. {FLOAT_CONST} { printf("Float Constant: %s\n", yytext); }
  35. {INT_CONST} { printf("Integer Constant: %s\n", yytext); }
  36. {STR_LITERAL} { printf("String Literal: %s\n", yytext); }
  37. {CHAR_LITERAL} { printf("Character Literal: %s\n", yytext); }
  38.  
  39. "=="|"!="|"="|"<="|">="|"<"|">" { printf("Relational Operator: %s\n", yytext); }
  40. "*"|"/"|"+"|"-" { printf("Arithmetic Operator: %s\n", yytext); }
  41.  
  42. {PUNCT} { printf("Punctuator: %s\n", yytext); }
  43. {ID} { printf("Identifier: %s\n", yytext); }
  44. [ \t\n] ; // ignore whitespaces
  45.  
  46. . { printf("Unrecognized: %s\n", yytext); }
  47.  
  48. %%
  49.  
  50. int main() {
  51. printf("Enter C-style code:\n");
  52. yylex();
  53. return 0;
  54. }
  55.  
Success #stdin #stdout #stderr 0.03s 6932KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/0LaGds/prog:4:1: Syntax error: Operator expected
ERROR: /home/0LaGds/prog:54:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit