Title:Write a program to implement calculator using LEX and YACC

Also See : System Programming Programs and Assignments

```---------------------------
YACC PROGRAM
---------------------------
%{
#include<ctype.h>
#include<stdio.h>
#include <math.h>

#define YYSTYPE double
%}

%token NUM
%token COS SIN TAN LOG

%left '+' '-'
%left '*' '/'
%right UMINUS

%%

S         : S E '\n' { printf("Answer: %g \nEnter:\n", \$2); }
| S '\n'
|
| error '\n' { yyerror("Error: Enter once more...\n" );yyerrok; }
;
E         : E '+' E    { \$\$ = \$1 + \$3; }
| E'-'E    { \$\$=\$1-\$3; }
| E'*'E    { \$\$=\$1*\$3; }

|E'/'E    { \$\$=\$1/\$3; }
| '('E')'    { \$\$=\$2; }
| '-'E %prec UMINUS { \$\$= -\$2; }
| NUM
| COS'('E')' {\$\$=cos(\$3);}
| SIN'('E')' {\$\$=sin(\$3);}
| TAN'('E')' {\$\$=tan(\$3);}
| LOG'('E')' {\$\$=log(\$3);}

;

%%

#include "lex.yy.c"

int main()
{
printf("Enter the expression: ");
yyparse();
}

------------------------------
LEX PROGRAM
------------------------------

%{

#include <math.h>
%}

DIGIT [0-9]+\.?|[0-9]*\.[0-9]+

%%

{DIGIT}    {yylval=atof(yytext);return NUM;}
cos|COS {return COS;}
sin|SIN {return SIN;}
tan|TAN {return TAN;}
log|LOG {return LOG;}
\n|.    {return yytext[0];}

--------------------------------
OUTPUT
--------------------------------
amrut@amrut-HP-Pro-3330-SFF:~/college/Desktop\$ lex d.l
amrut@amrut-HP-Pro-3330-SFF:~/college/Desktop\$ yacc d.y
amrut@amrut-HP-Pro-3330-SFF:~/college/Desktop\$ gcc y.tab.c -ly -lfl -lm
amrut@amrut-HP-Pro-3330-SFF:~/college/Desktop\$ ./a.out
Enter the expression: log(90)