0% found this document useful (0 votes)
14 views6 pages

Et21btco081 CD p8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Et21btco081 CD p8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Subject Code: BTCO13701 Subject Name: CD Date: / /2024

Enrollment No: ET21BTCO081 Name: Hasti Patel

Practical : 08
Aim:
1. Write a LEX program to count total printf and scanf statements from the input file.
Count should be written in the output file.
2. Write a LEX program that reads input from input file and write backs the same content
by replacing printf by write and scanf by read keyword in output file.
3. Write a LEX program that reads a c program from input file and eliminates single line
as well as multi-line comments from it and copies updated program to output file.
4. Write a LEX program that reads a program from an input file and counts the number of
letters, words, lines from it. Resulting count should be written to the output file.

1. Write a LEX program to count total printf and scanf statements from the input
file. Count should be written in the output file.

Code:

%{
#include<stdio.h>
int sf=0,pf=0;
%}

/* defined section */
%%
"scanf" { sf++; }
"printf" { pf++; }
%%
int main()
{
yyin=fopen("input.txt","r");
yyout=fopen("output.txt","w");
yylex();
printf("Number of scanfs=%d\nNumber of Printf's=%d\n",sf,pf);
fprintf(yyout, "Total printf statements: %d\n", pf);
fprintf(yyout, "Total scanf statements: %d\n", sf);
return 0;
}

SCET/CO/2024-25/ODD/BE Div-I/Sem-VII Page no:


Subject Code: BTCO13701 Subject Name: CD Date: / /2024
Enrollment No: ET21BTCO081 Name: Hasti Patel

int yywrap(){return 1;}

Output:

2. Write a LEX program that reads input from input file and write backs the same
content by replacing printf by write and scanf by read keyword in output file.

Code:
%{
#include<stdio.h>
int sf=0,pf=0;
%}

/* defined section */

%%
"scanf" { sf++; fprintf(yyout,"write");}
"printf" { pf++; fprintf(yyout,"read");}
%%
int main()
{
yyin=fopen("input.txt","r");
yyout=fopen("output.txt","w");
yylex();
printf("Number of scanfs=%d\nNumber of Printf's=%d\n",sf,pf);

return 0;
}
int yywrap(){return 1;}

SCET/CO/2024-25/ODD/BE Div-I/Sem-VII Page no:


Subject Code: BTCO13701 Subject Name: CD Date: / /2024
Enrollment No: ET21BTCO081 Name: Hasti Patel

Output:

3. Write a LEX program that reads a c program from input file and eliminate single
line as well as multi-line comments from it and copies updated program to output
file.

Code:

%{
#include<stdio.h>
int sl=0;
int ml=0;
%}
%%
"/*"[a-zA-Z0-9' '\t\n]+"*/" ml++;
"//".* sl++;
%%
int main()
{
yyin=fopen("input.c","r");

SCET/CO/2024-25/ODD/BE Div-I/Sem-VII Page no:


Subject Code: BTCO13701 Subject Name: CD Date: / /2024
Enrollment No: ET21BTCO081 Name: Hasti Patel

yyout=fopen("output.c","w");
yylex();
fclose(yyin);
fclose(yyout);
}
int yywrap(){return 1;}

Output:

SCET/CO/2024-25/ODD/BE Div-I/Sem-VII Page no:


Subject Code: BTCO13701 Subject Name: CD Date: / /2024
Enrollment No: ET21BTCO081 Name: Hasti Patel

4. Write a LEX program that reads a program from an input file and counts the
number of letters, words, lines from it. Resulting count should be written to the
output file.

Code:

%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%

\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;

%%
int main()
{
yyin= fopen("input.txt","r");
yyout=fopen("output.txt","w");
yylex();
total=s_letters+c_letters+num+spl_char;
fprintf(yyout," This File contains ...");
fprintf(yyout,"\n\t%d lines", lines);
fprintf(yyout,"\n\t%d words",words);
fprintf(yyout,"\n\t%d small letters", s_letters);
fprintf(yyout,"\n\t%d capital letters",c_letters);
fprintf(yyout,"\n\t%d digits", num);
fprintf(yyout,"\n\t%d special characters",spl_char);
fprintf(yyout,"\n\tIn total %d characters.\n",total);
}
int yywrap()

SCET/CO/2024-25/ODD/BE Div-I/Sem-VII Page no:


Subject Code: BTCO13701 Subject Name: CD Date: / /2024
Enrollment No: ET21BTCO081 Name: Hasti Patel

{
return(1);
}

Output:

SCET/CO/2024-25/ODD/BE Div-I/Sem-VII Page no:

You might also like