Coder Social home page Coder Social logo

neethiventhan123 / ex-11-implementation-of-calculator-using-lex-and-yacc- Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ramachandransec/ex-11-implementation-of-calculator-using-lex-and-yacc-

0.0 0.0 0.0 2 KB

IMPLEMENTATION OF CALCULATOR USING LEX AND YACC

ex-11-implementation-of-calculator-using-lex-and-yacc-'s Introduction

Ex-11-IMPLEMENTATION-OF-CALCULATOR-USING-LEX-AND-YACC-

IMPLEMENTATION OF CALCULATOR USING LEX AND YACC

Aim :

To implement a calculator using LEX and YACC.

ALGORITHM

  1. Start the program.
  2. Write a program in the vi editor and save it with .l extension.
  3. In the lex program, write the translation rules for the various mathematical functions.
  4. Write a program in the vi editor and save it with .y extension.
  5. Compile the lex program with lex compiler to produce output file as lex.yy.c. eg $ lex filename.l
  6. Compile the yacc program with yacc compiler to produce output file as y.tab.c. eg $ yacc โ€“d arith_id.y
  7. Compile these with the C compiler as gcc lex.yy.c y.tab.c
  8. Enter an expression as input and it is evaluated and the answer is displayed as output.

PROGRAM

Program: calculator.l file

%{
#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;}
ln |
LN {return nLOG;}
sin |
SIN {return SINE;}
cos |
COS {return COS;}
tan |
TAN {return TAN;}
mem {return MEM;}
[\t];
\$ return 0;
\n|. return yytext[0];
%%

Program: calculator.y file

%{
double memvar;
%}

%union { double dval; }

%token <dval> NUMBER
%token <dval> MEM
%token LOG nLOG SINE COS TAN
%left '-' '+'
%left '*' '/'
%right '^'
%left LOG nLOG SINE 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; }
| expression '/' expression { if ($3 == 0)
yyerror("divide by zero"); else
$$
 = $1 / $3;
}
| expression '^' expression {$$ = pow($1, $3); }
;

expression: '-' expression %prec UMINUS {$$ = -$2; }
| '(' expression ')' {$$ = $2; }
| LOG expression {$$ = log($2) / log(10); }
| nLOG expression {$$ = log($2); }
| SINE expression {$$ = sin($2 * 3.14 / 180); }
| COS expression {$$ = cos($2 * 3.14 / 180); }
| TAN expression {$$ = tan($2 * 3.14 / 180); }
| NUMBER {$$ = $1; }
| MEM {$$ = memvar; }
;

%%

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

int yyerror(char *error) {
printf("%s\n", error);
return 0;
}

OUTPUT

image

RESULT

The calculator is implemented using LEX and YACC and the output is verified.

ex-11-implementation-of-calculator-using-lex-and-yacc-'s People

Contributors

neethiventhan123 avatar ramachandransec avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.