Calculator Using LEX and YACC
Aim
Implement a calculator using LEX and YACC.
Program
yacc
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int flag=0;
%}
%token NUMBER
%left '+''-'
%left '*''/''%'
%left '('')'
%%
Calculate:E{
printf("The calculated value is %d\n",$$);
}
E:E'+'E {$$ = $1+$3;}
|E'-'E {$$ = $1-$3;}
|E'*'E {$$ = $1*$3;}
|E'/'E {$$ = $1/$3;}
|E'%'E {$$ = $1%$3;}
|'('E')' {$$ = $2;}
|NUMBER {$$ = $1;}
;
%%
void main()
{
printf("Enter the expression to calculate the value.\n");
yyparse();
if(flag==1)
{
printf("Successful evaluation done.\n");
}
}
void yyerror()
{
flag=1;
printf("Invalid expression to calculate, unable to parse\n");
}
lex
%{
#include"y.tab.h"
extern yyval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUMBER;}
[\n] return 0;
[ \t] ;
. return yytext[0];
%%
int yywrap()
{
return 1;
}