fork download
  1. %{
  2. #include <stdio.h>
  3.  
  4. int line_count = 0;
  5. int tab_count = 0;
  6. int space_count = 0;
  7. int word_count = 0;
  8. int char_count = 0;
  9. int in_word = 0;
  10. %}
  11.  
  12. %%
  13. \n { line_count++; char_count++; }
  14. \t { tab_count++; char_count++; }
  15. " " { space_count++; char_count++; in_word = 0; }
  16. [^ \t\n]+ { word_count += !in_word; in_word = 1; char_count += yyleng; }
  17. . { char_count++; }
  18.  
  19. %%
  20.  
  21. int main(int argc, char *argv[]) {
  22. if (argc < 2) {
  23. fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
  24. return 1;
  25. }
  26.  
  27. FILE *file = fopen(argv[1], "r");
  28. if (!file) {
  29. perror("Error opening file");
  30. return 1;
  31. }
  32.  
  33. yyin = file;
  34. yylex();
  35. fclose(file);
  36.  
  37. printf("Lines: %d\n", line_count);
  38. printf("Tabs: %d\n", tab_count);
  39. printf("Spaces: %d\n", space_count);
  40. printf("Words: %d\n", word_count);
  41. printf("Characters: %d\n", char_count);
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout #stderr 0.02s 6876KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/g74g6d/prog:2:1: Syntax error: Operator expected
ERROR: /home/g74g6d/prog:44:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit