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 :
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..)
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 :
good one
how to count vowels, character,line,words all in one program(in lex).
good
how to find the word starting with a and ending with d using lex program
how to count the number of words from multiple files specified as a command line??
thanks for the code :)
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
you are welcome @Neha Rani..
Please share it with others!
the output doesn't came. pliz help.
lex count.l
cc lex.yy.c
./a.out
Is there any wrong in my step
@nepoleon keishamfor compile use
gcc lex.yy.c -lfl -o count
and for run use this
./count
pls help me to write a program to count the no of vowels written in comment line of given c program.
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
hey!
The code lines written after yylex() are not working for me .I mean they are not printing.can anyone help me
Thank u
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
If multiple spaces are given in a row then word count will be wrong.
If multiple spaces are given in a row then word count will be wrong.
if you enter numbers with space it wont work.
showing lots of error on execution
count.l is not working. It doesn't give any output. what do do please help
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
use gcc lex.yy.c -lfl for genuine compiler
and then
./a.out
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
Use
cc lex.yy.c -ll
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
Thanks for sharing this information with us ! This blog is sharable.
Word Count Software
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.
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
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
what if someone give two spaces.
it will increment word 2 times
Post a Comment