fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int keywords = 0, identifiers = 0, numbers = 0;
  6.  
  7. // List of C-like keywords
  8. char *keyword_list[] = {
  9. "int", "float", "char", "double", "if", "else",
  10. "while", "for", "return", "void", "break", "continue", NULL
  11. };
  12.  
  13. int is_keyword(const char *word) {
  14. for (int i = 0; keyword_list[i] != NULL; i++) {
  15. if (strcmp(word, keyword_list[i]) == 0)
  16. return 1;
  17. }
  18. return 0;
  19. }
  20. %}
  21.  
  22. %%
  23.  
  24. [a-zA-Z_][a-zA-Z0-9_]* {
  25. if (is_keyword(yytext))
  26. keywords++;
  27. else
  28. identifiers++;
  29. }
  30.  
  31. [0-9]+ { numbers++; }
  32.  
  33. [ \t\n]+ ; // Ignore whitespace
  34.  
  35. . ; // Ignore other characters
  36.  
  37. %%
  38.  
  39. int main() {
  40. printf("Enter C-like code (press Ctrl+D to end):\n");
  41. yylex();
  42. printf("\n--- Token Counts ---\n");
  43. printf("Keywords: %d\n", keywords);
  44. printf("Identifiers: %d\n", identifiers);
  45. printf("Numeric Constants: %d\n", numbers);
  46. return 0;
  47. }
  48.  
  49. int yywrap() {
  50. return 1;
  51. }
  52.  
Success #stdin #stdout #stderr 0.03s 6984KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/3KYmgF/prog:2:1: Syntax error: Operator expected
ERROR: /home/3KYmgF/prog:51:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit