%{
#include <stdio.h>
#include <string.h>
int keyword_count = 0;
// List of C keywords
char *keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "int", "long", "register", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef", "union",
"unsigned", "void", "volatile", "while"
};
int is_keyword(char *word) {
for (int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1;
}
}
return 0;
}
%}
%%
[a-zA-Z_][a-zA-Z0-9_]* {
if (is_keyword(yytext)) {
keyword_count++;
}
}
. ; // Ignore all other characters
%%
int yywrap() {
return 1;
}
int main(int argc, char *argv[]) {
if (argc > 1) {
FILE *file = fopen(argv[1], "r");
if (!file) {
printf
("Could not
open file
%s\n", argv[1]); return 1;
}
yyin = file;
}
yylex();
printf
("Total
number of C keywords
: %d\n", keyword_count); return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBrZXl3b3JkX2NvdW50ID0gMDsKCi8vIExpc3Qgb2YgQyBrZXl3b3JkcwpjaGFyICprZXl3b3Jkc1tdID0gewogICAgImF1dG8iLCAiYnJlYWsiLCAiY2FzZSIsICJjaGFyIiwgImNvbnN0IiwgImNvbnRpbnVlIiwgImRlZmF1bHQiLAogICAgImRvIiwgImRvdWJsZSIsICJlbHNlIiwgImVudW0iLCAiZXh0ZXJuIiwgImZsb2F0IiwgImZvciIsICJnb3RvIiwKICAgICJpZiIsICJpbnQiLCAibG9uZyIsICJyZWdpc3RlciIsICJyZXR1cm4iLCAic2hvcnQiLCAic2lnbmVkIiwKICAgICJzaXplb2YiLCAic3RhdGljIiwgInN0cnVjdCIsICJzd2l0Y2giLCAidHlwZWRlZiIsICJ1bmlvbiIsCiAgICAidW5zaWduZWQiLCAidm9pZCIsICJ2b2xhdGlsZSIsICJ3aGlsZSIKfTsKCmludCBpc19rZXl3b3JkKGNoYXIgKndvcmQpIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZW9mKGtleXdvcmRzKS9zaXplb2Yoa2V5d29yZHNbMF0pOyBpKyspIHsKICAgICAgICBpZiAoc3RyY21wKHdvcmQsIGtleXdvcmRzW2ldKSA9PSAwKSB7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9CiV9CgolJQoKW2EtekEtWl9dW2EtekEtWjAtOV9dKiAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlzX2tleXdvcmQoeXl0ZXh0KSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGtleXdvcmRfY291bnQrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQoKLiAgICAgICAgICAgICAgICAgICAgICAgOyAvLyBJZ25vcmUgYWxsIG90aGVyIGNoYXJhY3RlcnMKCiUlCgppbnQgeXl3cmFwKCkgewogICAgcmV0dXJuIDE7Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pIHsKICAgIGlmIChhcmdjID4gMSkgewogICAgICAgIEZJTEUgKmZpbGUgPSBmb3Blbihhcmd2WzFdLCAiciIpOwogICAgICAgIGlmICghZmlsZSkgewogICAgICAgICAgICBwcmludGYoIkNvdWxkIG5vdCBvcGVuIGZpbGUgJXNcbiIsIGFyZ3ZbMV0pOwogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICAgICAgeXlpbiA9IGZpbGU7CiAgICB9CgogICAgeXlsZXgoKTsKICAgIHByaW50ZigiVG90YWwgbnVtYmVyIG9mIEMga2V5d29yZHM6ICVkXG4iLCBrZXl3b3JkX2NvdW50KTsKICAgIHJldHVybiAwOwp9Cgo=