Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to count the number of


vowels and consonants

SOURCE CODE-
%{
int v=0,c=0;
%}

%%
[aeiouAEIOU]+ {v++;}
[a-zA-Z] {c++;}

"\n" {
printf("Number of vowels: %d\n", v);
printf("Number of consonants: %d\n", c);
v=0;
c=0;
}
%%
int main()
{
printf("Enter the string:\n");
yylex();
return 0;
}
intyywrap(){}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3
Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify adverb, adjectives,


noun.

SOURCE CODE-
%{
#include<stdio.h>
%}
%%
think|ride|run|loudly|camly|quickly|jump|angrily|roast|say {
printf("%s is a verb.\n",yytext);
}
president|apple|lily|disneyland|minister|mother|father|dehradun|uk|india {
printf("%s is a noun.\n",yytext);
}
big|large|few|millions|eleven|enormous|many {
printf("%s is an adjective.\n",yytext);
}
%%
intyywrap(){ return 1;}
int main()
{
printf("Enter a word:");
yylex();
return 0;
}
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify positive and


negative numbers.

SOURCE CODE-
%{
#include<stdio.h>
%}

%%
^[-][0-9]+ {printf("%s is a Negative Number.\n",yytext);}
[0-9]+ {printf("%s is a Positive Number.\n",yytext);}
%%
intyywrap(){}
int main()
{
printf("Enter a number:");
yylex();
return 0;
}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify positive and


negative numbers in fraction.

SOURCE CODE-
%{
#include<stdio.h>
int p=0,n=0;
%}
DIGIT [0-9]
%%
\+?{DIGIT}*\.{DIGIT}+ {p=p+1;}
-{DIGIT}*\.{DIGIT}+ {n=n+1;}

"\n" {
printf("\nPositive Number in fraction: %d\n",p);
printf("\nNegative Number in fraction: %d\n",n);
p=0;
n=0;
}
%%

intyywrap(){}
int main(){
yylex();

return 0;
}
OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify a valid keyword.

SOURCE CODE-
%{
#include<stdio.h>
%}

%%
for|do|while|int|float|char|string|if|else|switch|long|short|signed|unsigned|try|catch {
printf("%s is a Keyword\n",yytext);
}
.* {printf("\nNot a valid keyword, Wrong Input!");}
%%
intyywrap(){ return 1;}
int main()
{
printf("Enter an input:");
yylex();
return 0;
}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to display a pre define


message when encountered“Hi”.

SOURCE CODE-
%{
#include<stdio.h>
%}
%%
"hi" {printf("\nBye");}
([A-Za-z])* {printf("\nWrong Input");}
%%
int main(){
yylex();
return 0;
}
intyywrap(){}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alexprogram to identify whether a number


is even or odd.

SOURCE CODE-
%{
#include<stdio.h>
intnum;
%}
%%
[0-9]+ {
num=atoi(yytext);
if(num%2==0)
printf("\nNumber is Even");
else
printf("\nNumber is Odd");
}
%%
int main(){
printf("\nEnter a Number:");
yylex();
return 0;
}
intyywrap(){}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write a lex program to count the length of longest


word.

SOURCE CODE-
%{
#include<stdio.h>
int c=0;
%}

%%
[a-zA-Z]+ {
if (yyleng> c)
c=yyleng;
}

"\n" {printf("\nLongest Word Count: %d\n",c);}


%%
intyywrap(){}
int main()
{
printf("Enter a string:\n");
yylex();
return 0;
}
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify whether given


character is lower case or upper case.

SOURCE CODE-
%{
#include<stdio.h>
%}

%%
[a-z] {printf("Character is in Lower Case");}
[A-Z] {printf("Character is in Upper Case");}
%%

int main()
{
printf("Enter the Character:\n");
yylex();
return 0;
}
intyywrap(){}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify whether a given


sentence is simple or compound.

SOURCE CODE-
%{
#include<stdio.h>
int flag=0;
%}
%%
[aA][nN][dD]+ {flag=1;}
[bB][uU][tT]+ {flag=1;}
[oO][rR]+ {flag=1;}

"\n" {
if(flag==1){
printf("\nIt is a Compound Sentence\n");
flag=0;
}
else
printf("\nIt is a Simple Sentence\n");
}
%%
int main(){
printf("Enter a sentence:\n");
yylex();
return 0;
}
intyywrap(){}

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to identify an identifier.

SOURCE CODE-
%{

#include<stdio.h>

%}

%%

^[a-zA-Z_][a-zA-Z0-9_]* {printf("\nValid Identifier\n");}

.* {printf("\nNot a Valid Identifier\n");}

%%

intyywrap(){}

int main(){

printf("\nEnter a Word:\n");

yylex();

return 0;

OUTPUT-
Name:Sakshi Aggarwal course:MCA-3

Student id-20712081 section:B

PROBLEM STATEMENT:Write alex program to count the length of a given


sentence.

SOURCE CODE-
%{

#include<stdio.h>

intlen=0;

%}

%%

[a-zA-Z0-9 ]+ {len=yyleng;}

"\n" {printf("\nLength of the string: %d\n",len);}

%%

intyywrap(){}

int main(){

printf("\nEnter a string:\n");

yylex();

return 0;

OUTPUT-

You might also like