%{
#include <stdio.h>
#include <ctype.h>
int blank_spaces = 0; // Counter for blank spaces
int word_count = 0; // Counter for words
int line_count = 0; // Counter for lines
%}
%%
\n { line_count++; } // Increment line count on encountering newline
[ \t]+ { blank_spaces++; } // Increment blank spaces count for spaces and tabs
[A-Za-z0-9]+ { word_count++; } // Increment word count for words (alphanumeric)
. { } // Ignore all other characters
%%
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
yyin
= file
; // Set the input file for lex to
read from
yylex
(); // Call the lexer to start scanning
printf("Lines: %d\n", line_count);
printf("Words: %d\n", word_count);
printf("Blank Spaces: %d\n", blank_spaces);
fclose(file);
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgoKaW50IGJsYW5rX3NwYWNlcyA9IDA7ICAvLyBDb3VudGVyIGZvciBibGFuayBzcGFjZXMKaW50IHdvcmRfY291bnQgPSAwOyAgICAvLyBDb3VudGVyIGZvciB3b3JkcwppbnQgbGluZV9jb3VudCA9IDA7ICAgIC8vIENvdW50ZXIgZm9yIGxpbmVzCiV9CgolJQoKXG4gICAgICAgICAgICAgeyBsaW5lX2NvdW50Kys7IH0gICAgICAgICAgICAgICAgIC8vIEluY3JlbWVudCBsaW5lIGNvdW50IG9uIGVuY291bnRlcmluZyBuZXdsaW5lClsgXHRdKyAgICAgICAgIHsgYmxhbmtfc3BhY2VzKys7IH0gICAgICAgICAgICAgICAvLyBJbmNyZW1lbnQgYmxhbmsgc3BhY2VzIGNvdW50IGZvciBzcGFjZXMgYW5kIHRhYnMKW0EtWmEtejAtOV0rICAgeyB3b3JkX2NvdW50Kys7IH0gICAgICAgICAgICAgICAgIC8vIEluY3JlbWVudCB3b3JkIGNvdW50IGZvciB3b3JkcyAoYWxwaGFudW1lcmljKQouICAgICAgICAgICAgICB7IH0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vIElnbm9yZSBhbGwgb3RoZXIgY2hhcmFjdGVycwoKJSUKCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pIHsKICAgIGlmIChhcmdjIDwgMikgewogICAgICAgIHByaW50ZigiVXNhZ2U6ICVzIDxmaWxlbmFtZT5cbiIsIGFyZ3ZbMF0pOwogICAgICAgIHJldHVybiAxOwogICAgfQoKICAgIEZJTEUgKmZpbGUgPSBmb3Blbihhcmd2WzFdLCAiciIpOwogICAgaWYgKGZpbGUgPT0gTlVMTCkgewogICAgICAgIHBlcnJvcigiRXJyb3Igb3BlbmluZyBmaWxlIik7CiAgICAgICAgcmV0dXJuIDE7CiAgICB9CgogICAgeXlpbiA9IGZpbGU7ICAvLyBTZXQgdGhlIGlucHV0IGZpbGUgZm9yIGxleCB0byByZWFkIGZyb20KICAgIHl5bGV4KCk7ICAgICAgLy8gQ2FsbCB0aGUgbGV4ZXIgdG8gc3RhcnQgc2Nhbm5pbmcKICAgIAogICAgcHJpbnRmKCJMaW5lczogJWRcbiIsIGxpbmVfY291bnQpOwogICAgcHJpbnRmKCJXb3JkczogJWRcbiIsIHdvcmRfY291bnQpOwogICAgcHJpbnRmKCJCbGFuayBTcGFjZXM6ICVkXG4iLCBibGFua19zcGFjZXMpOwoKICAgIGZjbG9zZShmaWxlKTsKICAgIHJldHVybiAwOwp9Cg==