Write a program to implement calculator using LEX and YACC

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

Also See : System Programming Programs and Assignments
#include <math.h>

    #define YYSTYPE double

%token NUM

%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: ");



#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];}

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)
Answer: 4.49981 


  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"

  2. JCECE Admit Card will be available to download in the 2nd Week of May,2018. Details about JCECE 2018 Admit Card are given in this article.

    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.

    OJEE Admit Card Important Dates Related To OJEE Admit Card 2018, How To Download OJEE 2018 Admit Card, Information In OJEE 2018 Admit Card, OJEE 2018 Admit Card Important Highlights. Also Find Complete details.

    Get the latest information on IPU CET Admit Card. Step by step guide is provided to view and download the IPU CET admit card is also provided in the article. IPU CET 2018 Admit Card Download link will be made available here as soon as GGSIPU Releases the Admit Card.

  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