Friday, September 9

LEX Program to count the number of lines, words and letters


Howdy guys,

Lets have a look on how a Lex programs works using a simple example.

This sample programs counts the number of lines, words and characters in a text file.


 
Lex programming is not rocket science. You have to just apply an eight year old kid's logic. Yes, you read right. Only a school kid's logic.

LOGIC

Read each character from the text file :

  • Is it a capital letter in English? [A-Z] : increment capital letter count by 1.
  • Is it a small letter in English? [a-z] : increment small letter count by 1
  • Is it [0-9]? increment digit count by 1.
  • All other characters (like '!', '@','&') are counted as special characters
  • How to count the number of lines? we simply count the encounters of '\n' <newline> character.that's all!! 
  • To count the number of words we count white spaces and tab character(of course, newline characters too..)

 Alignment of a Lex program is very simple and it makes the logic more vivid.



counter.l
%{
#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++;
%%





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


Sample output



 Let the 'myfile.txt' contains this.

                         This is my 1st lex program!!!
                         Cheers!! It works!!:)

The output will be


This file contains..
2 lines
9 words
30 small letters
3 capital letters
1 digits
9 special characters
In total 43 characters.



30 comments :

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

good one

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

how to count vowels, character,line,words all in one program(in lex).

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

good

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

how to find the word starting with a and ending with d using lex program

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

how to count the number of words from multiple files specified as a command line??

rneha725 said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by the author.
rneha725 said... Best Blogger Tips [Reply to comment] Best Blogger Templates

thanks for the code :)

james brownn said... Best Blogger Tips [Reply to comment] Best Blogger Templates

You have done a great job on article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you.
online word count

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

you are welcome @Neha Rani..
Please share it with others!

nepoleon keisham said... Best Blogger Tips [Reply to comment] Best Blogger Templates

the output doesn't came. pliz help.
lex count.l
cc lex.yy.c
./a.out
Is there any wrong in my step

Pradeep Garg said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@nepoleon keishamfor compile use
gcc lex.yy.c -lfl -o count
and for run use this
./count

patel said... Best Blogger Tips [Reply to comment] Best Blogger Templates

pls help me to write a program to count the no of vowels written in comment line of given c program.

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates


Very informative article.Thank you author for posting this kind of article .


http://www.instanceofjava.com/2015/09/count-number-of-words-in-string-java.html



Both are really good,
Cheers,
Venkat



Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

hey!
The code lines written after yylex() are not working for me .I mean they are not printing.can anyone help me
Thank u

Sridam Biswas said... Best Blogger Tips [Reply to comment] Best Blogger Templates

if i write last method i.e int yywrap()..when i run it showing something like that ..error stray /304 in that line...what is the meaning of it???..kindly if you tell it...Thank in advance

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

If multiple spaces are given in a row then word count will be wrong.

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

If multiple spaces are given in a row then word count will be wrong.

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

if you enter numbers with space it wont work.

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

showing lots of error on execution

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

count.l is not working. It doesn't give any output. what do do please help

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

I am running this code in flex on windows (editplus) but output is not shown ,whats the reason , only file reading program is not working,,, when i wrote yylex() at the end of print statments it show output of all 0 .. please i need help. are you only for posting or could you help also

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

use gcc lex.yy.c -lfl for genuine compiler
and then
./a.out

nikkolayebba said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Oh my goodness! a tremendous article dude. Thanks Nonetheless I'm experiencing situation with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting equivalent rss problem? Anybody who knows kindly respond. Thnkx casino slots

Rakesh said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Use
cc lex.yy.c -ll

Adams Young said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Please let me know if you’re looking for a article writer for your site. You have some really great posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine. Please send me an email if interested. Thank you! https://charactercount.org

martin said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thanks for sharing this information with us ! This blog is sharable.
Word Count Software

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Buddy there's a bug in your code if you change the contents of myfile.txt to only 3 "enters" or "newline" it'll give you o/p as 3 lines and 3 words. which is Wrong.

Smith said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Word count is an usefull tool to count Word, Line, Page and Character in multiple files and also you can calculate amount and generate reports. word count tool

Adams Young said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback. read

Aks said... Best Blogger Tips [Reply to comment] Best Blogger Templates

what if someone give two spaces.
it will increment word 2 times

Post a Comment