%option noyywrap
%{
#include <stdio.h>
void yyerror(char *s);
%}
letter [a-zA-Z]
digit [0-9]
punct [,.;"]
%%
else|if|void|int {printf("%s is a keyword\n", yytext);}
{digit}+ {printf("%s is a number\n", yytext);}
{letter}({letter}|{digit})* {printf("%s is an identifier\n", yytext);}
{op} {printf
("
%s is an operator\n", yytext);} [ ] ;
\) {printf("%s is a close parenthesis\n", yytext);}
{punct} {printf("%s is a punctuation\n", yytext);}
. {yyerror("Invalid token");}
%%
void yyerror(char *s)
{
fprintf(stderr, "Error: %s at token '%s'\n", s, yytext);
}
int main(int argc, char *argv[])
{
FILE *fp;
if (argc < 2) {
fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
return 1;
}
if ((fp = fopen(argv[1], "r")) == NULL) {
perror("Error opening file");
return 1;
}
yyin = fp;
yylex();
fclose(fp);
return 0;
}
JW9wdGlvbiBub3l5d3JhcAoKJXsKICAgICNpbmNsdWRlIDxzdGRpby5oPgogICAgdm9pZCB5eWVycm9yKGNoYXIgKnMpOwolfQoKbGV0dGVyIFthLXpBLVpdCmRpZ2l0IFswLTldCm9wIFstKyovXQpwdW5jdCBbLC47Il0KJSUKCmVsc2V8aWZ8dm9pZHxpbnQgIHtwcmludGYoIiVzIGlzIGEga2V5d29yZFxuIiwgeXl0ZXh0KTt9CntkaWdpdH0rICAgICAgICAgICB7cHJpbnRmKCIlcyBpcyBhIG51bWJlclxuIiwgeXl0ZXh0KTt9CntsZXR0ZXJ9KHtsZXR0ZXJ9fHtkaWdpdH0pKiB7cHJpbnRmKCIlcyBpcyBhbiBpZGVudGlmaWVyXG4iLCB5eXRleHQpO30Ke29wfSAgICAgICAgICAgICAgIHtwcmludGYoIiVzIGlzIGFuIG9wZXJhdG9yXG4iLCB5eXRleHQpO30KWyAgXSAgICAgICAgICAgICAgIDsKXCkgICAgICAgICAgICAgICAgIHtwcmludGYoIiVzIGlzIGEgY2xvc2UgcGFyZW50aGVzaXNcbiIsIHl5dGV4dCk7fQp7cHVuY3R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