%{
#include<stdio.h>
#include<stdlib.h>
int result=0;
int curr=0;
void apply_operation();
%}
%option noyywrap
digit [0-9]+
space [ \t]
%%
{space};
{digit}+ {
curr=atoi(yytext);
apply_operation();
}
\n {
printf("Result:%d",result);
result=0;
curr=0;
}
. {printf("Invalid character\n");
exit(1);
}
%%
void apply_operation(){
case '+': result+=curr;break;
case '-': result-=curr;break;
case '*':result*=curr;break;
case '/':{
if(curr==0){
printf("Error division by 0\n");
exit(1);
}
result/=curr;break;
}
}
}
int main(){
printf("Enter expression: ");
yylex();
return 0;
}
JXsKICAjaW5jbHVkZTxzdGRpby5oPgogICNpbmNsdWRlPHN0ZGxpYi5oPgogIGludCByZXN1bHQ9MDsKICBpbnQgY3Vycj0wOwogIGNoYXIgb3A9JysnOwogIHZvaWQgYXBwbHlfb3BlcmF0aW9uKCk7CiV9CiVvcHRpb24gbm95eXdyYXAKZGlnaXQgWzAtOV0rCnNwYWNlIFsgXHRdCiUlCntzcGFjZX07CntkaWdpdH0rIHsKICAgICAgICBjdXJyPWF0b2koeXl0ZXh0KTsKICAgICAgICBhcHBseV9vcGVyYXRpb24oKTsKICAgICAgICB9CiIrIiB7b3A9JysnO30KIi0iIHtvcD0nLSc7fQoiKiIge29wPScqJzt9CiIvIiB7b3A9Jy8nO30KXG4gewogIHByaW50ZigiUmVzdWx0OiVkIixyZXN1bHQpOwogIHJlc3VsdD0wOwogIGN1cnI9MDsKICBvcD0nKyc7Cn0KLiB7cHJpbnRmKCJJbnZhbGlkIGNoYXJhY3RlclxuIik7CiAgZXhpdCgxKTsKfQolJQp2b2lkIGFwcGx5X29wZXJhdGlvbigpewogIHN3aXRjaChvcCl7CiAgICBjYXNlICcrJzogcmVzdWx0Kz1jdXJyO2JyZWFrOwogICAgY2FzZSAnLSc6IHJlc3VsdC09Y3VycjticmVhazsKICAgIGNhc2UgJyonOnJlc3VsdCo9Y3VycjticmVhazsKICAgIGNhc2UgJy8nOnsKICAgICAgaWYoY3Vycj09MCl7CiAgICAgICAgcHJpbnRmKCJFcnJvciBkaXZpc2lvbiBieSAwXG4iKTsKICAgICAgICBleGl0KDEpOwogICAgICB9CiAgICAgIHJlc3VsdC89Y3VycjticmVhazsKICAgIH0KICAgIAogIH0KfQppbnQgbWFpbigpewogIHByaW50ZigiRW50ZXIgZXhwcmVzc2lvbjogIik7CiAgeXlsZXgoKTsKICByZXR1cm4gMDsKfQ==