%{
#include <stdio.h>
#include <string.h>
int keyword_count = 0;
int identifier_count = 0;
// List of C keywords
char *keywords[] = {
"int", "float", "if", "else", "while", "return", "for", "char", "void", NULL
};
int is_keyword(const char *word) {
for (int i = 0; keywords[i] != NULL; i++) {
if (strcmp(word, keywords[i]) == 0)
return 1;
}
return 0;
}
%}
%%
[a-zA-Z_][a-zA-Z0-9_]* {
if (is_keyword(yytext)) {
printf("Keyword: %s\n", yytext);
keyword_count++;
} else {
printf("Identifier: %s\n", yytext);
identifier_count++;
}
}
[ \t\n]+ ; // Ignore whitespace
. ; // Ignore other characters
%%
int main() {
printf("Enter code (press Ctrl+D to finish input):\n");
yylex();
printf("\nTotal Keywords: %d\n", keyword_count);
printf("Total Identifiers: %d\n", identifier_count);
return 0;
}
int yywrap() {
return 1;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBrZXl3b3JkX2NvdW50ID0gMDsKaW50IGlkZW50aWZpZXJfY291bnQgPSAwOwoKLy8gTGlzdCBvZiBDIGtleXdvcmRzCmNoYXIgKmtleXdvcmRzW10gPSB7CiAgICAiaW50IiwgImZsb2F0IiwgImlmIiwgImVsc2UiLCAid2hpbGUiLCAicmV0dXJuIiwgImZvciIsICJjaGFyIiwgInZvaWQiLCBOVUxMCn07CgppbnQgaXNfa2V5d29yZChjb25zdCBjaGFyICp3b3JkKSB7CiAgICBmb3IgKGludCBpID0gMDsga2V5d29yZHNbaV0gIT0gTlVMTDsgaSsrKSB7CiAgICAgICAgaWYgKHN0cmNtcCh3b3JkLCBrZXl3b3Jkc1tpXSkgPT0gMCkKICAgICAgICAgICAgcmV0dXJuIDE7CiAgICB9CiAgICByZXR1cm4gMDsKfQolfQoKJSUKClthLXpBLVpfXVthLXpBLVowLTlfXSogewogICAgaWYgKGlzX2tleXdvcmQoeXl0ZXh0KSkgewogICAgICAgIHByaW50ZigiS2V5d29yZDogJXNcbiIsIHl5dGV4dCk7CiAgICAgICAga2V5d29yZF9jb3VudCsrOwogICAgfSBlbHNlIHsKICAgICAgICBwcmludGYoIklkZW50aWZpZXI6ICVzXG4iLCB5eXRleHQpOwogICAgICAgIGlkZW50aWZpZXJfY291bnQrKzsKICAgIH0KfQoKWyBcdFxuXSsgICA7ICAgLy8gSWdub3JlIHdoaXRlc3BhY2UKLiAgICAgICAgICA7ICAgLy8gSWdub3JlIG90aGVyIGNoYXJhY3RlcnMKCiUlCgppbnQgbWFpbigpIHsKICAgIHByaW50ZigiRW50ZXIgY29kZSAocHJlc3MgQ3RybCtEIHRvIGZpbmlzaCBpbnB1dCk6XG4iKTsKICAgIHl5bGV4KCk7CiAgICBwcmludGYoIlxuVG90YWwgS2V5d29yZHM6ICVkXG4iLCBrZXl3b3JkX2NvdW50KTsKICAgIHByaW50ZigiVG90YWwgSWRlbnRpZmllcnM6ICVkXG4iLCBpZGVudGlmaWVyX2NvdW50KTsKICAgIHJldHVybiAwOwp9CgppbnQgeXl3cmFwKCkgewogICAgcmV0dXJuIDE7Cn0K