%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h" // Header generated by yacc
%}
%%
[0-9]+ { yylval
= atoi
(yytext
); return
NUMBER; } [+] { return PLUS; }
[-] { return MINUS; }
[*] { return MULTIPLY; }
[/] { return DIVIDE; }
\n { return EOL; }
[ \t] ; // Ignore whitespace
. { printf("Invalid character: %s\n", yytext); }
%%
int main() {
printf("Enter a mathematical expression (e.g., 3 + 4):\n");
if (yyparse() == 0) {
printf("Parsed successfully!\n");
} else {
printf("Parsing failed.\n");
}
return 0;
}
int yyerror(char *s) {
fprintf(stderr, "Error: %s\n", s);
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgInkudGFiLmgiIC8vIEhlYWRlciBnZW5lcmF0ZWQgYnkgeWFjYwolfQolJQpbMC05XSsgICAgICB7IHl5bHZhbCA9IGF0b2koeXl0ZXh0KTsgcmV0dXJuIE5VTUJFUjsgfQpbK10gICAgICAgICB7IHJldHVybiBQTFVTOyB9ClstXSAgICAgICAgIHsgcmV0dXJuIE1JTlVTOyB9ClsqXSAgICAgICAgIHsgcmV0dXJuIE1VTFRJUExZOyB9ClsvXSAgICAgICAgIHsgcmV0dXJuIERJVklERTsgfQpcbiAgICAgICAgICB7IHJldHVybiBFT0w7IH0KWyBcdF0gICAgICAgOyAvLyBJZ25vcmUgd2hpdGVzcGFjZQouICAgICAgICAgICB7IHByaW50ZigiSW52YWxpZCBjaGFyYWN0ZXI6ICVzXG4iLCB5eXRleHQpOyB9CiUlCgppbnQgbWFpbigpIHsKICAgIHByaW50ZigiRW50ZXIgYSBtYXRoZW1hdGljYWwgZXhwcmVzc2lvbiAoZS5nLiwgMyArIDQpOlxuIik7CiAgICBpZiAoeXlwYXJzZSgpID09IDApIHsKICAgICAgICBwcmludGYoIlBhcnNlZCBzdWNjZXNzZnVsbHkhXG4iKTsKICAgIH0gZWxzZSB7CiAgICAgICAgcHJpbnRmKCJQYXJzaW5nIGZhaWxlZC5cbiIpOwogICAgfQogICAgcmV0dXJuIDA7Cn0KCmludCB5eWVycm9yKGNoYXIgKnMpIHsKICAgIGZwcmludGYoc3RkZXJyLCAiRXJyb3I6ICVzXG4iLCBzKTsKICAgIHJldHVybiAwOwp9