%{
#include <stdio.h>
int char_count
= 0; // Total
number of characters
int word_count
= 0; // Total
number of words
int line_count
= 0; // Total
number of lines
%}
%%
\n { line_count++; char_count++; } // Increment line and character counts for each newline
[ \t]+ { char_count += yyleng; } // Count spaces and tabs as characters
[^\n \t]+ { char_count += yyleng; word_count++; } // Count non-whitespace as a word and characters
. { char_count++; } // Count any other character
%%
int main() {
printf("Enter text (Ctrl+D to end input on Linux/macOS, Ctrl+Z on Windows):\n");
yylex
(); // Call the scanner
// Print the results
printf("\nLines: %d\n", line_count);
printf("Words: %d\n", word_count);
printf("Characters: %d\n", char_count);
return 0;
}
int yywrap() {
return 1; // Indicate end of input
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgY2hhcl9jb3VudCA9IDA7IC8vIFRvdGFsIG51bWJlciBvZiBjaGFyYWN0ZXJzCmludCB3b3JkX2NvdW50ID0gMDsgLy8gVG90YWwgbnVtYmVyIG9mIHdvcmRzCmludCBsaW5lX2NvdW50ID0gMDsgLy8gVG90YWwgbnVtYmVyIG9mIGxpbmVzCiV9CgolJQpcbiAgICAgICAgICB7IGxpbmVfY291bnQrKzsgY2hhcl9jb3VudCsrOyB9IC8vIEluY3JlbWVudCBsaW5lIGFuZCBjaGFyYWN0ZXIgY291bnRzIGZvciBlYWNoIG5ld2xpbmUKWyBcdF0rICAgICAgeyBjaGFyX2NvdW50ICs9IHl5bGVuZzsgfSAgICAgIC8vIENvdW50IHNwYWNlcyBhbmQgdGFicyBhcyBjaGFyYWN0ZXJzClteXG4gXHRdKyAgIHsgY2hhcl9jb3VudCArPSB5eWxlbmc7IHdvcmRfY291bnQrKzsgfSAvLyBDb3VudCBub24td2hpdGVzcGFjZSBhcyBhIHdvcmQgYW5kIGNoYXJhY3RlcnMKLiAgICAgICAgICAgeyBjaGFyX2NvdW50Kys7IH0gICAgICAgICAgICAgIC8vIENvdW50IGFueSBvdGhlciBjaGFyYWN0ZXIKJSUKCmludCBtYWluKCkgewogICAgcHJpbnRmK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