fork download
  1. /* Lex code to count total number of tokens */
  2. %{
  3. int n = 0;
  4. %}
  5.  
  6. // Rule section
  7. %%
  8. // Count number of keywords
  9. "while"|"if"|"else" { n++; printf("\t keywords: %s", yytext); }
  10.  
  11. // Count number of data types
  12. "int"|"float" { n++; printf("\t data type: %s", yytext); }
  13.  
  14. // Count number of identifiers
  15. [a-zA-Z_][a-zA-Z0-9_]* { n++; printf("\t identifier: %s", yytext); }
  16.  
  17. // Count number of operators
  18. "<="|"=="|"="|"++"|"-"|"*"|"+" { n++; printf("\t operator: %s", yytext); }
  19.  
  20. // Count number of separators
  21. [(){}|, ;] { n++; printf("\t separator: %s", yytext); }
  22.  
  23. // Count number of floats
  24. [0-9]*"."[0-9]+ { n++; printf("\t float: %s", yytext); }
  25.  
  26. // Count number of integers
  27. [0-9]+ { n++; printf("\t integer: %s", yytext); }
  28. . ;
  29.  
  30. %%
  31.  
  32. int main() {
  33. yylex();
  34. printf("\nTotal number of tokens = %d\n", n);
  35. }
  36.  
Success #stdin #stdout #stderr 0.02s 7004KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/o3sVnu/prog:3:4: Syntax error: Operator expected
ERROR: /home/o3sVnu/prog:35:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit