System Software Lab Programs ( Lex and Yaac Programs )exa
// February 21st, 2009 // Engineering, Unix and System Software Lab
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 ]
If you enjoyed this post, make sure you subscribe to my RSS feed!
Related posts:
Top Commentators
Abhijeet (1)
Balachandra (1)
blaaze (1)
Guest Guestington (1)
julious (1)




