%{
#include <stdio.h>
#include <string.h>
int isKeyword(char *str) {
return strcmp(str, "if") == 0 ||
strcmp(str, "else") == 0 ||
strcmp(str, "while") == 0 ||
strcmp(str, "int") == 0 ||
strcmp(str, "float") == 0;
}
%}
%%
"if" { printf("Keyword: %s\n", yytext); }
"else" { printf("Keyword: %s\n", yytext); }
"while" { printf("Keyword: %s\n", yytext); }
"int" { printf("Keyword: %s\n", yytext); }
"float" { printf("Keyword: %s\n", yytext); }
[ \t\n]+ ; // Ignore whitespace
"(" { printf("Symbol: %s\n", yytext); }
")" { printf("Symbol: %s\n", yytext); }
"{" { printf("Symbol: %s\n", yytext); }
"}" { printf("Symbol: %s\n", yytext); }
";" { printf("Delimiter: %s\n", yytext); }
">="|"<="|"=="|"!="|">"|"<" { printf("Operator: %s\n", yytext); }
"=" { printf("Assignment Operator: %s\n", yytext); }
"*" { printf("Arithmetic Operator: %s\n", yytext); }
"/" { printf("Arithmetic Operator: %s\n", yytext); }
[0-9]+ { printf
("
Number: %s\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* {
if (isKeyword(yytext))
printf("Keyword: %s\n", yytext);
else
printf("Identifier: %s\n", yytext);
}
. { printf("Unknown: %s\n", yytext); }
%%
int main() {
yylex();
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBpc0tleXdvcmQoY2hhciAqc3RyKSB7CiAgICByZXR1cm4gc3RyY21wKHN0ciwgImlmIikgPT0gMCB8fAogICAgICAgICAgIHN0cmNtcChzdHIsICJlbHNlIikgPT0gMCB8fAogICAgICAgICAgIHN0cmNtcChzdHIsICJ3aGlsZSIpID09IDAgfHwKICAgICAgICAgICBzdHJjbXAoc3RyLCAiaW50IikgPT0gMCB8fAogICAgICAgICAgIHN0cmNtcChzdHIsICJmbG9hdCIpID09IDA7Cn0KJX0KCiUlCiJpZiIgICAgICAgIHsgcHJpbnRmKCJLZXl3b3JkOiAlc1xuIiwgeXl0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