Lex Program-Shivani Mca3b

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

......................................................................................................................................................

NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052


......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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;
}
int yywrap(){}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex program to identify adverb, adjectives,


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

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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);}
%%
int yywrap(){}
int main()
{
printf("Enter a number:");
yylex();
return 0;
}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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;
}
%%

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

return 0;
}
OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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!");}
%%
int yywrap(){ return 1;}
int main()
{
printf("Enter an input:");
yylex();
return 0;
}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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;
}
int yywrap(){}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex program to identify whether a number


is even or odd.
SOURCE CODE-
%{
#include<stdio.h>
int num;
%}
%%
[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;
}
int yywrap(){}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA 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);}


%%
int yywrap(){}
int main()
{
printf("Enter a string:\n");
yylex();
return 0;
}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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;
}
int yywrap(){}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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;
}
int yywrap(){}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex 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");}
%%
int yywrap(){}
int main(){
printf("\nEnter a Word:\n");
yylex();
return 0;
}

OUTPUT-
......................................................................................................................................................
NAME - SHIVANI COURSE - MCA (3RD - ‘B’) STUDENT ID - 20712052
......................................................................................................................................................

SUBJECT – COMPILER CONSTRUCTION LAB


PMC-302

PROBLEM STATEMENT: WA lex program to count the length of a given


sentence.
SOURCE CODE-
%{
#include<stdio.h>
int len=0;
%}
%%
[a-zA-Z0-9 ]+ {len=yyleng;}

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


%%
int yywrap(){}
int main(){
printf("\nEnter a string:\n");
yylex();
return 0;
}

OUTPUT-

You might also like