Untitled 1.odt
Untitled 1.odt
%{
# include<stdio.h>
int countadd = 0,countminus = 0,countmul = 0,countdiv = 0,ob,cb,count=0,invl=0;
%}
%%
[+] {countadd++;count++;}
[-] {countminus++;count++;}
[*] {countmul++;count++;}
[/] {countdiv++;count++;}
[(] {ob++;count++;}
[)] {cb++;count++;}
[\+]+ {invl++;}
[\-]+ {invl++;}
[\*]+ {invl++;}
[\/]+ {invl++;}
%%
int yywrap(void)
{
return 1;
}
void main()
{
printf("Enter the expression\n");
yylex();
if(ob == cb)
printf("No of + operators = %d\nNo of - operators = %d\nNo of * opeartors = %d\nNo of /
opeartors = %d\nTotal no of opeartors = %d\n",countadd,countminus,countmul,countdiv,count);
else if(invl!=0)
printf("Invalid Expression\n");
else if(ob!=cb)
printf("Invalid Expression\n");
}
2.
%{
#include<stdio.h>
#include<string.h>
int pfc=0,sfc=0;
%}
%%
"printf" {fprintf(yyout,"writef"); pfc++;}
"scanf" {fprintf(yyout,"readf"); sfc++;}
%%
int yywrap(void)
{
return 1;
}
void main(int argc,char *argr[])
{
if(argc!=3)
{
printf("Usage :./a.out in.txt out.txt \n");
exit(0);
}
yyin=fopen(argr[1],"r");
yyout=fopen(argr[2],"w");
yylex();
printf("no.of printf lines = %d\n",pfc);
printf("no. of scanf lines = %d\n",sfc);
}
3.
%{
#include<stdio.h>
#include<stdlib.h>
int flag=0,c,j;
%}
%%
[0-9]+ {c=atoi(yytext);
if(c==2)
{
printf("\n Prime number");
}
else if(c==0 || c==1)
{
printf("\n Not a Prime number");
}
else
{
for(j=2;j<c;j++)
{
if(c%j==0)
flag=1;
}
if(flag==1)
printf("\n Not a prime number");
else if(flag==0)
printf("\n Prime number");
}
}
%%
int yywrap(void){
}
int main()
{
yylex();
return 0;
}
4.
%{
#include<stdio.h>
#include<stdlib.h>
int pn=0,nn=0,pf=0,nf=0;
%}
DIGIT[0-9]
%%
\+?{DIGIT}+ pn++;
-{DIGIT}+ nn++;
\+?{DIGIT}*\.{DIGIT}+ pf++;
-{DIGIT}*\.{DIGIT}+ nf++;
%%
int yywrap(void){
}
int main()
{
yylex();
printf("No. of +ve numbers=%d\n",pn);
printf("No. of -ve numbers=%d\n",nn);
printf("No. of +ve fraction=%d\n",pf);
printf("No. of -ve fraction=%d\n",nf);
return 0;
}