%{
#include<stdio.h>
#include<string.h>
int i
= 0; // Counter for the
number of words
%}
/* Rules Section */
%%
[a-zA-Z0-9]+ { i++; } /* Rule for counting words composed of alphanumeric characters */
[\n
] { printf
("
Number of words
: %d\n", i); i = 0; } /* Print count when a newline is encountered */ [ \t]+ { /* Ignore spaces and tabs */ }
%%
int yywrap(void) {
return 1; // Return 1 to indicate the end of the input
}
int main() {
// The function that starts the lexical analysis
printf("Enter words, and press Enter to get the word count:\n");
yylex(); // Start lexical analysis
return 0;
}
JXsKI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RyaW5nLmg+CmludCBpID0gMDsgIC8vIENvdW50ZXIgZm9yIHRoZSBudW1iZXIgb2Ygd29yZHMKJX0KCi8qIFJ1bGVzIFNlY3Rpb24gKi8KJSUKClthLXpBLVowLTldKyAgICB7IGkrKzsgfSAgLyogUnVsZSBmb3IgY291bnRpbmcgd29yZHMgY29tcG9zZWQgb2YgYWxwaGFudW1lcmljIGNoYXJhY3RlcnMgKi8KW1xuXSAgICAgICAgICAgIHsgcHJpbnRmKCJOdW1iZXIgb2Ygd29yZHM6ICVkXG4iLCBpKTsgaSA9IDA7IH0gIC8qIFByaW50IGNvdW50IHdoZW4gYSBuZXdsaW5lIGlzIGVuY291bnRlcmVkICovClsgXHRdKyAgICAgICAgICB7IC8qIElnbm9yZSBzcGFjZXMgYW5kIHRhYnMgKi8gfQoKJSUKCmludCB5eXdyYXAodm9pZCkgewogICAgcmV0dXJuIDE7ICAvLyBSZXR1cm4gMSB0byBpbmRpY2F0ZSB0aGUgZW5kIG9mIHRoZSBpbnB1dAp9CgppbnQgbWFpbigpIHsKICAgIC8vIFRoZSBmdW5jdGlvbiB0aGF0IHN0YXJ0cyB0aGUgbGV4aWNhbCBhbmFseXNpcwogICAgcHJpbnRmKCJFbnRlciB3b3JkcywgYW5kIHByZXNzIEVudGVyIHRvIGdldCB0aGUgd29yZCBjb3VudDpcbiIpOwogICAgeXlsZXgoKTsgIC8vIFN0YXJ0IGxleGljYWwgYW5hbHlzaXMKICAgIHJldHVybiAwOwp9Cg==