Wisdom Materials
Home
About Us
Our Clients
Careers
Services
Education
Jobs
News
Business
Health
Astrology
Entertainment
RealEstate
Devotion
Contact Us
Compiler Construction Lab Manual
/ Counting WhitespacesWords Characters Lines in file LEX Program
Program Name
Write a Compiler Construction Program for Counting WhitespacesWords Characters Lines in file LEX Program.
Theory
Counting WhitespacesWords Characters Lines in file LEX Program
Program Code
Copy Program Code
%{ int c=0,w=0,l=0,s=0; %} %% [^ \t\n]+ w++;c+=yyleng; [\n] l++; [ \t] s++; %% int main(int argc,char *argv[]) { yyin=fopen(argv[1],"r"); yylex(); printf("\n the no.of character %d",c); printf("\n the no.of words %d",w); printf("\n the no.of lines%d",l); printf("\n the no.of space %d",s); fclose(yyin); } int yywrap(void) { return 1; }
Saving:
vi CWWCL.l
Compiling and Running
lex CWWCL.L
cc lex.yy.c
./a.out a.txt
Input
Create a text file a.txt and enter data.
Vi a.txt
Prasad created this file1234 a1234
abcd
Output
the no.of character 34
the no.of words 6
the no.of lines2
the no.of space 4
Home
Back