0% found this document useful (0 votes)
6 views3 pages

Ayush

The document outlines two practical exercises involving LEX code. The first exercise counts vowels and consonants in a given string, while the second checks for valid email formats. Both exercises include algorithms and source code examples for implementation.

Uploaded by

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

Ayush

The document outlines two practical exercises involving LEX code. The first exercise counts vowels and consonants in a given string, while the second checks for valid email formats. Both exercises include algorithms and source code examples for implementation.

Uploaded by

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

Practical 11

Objective: Design a LEX Code to count number of vowels and consonants in a given
pattern.

Algorithm:

9. Initialize Counters → Set v = 0 (vowel count) and c = 0 (consonant count).


10. Print Prompt → Display "Enter a string: " before reading input.
11. Read Input Character by Character Until EOF
12. Classify and Count Characters:
• Newline (\n) → Print "Enter a string: " again.
• Vowels ([aeiouAEIOU]) → Increment v++.
• Consonants ([b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]) → Increment c++.
• Other Characters (.) → Ignore.
13. Print Results:
• Display "Total number of vowels: X" where X = v.
• Display "Total number of consonants: Y" where Y = c.
14. End Execution.

Source Code:

%{
#include<stdio.h>
int v=0,c=0;
%}

%%
\n {printf("Enter a string: ");}
[aeiouAEIOU] {v++;}
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] {c++;}
.;
%%

int main(){
printf("Enter a string: ");
yylex();
printf("Total number of vowels: %d\n", v);
printf("Total number of consonants: %d\n", c);
return 0;
}

Name: Ayush Section: Roll no.


Uniyal E1 23
int yywrap()
{ return 1;
}

Output:

Name: Ayush Section: Roll no.


Uniyal E1 23
Practical 12

Objective: Design a LEX Code to check for a valid E-mail Id.


Algorithm:
15. Print Prompt → Display "Enter a string: " before reading input.
16. Read Input Character by Character Until EOF
17. Classify and Validate Email Format:
• Newline (\n) → Print "Enter a string: " again.
• Valid Email Pattern ([a-zA-Z0-9]+[a-zA-Z0-9._]*[a-zA-Z0-9]+"@"[a-zA-
Z0-9]+[a-zA-Z0-9.]+[a-zA-Z0-9]+) → Print as "X is a valid email." where X
is the matched input.
• Other Inputs (.+) → Print as "X is not a valid email." where X is the matched
input.
18. End Execution.
Source Code:
%{
#include<stdio.h>
%}

%%
\n {printf("Enter a string: ");}
[a-zA-Z0-9]+[a-zA-Z0-9._]*[a-zA-Z0-9]+"@"[a-zA-Z0-9]+[a-zA-Z0-9.]+[a-zA-Z0-9]+
{printf("%s is a valid email.\n", yytext);}
.+ {printf("%s is not a valid email.\n", yytext);}
%%
int main(){
printf("Enter a string: ");
yylex();
return 0;
}
int yywrap()
{ return 1;
}
Output:

Name: Ayush Section: Roll no.


Uniyal E1 23

You might also like