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
--------------------------------
[email protected]:~/college/Desktop\$ lex d.l
[email protected]:~/college/Desktop\$ yacc d.y
[email protected]:~/college/Desktop\$ gcc y.tab.c -ly -lfl -lm
[email protected]:~/college/Desktop\$ ./a.out
Enter the expression: log(90)
Enter:
sin(90)

```

1. it shows a error like this
"/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/liby.a(yyerror.o): In function `yyerror':
(.text+0x1c): undefined reference to `rpl_fprintf'
collect2: ld returned 1 exit status"

Assam CEE admit card to be released on 9th April 2018. Click Here to know more. Step by step guide to view and download the admit card. Important instructions to be followed on the exam day. contents of Hall Ticket

GUJCET Admit Card will be available to download in the Month of May. Get the latest information on GUJCET 2018 Admit Card here. The procedure to view and download the GUJCET 2018 admit card is also given in the article. GUJCET 2018 Hall Ticket link will be provided here as soon as it officially released.

Get the detailed information about the Admit Card of Uttarakhand State Entrance Examination UKSEE for the academic session 2018-19 here UKSEE admit card

GCET Admit card will be available from 27th April 2018. Get the latest information on GCET 2018 Admit Card. The admit card for GCET 2018 will be handed over to the applicant as soon as the candidate submit the application form at the application reception center.

3. This comment has been removed by the author.

4. I searched far and wide but failed to grab my hands on a good mortgage calculator. Thank God! Yours has not disappointed me. mortgage calculator

5. This comment has been removed by the author.

6. In which case you may then ask, "What are unprofessional mortgages? because they sound decidedly dangerous.