Skip to main content

Posts

Showing posts with the label Implementation of Calculator using LEX and YACC

Implementation of Calculator using LEX and YACC

 Implementation of Calculator using LEX and YACC Lex<Cal.L> %{ #include"y.tab.h" #include<math.h> %} %% ([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {yylval.dval=atof(yytext);return NUMBER;} log | LOG {return LOG;} In {return nLOG;} sin | SIN {return SINE;} cos | COS {return COS;} tan | TAN {return TAN;} mem {return MEM;} [\t]; \$ return 0; \n|. return yytext[0]; %% Yacc<Cal.Y> %{ double memvar; %} %union { double dval; } %token<dval>NUMBER %token<dval>MEM %token LOG SINE nLOG COS TAN %left '-' '+' %left '*' '/' %right '^' %left LOG SINE nLOG COS TAN %nonassoc UMINUS %type<dval>expression %% start:statement'\n' |start statement'\n' ; statement:MEM'='expression {memvar=$3;} | expression{printf("Answer=%g\n",$1);} ; expression:expression'+'expression {$$=$1+$3;} | expression '-' expression {$$=$1-$3;} | expression '*' expression {$$=$1*$3;} | expr...