***Note : While executing the program remove the comments in blue. Comments mentioned are for understanding purpose
calc.l:
%{
//declarations for c code
#include<stdio.h>
#include"y.tab.h"
//extern because it is declared in yacc file and used here
extern int yylval;
%}
//regular expressions and corresponding actions for input streams read
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
//If any number is encountered yylval stores its value and we are returning token number which shows yylval's type ie int
[\t]; //we are ignoring white spaces
[\n] return 0;
. return yytext[0]; //return all the other characters as it is
%%
int yywrap() //returns 1 when end of file is reached otherwise its value is 0
{
return 1;
}
calc.y :
//for c declarations
%{
#include<stdio.h>
#include<time.h>
int flag=0;
extern FILE* yyin; //file pointer by default points to terminal
%}
//for declarations in yacc
%token NUMBER //declaring token NUMBER
%left '+''-' //to maintain associativity
%left '*''/''%'
%left '('')'
//context free grammar rules and corresponding action
%%
ArithematicExpression:E{
printf("\n Result=%d\n",$$);
return 0;
}
E:E'+'E{$$=$1+$3;} //$$ refers to non-terminal on left side, $1 to first E on right and so on
|E'-'E{$$=$1-$3;} //Pseudo varibles in {} describe action to be performed for given grammar rule
|E'*'E{$$=$1*$3;}
|E'/'E{$$=$1/$3;}
|E'%'E{$$=$1%$3;}
|'('E')'{$$=$2;}
|NUMBER{$$=$1;}
;
%%
void main()
{
clock_t starttime;
clock_t endtime;
double timeinterval;
int i;
FILE* fp=fopen("expressions.txt","r"); //file contains expression to be evaluated
yyin=fp;
starttime=clock();
yyparse(); //yyparse() in turn calls yylex()
endtime=clock();
timeinterval=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("Time required for execution=%lf Seconds.\n",timeinterval);
if(flag==0)
{
printf("\n ENTERED ARETHEMATIC EXPRESSION IS VALID \n\n");
}
}
void yyerror()
{
printf("\n ENTERED ARETHEMATIC EXPRESSION IS INVALID \n\n");
flag=1;
}
expressions.txt:
5+2*6
3*2+1
OUTPUT:
calc.l:
%{
//declarations for c code
#include<stdio.h>
#include"y.tab.h"
//extern because it is declared in yacc file and used here
extern int yylval;
%}
//regular expressions and corresponding actions for input streams read
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
//If any number is encountered yylval stores its value and we are returning token number which shows yylval's type ie int
[\t]; //we are ignoring white spaces
[\n] return 0;
. return yytext[0]; //return all the other characters as it is
%%
int yywrap() //returns 1 when end of file is reached otherwise its value is 0
{
return 1;
}
calc.y :
//for c declarations
%{
#include<stdio.h>
#include<time.h>
int flag=0;
extern FILE* yyin; //file pointer by default points to terminal
%}
//for declarations in yacc
%token NUMBER //declaring token NUMBER
%left '+''-' //to maintain associativity
%left '*''/''%'
%left '('')'
//context free grammar rules and corresponding action
%%
ArithematicExpression:E{
printf("\n Result=%d\n",$$);
return 0;
}
E:E'+'E{$$=$1+$3;} //$$ refers to non-terminal on left side, $1 to first E on right and so on
|E'-'E{$$=$1-$3;} //Pseudo varibles in {} describe action to be performed for given grammar rule
|E'*'E{$$=$1*$3;}
|E'/'E{$$=$1/$3;}
|E'%'E{$$=$1%$3;}
|'('E')'{$$=$2;}
|NUMBER{$$=$1;}
;
%%
void main()
{
clock_t starttime;
clock_t endtime;
double timeinterval;
int i;
FILE* fp=fopen("expressions.txt","r"); //file contains expression to be evaluated
yyin=fp;
starttime=clock();
yyparse(); //yyparse() in turn calls yylex()
endtime=clock();
timeinterval=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("Time required for execution=%lf Seconds.\n",timeinterval);
if(flag==0)
{
printf("\n ENTERED ARETHEMATIC EXPRESSION IS VALID \n\n");
}
}
void yyerror()
{
printf("\n ENTERED ARETHEMATIC EXPRESSION IS INVALID \n\n");
flag=1;
}
expressions.txt:
5+2*6
3*2+1
OUTPUT:
No comments:
Post a Comment