fork download
  1. %{
  2. #include <stdio.h>
  3.  
  4. int word_count = 0; // To count the number of words
  5. int char_count = 0; // To count the number of characters
  6. int line_count = 0; // To count the number of lines
  7. %}
  8.  
  9. %%
  10.  
  11. \n { line_count++; } // Increment line count on newline
  12. [ \t]+ { /* Ignore spaces and tabs */ }
  13. [A-Za-z0-9]+ { word_count++; char_count += yyleng; } // Count words and characters
  14. . { char_count++; } // Count other characters (including punctuation)
  15.  
  16. %%
  17.  
  18. int main() {
  19. yylex();
  20. printf("Lines: %d\n", line_count);
  21. printf("Words: %d\n", word_count);
  22. printf("Characters: %d\n", char_count);
  23. return 0;
  24. }
  25.  
Success #stdin #stdout #stderr 0.03s 6940KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/qHjLOy/prog:2:1: Syntax error: Operator expected
ERROR: /home/qHjLOy/prog:24:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit