Lex program to count the number of words, characters, blank spaces and lines in a file

/*Lex program to count the number of words, characters, blank spaces and lines in a C file.*/
 
 
%{
#include<stdio.h>
int count_lines=0, count_words=0,count_letters=0,S_letters=0, count_num=0, count_char=0,total=0;
%} 
 
%%
\n { count_lines++; count_words++;}
[\t ' '] count_words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] count_num++;
. count_char++;
%%

void main()
{
yyin= fopen("abc.txt","r");
yylex();
total=s_letters+count_letters+count_num+count_char;
printf(" This File contains ...");
printf("\n\t%d lines", count_lines);
printf("\n\t%d words",count_words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",count_letters);
printf("\n\t%d digits", count_num);
printf("\n\t%d special characters",count_char);
printf("\n\tIn total %d characters.\n",total);
}
 
int yywrap()
{
return(1);

//abc.txt file

output :

No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...