/* lexical_analyzer.l */
%{
#include <stdio.h>
#include <stdlib.h>
%}
%%
"int" { printf("Keyword: int\n"); }
"float"
{ printf
("Keyword
: float\n"
); }"if" { printf("Keyword: if\n"); }
"else" { printf("Keyword: else\n"); }
[0-9]+ { printf
("
Integer: %s\n", yytext); } [0-9]+\
.[0-9]+ { printf
("
Float: %s\n", yytext); }
"=" { printf("Assignment Operator: =\n"); }
"==" { printf("Equality Operator: ==\n"); }
"+" { printf("Arithmetic Operator: +\n"); }
"-" { printf("Arithmetic Operator: -\n"); }
"*" { printf("Arithmetic Operator: *\n"); }
"/" { printf("Arithmetic Operator: /\n"); }
[ \t\n\r]+ { /* Skip whitespace */ }
. { printf("Unknown character: %s\n", yytext); }
%%
int main(int argc, char **argv) {
if (argc > 1) {
FILE *fp = fopen(argv[1], "r");
if (!fp) {
perror("Error opening file");
return 1;
}
yyin = fp;
}
yylex(); // Start lexical analysis
return 0;
}
LyogbGV4aWNhbF9hbmFseXplci5sICovCiV7CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiV9CgolJQoKImludCIgICAgICAgeyBwcmludGYoIktleXdvcmQ6IGludFxuIik7IH0KImZsb2F0IiAgICAgeyBwcmludGYoIktleXdvcmQ6IGZsb2F0XG4iKTsgfQoiaWYiICAgICAgICB7IHByaW50ZigiS2V5d29yZDogaWZcbiIpOyB9CiJlbHNlIiAgICAgIHsgcHJpbnRmKCJLZXl3b3JkOiBlbHNlXG4iKTsgfQoKWzAtOV0rICAgICAgeyBwcmludGYoIkludGVnZXI6ICVzXG4iLCB5eXRleHQpOyB9ClswLTldK1wuWzAtOV0rICB7IHByaW50ZigiRmxvYXQ6ICVzXG4iLCB5eXRleHQpOyB9CgoiPSIgICAgICAgICB7IHByaW50ZigiQXNzaWdubWVudCBPcGVyYXRvcjogPVxuIik7IH0KIj09IiAgICAgICAgeyBwcmludGYoIkVxdWFsaXR5IE9wZXJhdG9yOiA9PVxuIik7IH0KIisiICAgIC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