Lex Program: cal.l
%{
#include"y.tab.h"
#include<stdio.h>
%}
%%
[0-9]+ { yylval.d=atof(yytext);
return DIGIT;}
\n | . {return yytext[0];}
%%
cal.y
%{
#include<stdio.h>
#include<math.h>
%}
%union
{
double d;
}
%token <d> DIGIT;
%type <d> E;
%type <d> T;
%type <d> F;
%left <d> '+''-';
%left <d> '*''/';
%%
start
: E
'\n'{printf
("Answer
is%g\n",$1);exit(0);};
E: E'+'T {$$=$1+$3;}
| E'-'T {$$=$1-$3;}
| T{$$=$1;}
;
T: T'*'F{$$=$1*$3;}
| T'/'F{$$=$1/$3;}
| F{$$=$1;}
;
F: '('E')'{$$=$2;}
| DIGIT{$$=$1;}
;
%%
int main()
{
printf("Enter an expression");
yyparse();
}
int yyerror(char *error)
{
printf("%s",error);
}
TGV4IFByb2dyYW06IGNhbC5sCiV7CiNpbmNsdWRlInkudGFiLmgiCiNpbmNsdWRlPHN0ZGlvLmg+CiV9CiUlClswLTldKyB7IHl5bHZhbC5kPWF0b2YoeXl0ZXh0KTsKcmV0dXJuIERJR0lUO30KXG4gfCAuIHtyZXR1cm4geXl0ZXh0WzBdO30KJSUKY2FsLnkKJXsKCiNpbmNsdWRlPHN0ZGlvLmg+CiNpbmNsdWRlPG1hdGguaD4KJX0KJXVuaW9uCnsKZG91YmxlIGQ7Cn0KJXRva2VuIDxkPiBESUdJVDsKJXR5cGUgPGQ+IEU7CiV0eXBlIDxkPiBUOwoldHlwZSA8ZD4gRjsKJWxlZnQgPGQ+ICcrJyctJzsKJWxlZnQgPGQ+ICcqJycvJzsKJSUKc3RhcnQ6IEUnXG4ne3ByaW50ZigiQW5zd2VyIGlzJWdcbiIsJDEpO2V4aXQoMCk7fQo7CkU6IEUnKydUIHskJD0kMSskMzt9CnwgRSctJ1QgeyQkPSQxLSQzO30KfCBUeyQkPSQxO30KOwpUOiBUJyonRnskJD0kMSokMzt9CnwgVCcvJ0Z7JCQ9JDEvJDM7fQp8IEZ7JCQ9JDE7fQo7CkY6ICcoJ0UnKSd7JCQ9JDI7fQp8IERJR0lUeyQkPSQxO30KOwolJQppbnQgbWFpbigpCnsKcHJpbnRmKCJFbnRlciBhbiBleHByZXNzaW9uIik7Cnl5cGFyc2UoKTsKfQppbnQgeXllcnJvcihjaGFyICplcnJvcikKewpwcmludGYoIiVzIixlcnJvcik7Cn0=