%{
#include <stdio.h>
#include <string.h>
#define MAX_KEYWORDS 10
// Array of keywords
const char *keywords[] = {
"if", "else", "while", "for", "return",
"int", "float", "char", "void", "break"
};
// Function to check if the string
is a keyword
int is_keyword(const char *str) {
for (int i = 0; i < MAX_KEYWORDS; i++) {
if (strcmp(str, keywords[i]) == 0) {
return 1; // It's a keyword
}
}
return 0; // It's not a keyword
}
%}
%%
// Match keywords or identifiers (words starting with a letter or underscore)
[a-zA-Z_][a-zA-Z0-9_]* {
if (is_keyword(yytext)) {
printf
("
'%s' is a keyword
.\n"
, yytext
); } else {
printf
("
'%s' is not a keyword
.\n"
, yytext
); }
}
// Match anything that
is not a word
[ \t\n]+ { /* Ignore whitespace */ }
. { /* Ignore any other characters */ }
%%
int main() {
// Read input string from the user
printf("Enter a string: ");
yylex(); // Run Lex to start processing input
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCiNkZWZpbmUgTUFYX0tFWVdPUkRTIDEwCgovLyBBcnJheSBvZiBrZXl3b3Jkcwpjb25zdCBjaGFyICprZXl3b3Jkc1tdID0gewogICAgImlmIiwgImVsc2UiLCAid2hpbGUiLCAiZm9yIiwgInJldHVybiIsIAogICAgImludCIsICJmbG9hdCIsICJjaGFyIiwgInZvaWQiLCAiYnJlYWsiCn07CgovLyBGdW5jdGlvbiB0byBjaGVjayBpZiB0aGUgc3RyaW5nIGlzIGEga2V5d29yZAppbnQgaXNfa2V5d29yZChjb25zdCBjaGFyICpzdHIpIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTUFYX0tFWVdPUkRTOyBpKyspIHsKICAgICAgICBpZiAoc3RyY21wKHN0ciwga2V5d29yZHNbaV0pID09IDApIHsKICAgICAgICAgICAgcmV0dXJuIDE7IC8vIEl0J3MgYSBrZXl3b3JkCiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7IC8vIEl0J3Mgbm90IGEga2V5d29yZAp9CiV9CgolJQoKL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