To implement a calculator using LEX and YACC.
- Start the program.
- Write a program in the vi editor and save it with .l extension.
- In the lex program, write the translation rules for the various mathematical functions.
- Write a program in the vi editor and save it with .y extension.
- Compile the lex program with lex compiler to produce output file as lex.yy.c. eg $ lex filename.l
- Compile the yacc program with yacc compiler to produce output file as y.tab.c. eg $ yacc โd arith_id.y
- Compile these with the C compiler as gcc lex.yy.c y.tab.c
- Enter an expression as input and it is evaluated and the answer is displayed as output.
%{
#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];
%%
%{
#include <stdio.h>
#include <math.h>
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("Error: Divide by zero");
else
$$ = $1 / $3;
}
| expression '^' expression { $$ = pow($1, $3); }
| '-' expression %prec UMINUS { $$ = -$2; }
| '(' expression ')' { $$ = $2; }
| LOG expression { $$ = log($2) / log(10); }
| nLOG expression { $$ = log($2); }
| SINE expression { $$ = sin($2 * M_PI / 180); }
| COS expression { $$ = cos($2 * M_PI / 180); }
| TAN expression { $$ = tan($2 * M_PI / 180); }
| NUMBER { $$ = $1; }
| MEM { $$ = memvar; }
;
%%
#include "y.tab.h"
#include <stdio.h>
int main() {
printf("Enter the expression: ");
yyparse();
return 0;
}
int yyerror(char *error) {
printf("%s\n", error);
return 0;
}
![description](https://private-user-images.githubusercontent.com/119393279/326276694-425c7cf0-2c0a-40fb-98db-5d3dab6c376d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNzA1OTEsIm5iZiI6MTcyMjE3MDI5MSwicGF0aCI6Ii8xMTkzOTMyNzkvMzI2Mjc2Njk0LTQyNWM3Y2YwLTJjMGEtNDBmYi05OGRiLTVkM2RhYjZjMzc2ZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzI4JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyOFQxMjM4MTFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wNGMyNDEzNTdhNzFhNDdhYjI0YzY5ZGRlNWEzYWQzNDdjODUxMGQ5OTdkODMwNDUyZGNmN2QyZjkzZGJiNDFlJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8M2KgCHEzRKr1Y2mCQxTQweZ0csszv_Z3Wwvzrs3aAs)
The calculator is implemented using LEX and YACC and the output is verified.