%{
#include <stdio.h>
#include <string.h>
const char *keywords[] = {"int", "float", "if", "else", "for", "while", "return", "void"};
const int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
void check_keyword(char *word) {
for (int i = 0; i < num_keywords; i++) {
if (strcmp(word, keywords[i]) == 0) {
printf("Keyword: %s\n", word);
return;
}
}
printf("Identifier: %s\n", word);
}
%}
%%
// Operators
[+\-*/%=<>!] { printf("Operator: %s\n", yytext); }
[ \t\n] { /* Ignore whitespace */ }
[a-zA-Z_][a-zA-Z0-9_]* { check_keyword(yytext); }
. { printf("Enter a valid input.\n"); }
%%
int main() {
yylex(); // Start the scanning process
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmNvbnN0IGNoYXIgKmtleXdvcmRzW10gPSB7ImludCIsICJmbG9hdCIsICJpZiIsICJlbHNlIiwgImZvciIsICJ3aGlsZSIsICJyZXR1cm4iLCAidm9pZCJ9Owpjb25zdCBpbnQgbnVtX2tleXdvcmRzID0gc2l6ZW9mKGtleXdvcmRzKSAvIHNpemVvZihrZXl3b3Jkc1swXSk7Cgp2b2lkIGNoZWNrX2tleXdvcmQoY2hhciAqd29yZCkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBudW1fa2V5d29yZHM7IGkrKykgewogICAgICAgIGlmIChzdHJjbXAod29yZCwga2V5d29yZHNbaV0pID09IDApIHsKICAgICAgICAgICAgcHJpbnRmKCJLZXl3b3JkOiAlc1xuIiwgd29yZCk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIklkZW50aWZpZXI6ICVzXG4iLCB3b3JkKTsKfQolfQoKJSUKLy8gT3BlcmF0b3JzClsrXC0qLyU9PD4hXSAgICAgICAgICAgIHsgcHJpbnRmKCJPcGVyYXRvcjogJXNcbiIsIHl5dGV4dCk7IH0KWyBcdFxuXSAgICAgICAgICAgICAgICAgIHsgLyogSWdub3JlIHdoaXRlc3BhY2UgKi8gfQpbYS16QS1aX11bYS16QS1aMC05X10qICAgeyBjaGVja19rZXl3b3JkKHl5dGV4dCk7IH0KLiAgICAgICAgICAgICAgICAgICAgICAgIHsgcHJpbnRmKCJFbnRlciBhIHZhbGlkIGlucHV0LlxuIik7IH0KCiUlIAoKaW50IG1haW4oKSB7CiAgICB5eWxleCgpOyAvLyBTdGFydCB0aGUgc2Nhbm5pbmcgcHJvY2VzcwogICAgcmV0dXJuIDA7Cn0K