fork download
  1. %{
  2. #include <stdio.h>
  3.  
  4. int word_count = 0;
  5. int line_count = 0;
  6. %}
  7.  
  8. %%
  9. [ \t]+ ; // Ignore spaces and tabs
  10. \n { line_count++; } // Count each newline as a line
  11. [^ \t\n]+ { word_count++; } // Count sequences of non-space characters as words
  12. %%
  13.  
  14. int main() {
  15. printf("Enter text (Ctrl+D to end input on Linux / Ctrl+Z on Windows):\n");
  16. yylex(); // Start scanning
  17. printf("\nTotal number of lines: %d\n", line_count);
  18. printf("Total number of words: %d\n", word_count);
  19. return 0;
  20. }
Success #stdin #stdout #stderr 0.03s 6996KB
stdin
Input 
Hello world
This is a Lex program
stdout
Standard output is empty
stderr
ERROR: /home/PMnYJ3/prog:20:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? EOF: exit