This post was published long ago, when I was a student and an amateur blogger. The links might be broken and content may not be useful anymore. Please read this content keeping its age in mind.

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

1 a) Lex Program to count the number of characters, words, spaces and lines in a given input file.

% {

#include<stdio.h>
int (cc=0,lc=10,wc=0,sc=0)

% }

[^ t n]+ {wc++;cc+=yyleng;}
[/t]      {sc++;}
n        {lc++}

%%

main(int arg c, char * argv[])
{
FILE * file;
if(argc>1)

file=fopen(argv[1],”r”);
if(!file)
{
printf(“Could not open file :);
exit(1);
}
yyin=file;
}
yylex();

printf(“n Number of Spaces = %d “,sc);
printf(“n Number of Words = %d “,wc);
printf(“n Number of Characters = %d “,cc);
printf(“n Number of Lines  = %d “,lc);

return 0;

}

…………………………………………………………………………………….

SAMPLE OUTPUT

…………………………………………………………………………………….

FILE CONTENT :

HELLO WORLD,

WELCOME TO MY FIRST LEX PROGRAM

PROGRAM OUTPUT :

Number of Spaces = 6

Number of Words = 8
Number of Characters = 38
Number of Lines  = 2

[Request : If you are going to post this program on any other website please link back to original post ]