[text] github

Viewer

  1. %{
  2.     int id_count = 0, keyword_count = 0, operator_count = 0, number_count = 0;
  3. %}
  4.  
  5. identifier  [a-zA-Z_][a-zA-Z0-9_]*
  6. keyword     "auto"|"break"|"case"|"char"|"const"|"continue"|"default"|"do"|"double"|"else"|"enum"|"extern"|"float"|"for"|"goto"|"if"|"int"|"long"|"register"|"return"|"short"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"void"|"volatile"|"while"
  7. operator    "+"|"-"|"*"|"/"|"="|"=="|"!="|"<"|">"|"<="|">="|"&&"|"||"|"!"|"++"|"--"|"&"|"|"|"^"|"~"|"<<"|">>"|"["|"]"|"("|")"|";"|","|"."|"->"|"?"|":"|"{"|"}"
  8. number      [0-9]+
  9.  
  10. %%
  11.  
  12. {identifier}   { id_count++; }
  13. {keyword}      { keyword_count++; }
  14. {operator}     { operator_count++; }
  15. {number}       { number_count++; }
  16. .|\n           { /* Ignore other characters */ }
  17.  
  18. %%
  19.  
  20. int main() {
  21.     yylex();
  22.     printf("Identifiers: %d\n", id_count);
  23.     printf("Keywords: %d\n", keyword_count);
  24.     printf("Operators: %d\n", operator_count);
  25.     printf("Numbers: %d\n", number_count);
  26.     return 0;
  27. }
  28.  
  29. int yywrap() {
  30.     return 1;
  31. }

Editor

You can edit this paste and save as new:


File Description
  • github
  • Paste Code
  • 26 Apr-2024
  • 1.03 Kb
You can Share it: