You are on page 1of 14

Assignment No.

Date :

Assignment Name

: Assignment to understand basic syntax of LEX specifications,


built in function & variables.

Batch

Roll No.

---------------------------------------------------------------------------------------------------------------1. Eliminates the White Spaces, Line, Words and Symbol & Characters from the
given program.

%{
#include<stdio.h>
int cnts=0,cntl=0,cntss=0;
char cntc=0,w=0;
%}
%%
[A-Za-z]* {cntc=cntc+yyleng;w++;}
[\n]+ {cntl++;}
[ \t]+ {cnts++;}
[^\n| \t|A-Za-z|0-9] {cntss++;}
%%
main()
{
yyin=fopen("/home/student/input.txt","r");
yylex();
printf("no. of White space:- %d\n",cnts);
printf("no. of Words:- %d\n",w);
printf("no. of line:-%d\n",cntl);
printf("no. of char:-%d\n",cntc);
printf("no. of symbols:-%d\n",cntss);
}

int yywrap()
{
return(1);
printf("\n");
}
input.txt :
I am studying in Sapkal knowledge hub...
$$$Life is beautiful.$$$
Output :
student@PGRLab-15:~$ lex ass1.l
student@PGRLab-15:~$ cc lex.yy.c -ll
student@PGRLab-15:~$ ./a.out
no. of White space:- 8
no. of Words:- 10
no. of line:-2
no. of char:-46
no. of symbols:-10

2. Count Number of vowels & consonants.

%{
#include<stdio.h>
int v=0;
int c=0;
char str[10];
%}
%%
[aeiouAEIOU] {v++;}
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] {c++;}
.|\n {}
%%
main()
{
yyin=fopen("p1.txt","r");
yylex();
printf("no.of vowels are:%d \n",v);
printf("no.of consonants are:%d \n",c);
}
int yywrap()
{
return 1;
}
p1.txt :
Sapkal knowledge hub

Output :
student@PGRLab-14:~$ gedit prog2.l
student@PGRLab-14:~$ lex prog2.l
student@PGRLab-14:~$ cc lex.yy.c
student@PGRLab-14:~$ ./a.out
no.of vowels are:6
no.of consonants are:12

3. Read input from file find & replace a given string.


%{
#include<stdio.h>
#include<string.h>
char str1[10],str2[10];
FILE *fp;
%}
%%
[a-z]+ {if(strcmp(str1,yytext)==0)
{
fprintf(fp,"%s",str2);
}
else
{
fprintf(fp,"%s",yytext);
}
}
[ \t] {fprintf(fp,"%s",yytext);}
%%
main()
{
printf("Enter the string to be search:");
scanf("%s",str1);
printf("Enter the string to be replace:");
scanf("%s",str2);
yyin=fopen("s1.txt","r");
fp=fopen("s2.txt","w");
yylex();
}

Output :
student@PGRLab-21:~$ lex mmm.l
student@PGRLab-21:~$ cc lex.yy.c -ll
student@PGRLab-21:~$ ./a.out
Enter the string to be search:india
Enter the string to be replace:japan
S1.txt
india is great
S2.txt
japan is great

4. Change Case ( Upper Case, Lower Case, Toggle, Sentence)


%{
#include<string.h>
char str[20],ch;
int i,chh;
%}
%%
[0-9|y|Y|S|s]

do
{
printf("__________ MENU __________\n");
printf("\n 1. Upper To Lower");
printf("\n 2. Lower To Upper");
printf("\n 3. Sentence Form");
printf("\n 4. Toggle");
printf("\n 5. Exit");
printf("\n Enter Your Choice : ");
scanf("%d",&chh);
switch(chh)
{
case 1:
printf("\nEnter String in Upper Case : ");
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
str[i]+=32;
}
printf("%s", str);
break;
case 2:
printf("\nEnter String in Lower Case : ");
scanf("%s",str);

for(i=0;i<strlen(str);i++)
{
str[i]-=32;
}
printf("%s", str);
break;
case 3:
printf("\nEnter String : ");
scanf("%s",str);
if(str[0]>=91)
{
str[0]-=32;
}
for(i=1;i<=strlen(str);i++)
{
if(str[i]>=65 && str[i]<=90)
str[i]+=32;
}
printf("%s",str);
break;
case 4:
printf("\nEnter String In Toggle Form : ");
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
if(91<=str[i] && str[i]<=116)
str[i]-=32;
else if(65<=str[i] && str[i]<=90)
str[i]+=32;
}
printf("%s",str);
break;

case 5:
exit(0);
break;
}
printf("\nDo You Want To Continue ??? (Y/N) ");
scanf("%c",&ch);
if(ch=='N'| ch=='n')
exit(0);
}while(ch=='y' | ch=='Y');
}
%%
main()
{
printf("\nDo You Want To Start Program ??(S) : ");
yylex();
}
Output :
student@PGRLab-21:~$ lex ass11.l
student@PGRLab-21:~$ cc lex.yy.c -ll
student@PGRLab-21:~$ ./a.out
Do You Want To Start Program ??(S) : s
__________ MENU __________
1. Upper To Lower
2. Lower To Upper

3. Sentence Form
4. Toggle
5. Exit
Enter Your Choice : 1
Enter String in Upper Case : MADHURI
madhuri
Do You Want To Continue ??? (Y/N)
y
__________ MENU __________
1. Upper To Lower
2. Lower To Upper
3. Sentence Form
4. Toggle
5. Exit
Enter Your Choice : 2
Enter String in Lower Case : madhuri
MADHURI
Do You Want To Continue ??? (Y/N)
y
__________ MENU __________
1. Upper To Lower
2. Lower To Upper
3. Sentence Form

4. Toggle
5. Exit
Enter Your Choice : 3
Enter String : madhuri
Madhuri
Do You Want To Continue ??? (Y/N)
y
__________ MENU __________
1. Upper To Lower
2. Lower To Upper
3. Sentence Form
4. Toggle
5. Exit
Enter Your Choice : 4
Enter String In Toggle Form : madHURI
MADhuri
Do You Want To Continue ??? (Y/N)
y
__________ MENU __________
1. Upper To Lower
2. Lower To Upper
3. Sentence Form
4. Toggle

5. Exit
Enter Your Choice : 5
student@PGRLab-21:~$

5.

Find substring abc & convert to ABC.

%{
#include<stdio.h>
#include<string.h>
int i;
char str[20];
%}
%%
[a-z]+ {
strcpy(str,yytext);
for(i=0;i<strlen(str);i++)
{
if(str[i]==97)
str[i]='A';
if(str[i]==98)
str[i]='B';
if(str[i]==99)
str[i]='C';
}
{printf("\n string is:%s",str);}
}
%%
int main()
{
yylex();
}
int yywrap()

{
return(1);
}

Output :
student@PGRLab-16:~$ lex ass11.l
student@PGRLab-16:~$ cc lex.yy.c -ll
student@PGRLab-16:~$ ./a.out
abc
string is:ABC

You might also like