Indian Institute of Information Technology
Surat ,
Lab Report on
System Software (CS403) Practical
Submitted by
KAPIL YADAV[UI23CS32]
Course Faculty
Ms. Shraddha Patel
Department of Computer Science and
Engineering
Indian Institute of Information Technology
Surat
Gujarat-394190, India
January-2024
1. Write a LEX program to count the number of vowels, consonants, +ve, -ve and zero digits in
a given string.
CODE:
%{
#include <stdio.h>
int vowels = 0, consonants = 0, positive_digits = 0, negative_digits = 0, zeros = 0;
%}
%%
[aeiouAEIOU] { vowels++; }
[a-zA-Z] { consonants++; }
[1-9] { positive_digits++; }
\-[1-9] { negative_digits++; }
0 { zeros++; }
. ;
%%
int main() {
printf("Enter a string: ");
yylex();
printf("\nCounts:\n");
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
printf("Positive digits: %d\n", positive_digits);
printf("Negative digits: %d\n", negative_digits);
printf("Zeros: %d\n", zeros);
return 0;
}
int yywrap() {
return 1;
OUTPUT:
2.Write a LEX program to count the number of lines, spaces and tab spaces from a given file.
Also eliminate them and print that program into separate file.
CODE:
%{
#include<stdio.h>
int lines = 0;
int spaces = 0;
int tabs = 0;
%}
%%
[ ] {spaces++;}
[\t] {tabs++;}
"\n" {lines++;}
<<EOF>> {printf("\nl = %d, s = %d, t = %d\n\n",lines,spaces,tabs); return 0;}
%%
int yywrap(void){}
int main(){
yyin=fopen("token.lex","r");
yyout=fopen("output.hello","w");
yylex();
return 1;
OUTPUT:
3. Write a LEX program to convert upper case to lower and lower case to upper case from a
given string.
CODE:
%{
#include <stdio.h>
#include <ctype.h>
%}
%%
[A-Za-z] {
if (islower(yytext[0]))
putchar(toupper(yytext[0]));
else
putchar(tolower(yytext[0]));
}
. { putchar(yytext[0]); }
%%
int yywrap() {
return 1; // End of input
}
int main() {
yyin = stdin;
yylex();
return 0;
}
OUTPUT:
4. Write a LEX program to identify octal or hexadecimal numbers.
CODE:
%{
#include <stdio.h>
%}
%%
0[xX][0-9a-fA-F]+ { printf("Hexadecimal: %s\n", yytext); }
0[0-7]+ { printf("Octal: %s\n", yytext); }
%%
int yywrap() {
return 1; // End of input
}
int main() {
yyin = stdin;
yylex();
return 0;
}
OUTPUT
5. Write a LEX program to count number of words those are <=5.
CODE:
%{
#include <stdio.h>
int short_words = 0;
%}
%%
[a-zA-Z]{1,5} { short_words++; }
[a-zA-Z]+ ;
[ \t\n] ;
. ;
%%
int main() {
printf("Enter a string: ");
yylex();
printf("Number of words with length <= 5: %d\n", short_words);
return 0;
}
int yywrap() {
return 1;
OUTPUT:
6. Write a LEX program to accept string that starting with vowels.
CODE:
%{
#include <stdio.h>
%}
%%
^[aeiouAEIOU][a-zA-Z]* {
printf("Valid string starting with vowel: %s\n", yytext);
}
.{
printf("Invalid string, does not start with vowel: %s\n", yytext);
}
%%
int yywrap(void){}
int main() {
yyin = stdin;
yylex();
return 0;
}
OUTPUT
7. Write a LEX program to recognize a valid arithmetic expression and identify the
identifiers and operators present. Print them separately.
CODE:
%{
#include <stdio.h>
%}
DIGIT [0-9]
LETTER [a-zA-Z]
IDENTIFIER {LETTER}({LETTER}|{DIGIT})*
OPERATOR [+\-*/=()]
%%
{IDENTIFIER} { printf("Identifier: %s\n", yytext); }
{OPERATOR} { printf("Operator: %s\n", yytext); }
{DIGIT}+ { /* Ignore numeric literals */ }
[\n\t ] { /* Ignore whitespace and newlines */ }
. { printf("Unrecognized character: %s\n", yytext); }
%%
int main() {
printf("Enter an arithmetic expression:\n");
yylex(); /* Start lexical analysis */
return 0;
int yywrap() {
return 1;
OUTPUT: