%{
#include <stdio.h>
#include <stdlib.h>
/* Variables to store counts */
int char_count = 0;
int word_count = 0;
%}
%%
[a-zA-Z]+ {word_count++; char_count += yyleng;} /* Match words (sequence of alphabetic characters) and count them */
. {char_count++;} /* Match any single character (including spaces, punctuation) and count them */
%%
int main(){
printf("Enter input text (Ctrl+D to end):\n");
yylex();
printf("\nTotal characters: %d\n", char_count);
printf("Total words: %d\n", word_count);
return 0;
}
int yywrap() {
return 1;
}
JXsKICAgICNpbmNsdWRlIDxzdGRpby5oPgogICAgI2luY2x1ZGUgPHN0ZGxpYi5oPgogICAgLyogVmFyaWFibGVzIHRvIHN0b3JlIGNvdW50cyAqLwogICAgaW50IGNoYXJfY291bnQgPSAwOwogICAgaW50IHdvcmRfY291bnQgPSAwOwolfQolJQpbYS16QS1aXSsgICAge3dvcmRfY291bnQrKzsgY2hhcl9jb3VudCArPSB5eWxlbmc7fSAvKiBNYXRjaCB3b3JkcyAoc2VxdWVuY2Ugb2YgYWxwaGFiZXRpYyBjaGFyYWN0ZXJzKSBhbmQgY291bnQgdGhlbSAqLwouICAgIAkge2NoYXJfY291bnQrKzt9IC8qIE1hdGNoIGFueSBzaW5nbGUgY2hhcmFjdGVyIChpbmNsdWRpbmcgc3BhY2VzLCBwdW5jdHVhdGlvbikgYW5kIGNvdW50IHRoZW0gKi8KJSUKaW50IG1haW4oKXsKICAgIHByaW50ZigiRW50ZXIgaW5wdXQgdGV4dCAoQ3RybCtEIHRvIGVuZCk6XG4iKTsKICAgIHl5bGV4KCk7CiAgICBwcmludGYoIlxuVG90YWwgY2hhcmFjdGVyczogJWRcbiIsIGNoYXJfY291bnQpOwogICAgcHJpbnRmKCJUb3RhbCB3b3JkczogJWRcbiIsIHdvcmRfY291bnQpOwogICAgcmV0dXJuIDA7Cn0KaW50IHl5d3JhcCgpIHsKICAgIHJldHVybiAxOwp9Cg==