Code:
Algorithm:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
int wc=0,cc=0,lc=0,bc=0; | |
char infile[25]; | |
%} | |
word [^ \t\n]+ | |
eol \n | |
%% | |
{word} {wc++; cc+=yyleng;} | |
{eol} {lc++; cc++; } | |
[ ] {bc++; cc++; } | |
[\t] {bc+=8; cc++;} | |
%% | |
main() | |
{ | |
printf(" Read the Input File Name \n "); | |
scanf(“%s”,infile); | |
yyin=fopen(infile,"r"); | |
yylex(); | |
fclose(yyin); | |
printf(" No. of Chars: %d\n No. of Words: %d\n No.of Lines: %d\n No. of Blanks: %d\n",cc,wc,lc,bc); | |
} |
Step1: Start
Step2: Read Input file Name
Step3: Initialize wc=0 , lc=0, cc=0, bc=0
Step4: If input file content matches
[^ \t\n]+ then increment wc=wc+1 and cc=cc+ length of the current scanned word
else
\n then increment lc=lc+1
else
“ “ then increment bc=bc+1
else
\t then increment bc=bc+8 and cc=cc+1
Step5: Print the number of Words, Characters, Lines, Blank Spaces in the file
Step6: Stop
Explanation:
The aim of the code is to count the number of characters, word, lines and blank spaces. Logic is simple. Declare four variables for each count and then update each variables according to the pattern matched. Lets begin.
%{
int wc=0,cc=0,lc=0,bc=0;
char infile[25];
%}
This is declaration section of Lex part. Declare four variable to hold count of each words, characters, lines and spaces. initialize to 0 at the beginning. An extra variable for storing input file name is also declared.
word [^ \t\n]+
eol \n
These are two regular expression declaration for each word and End of line. (self explanatory)
%%
{word} {wc++; cc+=yyleng;}
{eol} {lc++; cc++; }
[ ] {bc++; cc++; }
[\t] {bc+=8; cc++;}
%%
'%%' represents seperation of declartion and definition section. Here we declare that if any of the pattern described in the definition section matches the scanned input in the input file, we are performing an action specified in the {...} part. We do so for each pattern. i.e., word, character,line and spaces.
- If word is encountered, then increment wc by 1. and increment character count by the length of matched character which is by default stored in lex variable 'yyleng'.
- If End of line 'eol' is encountered, then increment line count and character count by 1.
- If it is a blank space '[ ]' then increment blank space count 'bc' and character count 'cc' by 1.
- If tab space, '\t' then increment character count by 1 and blank space count by 8.
main()
{
printf(" Read the Input File Name \n ");
scanf(“%s”,infile);
yyin=fopen(infile,"r");
yylex();
fclose(yyin);
printf(" No. of Chars: %d\n No. of Words: %d\n No.of Lines: %d\n No. of Blanks: %d\n",cc,wc,lc,bc);
}
2 comments
Write commentsAll the programs are clearly explained. Thankyou so much for making us understand the concepts and helping us do well in the lab exams. Great Work ! Keep Going :)
ReplyThank you :)
ReplyShare your views about this article!