fork download
  1. %{
  2. #include <stdio.h>
  3.  
  4. int char_count = 0; // Total number of characters
  5. int word_count = 0; // Total number of words
  6. int line_count = 0; // Total number of lines
  7. %}
  8.  
  9. %%
  10. \n { line_count++; char_count++; } // Increment line and character counts for each newline
  11. [ \t]+ { char_count += yyleng; } // Count spaces and tabs as characters
  12. [^\n \t]+ { char_count += yyleng; word_count++; } // Count non-whitespace as a word and characters
  13. . { char_count++; } // Count any other character
  14. %%
  15.  
  16. int main() {
  17. printf("Enter text (Ctrl+D to end input on Linux/macOS, Ctrl+Z on Windows):\n");
  18. yylex(); // Call the scanner
  19.  
  20. // Print the results
  21. printf("\nLines: %d\n", line_count);
  22. printf("Words: %d\n", word_count);
  23. printf("Characters: %d\n", char_count);
  24. return 0;
  25. }
  26.  
  27. int yywrap() {
  28. return 1; // Indicate end of input
  29. }
  30.  
Success #stdin #stdout #stderr 0.02s 6904KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/sh5gvD/prog:2:1: Syntax error: Operator expected
ERROR: /home/sh5gvD/prog:29:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit