Category Archives: Laboratory Work

Laboratory Manual – System Programming (160706) – VI sem CE

Click the following link to download a laboratory manual.

Laboratory Manual – System Programming (160706)

Pract 7: Write a Lex program to recognize whether a given sentence is simple or compound.

%{
#include<stdio.h>
int is_simple=1;
%}
%%
[   \t\n]+[aA][nN][dD][   \t\n]+ {is_simple=0;}
[   \t\n]+[oO][rR][   \t\n]+ {is_simple=0;}
[   \t\n]+[bB][uU][tT][   \t\n]+ {is_simple=0;}
. {;}
%%
int yywrap()
{
return 1;
}

main()
{
int k;
printf(“Enter the sentence.. at end press ^d”);
yylex();
if(is_simple==1)
{
printf(“\nThe given sentence is simple”);
}
else
{
printf(“\nThe given sentence is compound”);
}
}

Pract 6: Write a Lex program to count no of: (a) +ve and –ve integers and (b) +ve and –ve fractions

%{
#include<stdio.h>
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: <./a.out> <sourcefile>\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);
}

Pract 5: Write a Lex program to count the number of characters, words, spaces, end of lines in a given input file.

%{

#include<stdio.h>

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: <./a.out> <sourcefile>\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\n”,c,w,s,l);

}

Pract 4: Write a Lex program to count the number of vowels and consonants in a given string.

%{
#include<stdio.h>
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);
}

How to Compile:
1.    Save the file with .l extention. E.g. vowels.l
2.    lex vowels.l
3.    gcc –o objfile lex.yy.c
4.    ./objfile

 

PLEASE ALSO SEE: