LEX to count the no of "scanf" and "printf" statement in a c program. Replace them with "writf" and "readf" respectively.
/* declaration section in this sections we will declare the different value and include the header file which we are using in this program to run this program */
%{
#include<stdio.h>
int sf=0,pf=0;
%}
/* defined section */
%%
"scanf" { sf++; fprintf(yyout,"readf");} // replace scanf with readf
"printf" { pf++; fprintf(yyout,"writef");} // replace printf with writef
%%
int main()
{
yyin=fopen("open.c","r"); // input file open.c
yyout=fopen("new.c","w"); // output file new.c with replace
yylex();
//no of printf and scanf in the file
printf("Number of scanfs=%d\nNumber of Printf's=%d\n",sf,pf);
return 0;
}
%{
#include<stdio.h>
int sf=0,pf=0;
%}
/* defined section */
%%
"scanf" { sf++; fprintf(yyout,"readf");} // replace scanf with readf
"printf" { pf++; fprintf(yyout,"writef");} // replace printf with writef
%%
int main()
{
yyin=fopen("open.c","r"); // input file open.c
yyout=fopen("new.c","w"); // output file new.c with replace
yylex();
//no of printf and scanf in the file
printf("Number of scanfs=%d\nNumber of Printf's=%d\n",sf,pf);
return 0;
}
How to run this program
save the file as "ass1.6.l"
open terminal and run " flex ass1.6.l "
it generates c file run " cc lex.yy.c -o ass1.6 -ll " asoutputt in file count
now run for output " ./ass1.6"
"open.txt"
#include<stdio.h>
main()
{
int a;
printf("enter the number for sum\n")
scanf("%d",&a);
}
new.txt
Comments
Post a Comment