fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int line_number = 1, token_number = 0;
  6. #define PRINT_TOKEN(type) printf("%d: TOKEN: %s, VALUE: %s\n", line_number, type, yytext); token_number++;
  7. %}
  8.  
  9. %option noyywrap
  10.  
  11. %%
  12. [ \t]+ ;
  13. \n { line_number++; }
  14. "if"|"else"|"while"|"return"|"int"|"float"|"void" { PRINT_TOKEN("KEYWORD") }
  15. [0-9]+ { PRINT_TOKEN("NUMBER") }
  16. [a-zA-Z_][a-zA-Z0-9_]* { PRINT_TOKEN("IDENTIFIER") }
  17. [+\-*/=;()] { PRINT_TOKEN("OPERATOR") }
  18. . { PRINT_TOKEN("SPECIAL") }
  19.  
  20. %%
  21.  
  22. int main(int argc, char **argv) {
  23. printf("Enter LOC: ");
  24. yylex();
  25. printf("Token count: %d\n", token_number);
  26. printf("LOC count: %d\n", line_number - 1);
  27. return 0;
  28. }
  29.  
Success #stdin #stdout #stderr 0.03s 6764KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/zObT7I/prog:2:1: Syntax error: Operator expected
ERROR: /home/zObT7I/prog:28:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit