System Software Lab Manual

System Software Lab Manual

SYSTEM SOFTWARE LAB MANUAL

(LEX PROGRAMS)

1. Program to count the number of vowels and consonants in a given string.

%{
#include
int vowels=0;
int cons=0;
%}
%%
[aeiouAEIOU] {vowels++;}
[a-zA-Z] {cons++;}
%%
int yywrap()
{
return 1;
}
main()
{
printf(“Enter the string.. at end press ^d\n”);
yylex();
printf(“No of vowels=%d\nNo of consonants=%d\n”,vowels,cons);
}

2. Program to count the number of characters, words, spaces, end of lines in a given input file.

%{
#include
Int c=0, w=0, s=0, l=0;
%}
WORD [^ \t\n,\.:]+
EOL [\n]
BLANK [ ]
%%
{WORD} {w++; c=c+yyleng;}
{BLANK} {s++;}
{EOL} {l++;}
. {c++;}
%%
int yywrap()
{
return 1;
}
main(int argc, char *argv[])
{
If(argc!=2)
{
printf(“Usage: \n”);
exit(0);
}
yyin=fopen(argv[1],”r”);
yylex();
printf(“No of characters=%d\nNo of words=%d\nNo of spaces=%d\n No of lines=%d”,c,w,s,l);
}

3. Program to count no of:
a) +ve and –ve integers
b) +ve and –ve fractions

%{
#include
int posint=0, negint=0,posfraction=0, negfraction=0;
%}
%%
[-][0-9]+ {negint++;}
[+]?[0-9]+ {posint++;}
[+]?[0-9]*\.[0-9]+ {posfraction++;}
[-][0-9]* \.[0-9]+ {negfraction++;}
%%
int yywrap()
{
return 1;
}

main(int argc, char *argv[])
{
If(argc!=2)
{
printf(“Usage: \n”);
exit(0);
}
yyin=fopen(argv[1],”r”);
yylex();
printf(“No of +ve integers=%d\n No of –ve integers=%d\n No of +ve
fractions=%d\n No of –ve fractions=%d\n”, posint, negint,
posfraction, negfraction);
}

4. Program to count the no of comment line in a given C program. Also
eliminate them and copy that program into separate file

%{
#include
int com=0;
%}
%s COMMENT
%%
“/*”[.]*”*/” {com++;}
“/*” {BEGIN COMMENT ;}
”*/” {BEGIN 0; com++ ;}
\n {com++ ;}
. {;}
.|\n {fprintf(yyout,”%s”,yytext);
%%
int yywrap()
{
return 1;
}

main(int argc, char *argv[])
{
If(argc!=2)
{...

Similar Essays