0% found this document useful (0 votes)
22 views7 pages

Lab 6 - LEX

The document outlines various LEX specifications for string manipulation tasks such as finding string length, counting vowels and consonants, identifying integers and floats, counting words, and validating strings starting with a vowel. Each task is accompanied by code snippets demonstrating the implementation of the specifications. The document serves as a practical guide for using LEX in lexical analysis.

Uploaded by

aatreyeedev05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Lab 6 - LEX

The document outlines various LEX specifications for string manipulation tasks such as finding string length, counting vowels and consonants, identifying integers and floats, counting words, and validating strings starting with a vowel. Each task is accompanied by code snippets demonstrating the implementation of the specifications. The document serves as a practical guide for using LEX in lexical analysis.

Uploaded by

aatreyeedev05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPT 6: LEX SPECIFICATIONS

Aim:
a) Write a LEX Specification to find the length of a string
b) Write a LEX Specification to count the no. of vowels and consonants in a string
c) Write a LEX Specification to find whether the given no. is an integer or float
d) Write a LEX Specification to find the number of words in the given string
e) Write a LEX Specification to accept a string starting with a vowel
f) Write a LEX Specification to count the number of words in a string whose
length is < 10 but > 6

Setup:

6a: Write a LEX Specification to find the length of a string


Code:
%{
#include <stdio.h>
#include <string.h>
%}
%%

\"[^\"]*\" {
printf("Length of the string: %lu\n", strlen(yytext));
}

%%
int main() {
yylex();
return 0;
}
Execution:

Output:
6b: Write a LEX Specification to count the no. of vowels, consonants in a string
Code:

%{
#include <stdio.h>
#include <ctype.h>

int vowel_count = 0;
int consonant_count = 0;
%}

%%

[aAeEiIoOuU] { vowel_count++; }
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] { consonant_count++; }
.|\n

%%

int main() {
yylex(); // Start scanning input
printf("Vowels: %d\n", vowel_count);
printf("Consonants: %d\n", consonant_count);
return 0;
}

Output:
6c: Write a LEX Specification to find whether the given no. is an integer or float
Code:

%{
#include <stdio.h>
#include <stdlib.h>
%}

%%

[0-9]+ { printf("Integer\n"); }
[0-9]*\.[0-9]+ { printf("Float\n"); }
[0-9]+\.[0-9]* { printf("Float\n"); }

%%

int main() {
yylex();
return 0;
}

Output:
6d: Write a LEX Specification to find the number of words in the given string
Code:

%{
#include <stdio.h>
#include <ctype.h>

int word_count = 0;
%}

%%

[a-zA-Z0-9]+ { word_count++; } /* Match words made up of letters and


digits */
[\n\t\r ]+ { }
. { }

%%

int main() {
yylex(); // Start lexical analysis
printf("Number of words: %d\n", word_count);
return 0;
}

Output:
6e: Write a LEX Specification to accept a string starting with a vowel
Code:

%{
#include <stdio.h>
#include <string.h>

%}

%%

^[aeiouAEIOU][a-zA-Z]* { printf("valid\n"); } /* String starts with a


vowel */
^.* { printf("invalid\n"); } /* Any other string */

%%

int main() {
yylex(); // Start lexical analysis
return 0;
}

Output:
6f: Write a LEX Specification to count the number of words in a string whose
length is < 10 but > 6
Code:

%{
#include <stdio.h>
#include <string.h>

int count = 0;
%}

%%

[a-zA-Z]{7,9} { count++; }
[\s\n\t]+ { }
.|\n { }

%%

int main() {
yylex(); // Start lexical analysis
printf("Number of words with length > 6 and < 10: %d\n", count);
return 0;
}

Output:

You might also like