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

1. Lex program to display the same line as read from keyboard.

Input parameters :A file that has content written in it (can be txt, docs).
Output parameters : Output will be same as the contents of the file.
Program listing :

%{
#include <stdio.h>
%}

%%
. | \n {ECHO;} //Reads every character
%%

int yywrap()
{
return 1; //return 1 if no more processing is required
}

int main()
{
yyin=fopen("abc.txt", "r"); //opening the file in read mode
yylex();
return 0;
}

Input : This is a sample text.


Output :
2
2. Write a program to display only digits.

Input parameters :A text file containing numbers, words, new line, space, tabs, single
characters.
Output parameters :Only digits from the file should be visible.
Program Listing :
%{
#include <stdio.h>
%}

%%
[a-zA-Z]*//ignores strings containing alphabets
[\t |\n| |\r|\v]*//ignores tab, newline, escape characters
[0-9] {ECHO;} //to print only numbers
.
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}

Input : 12345--abc?67890
Output :

3
3. WAP to print line number of each line in a file.

Input parameters :A text file


Output parameters :Line number in front of each line
Program Listing :

%{
#include <stdio.h>
int i=2; //initializing variable to start counting from 2
%}

%%
\n {printf("\nLine %d. ", i++);} //printing line number and incrementing i
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("abc.txt", "r");
printf("Line 1. ");
yylex();
return 0;
}

Input :
This is line 1
this is line 2
Output :

4
4. WAP to remove comments from a file i.e to print all data except comments.

Input parameters :A text file containing comments (both single and multiline)
Output parameters :All text except the one written inside the comments
Program listing :

%{
#include <stdio.h>
%}

%%
\/\/(.*) //for single line comment
\/\*(.*)(\n)*(.*)(\n)*\*\/ //for multiline comment
. | \n {ECHO;}
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}

Input :
My name is Viruj Janweja /*multiline comment*/
//single line comment

Output :

5
5. WAP to check entered number is Integer or Float.

Input parameters : Text file containing a number


Output parameters :The number is assigned a integer or float tag
Program listing :

%{
#include <stdio.h>
%}

%%
[-][0-9]*\.[0-9]+ | //number starting from minus and checking for period
[+]?[0-9]*\.[0-9]+ {printf("%s : float", yytext);}
[-]0*[1-9]+ |
[+]?0*[0-9]+ {printf("%s : integer", yytext);} //ignoring starting zeros in front
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("number.txt", "r");
yylex();
return 0;
}

Input :
0.2
34
0000134
-89
-9.6

Output :

6
7
6. WAP to check a given substring in text.

Input parameters :A text file containing words in which a given substring has to be
searched.
Output parameters :Prints ‘Found’ if substring is found.
Program listing :

%{
#include <stdio.h>
int a=0; //0 means the string has not been found
%}

%%
[.\n]*(viruj)[.\n]* {printf("Substring found"), a=1;} //set the variable to 1 if string is
found
[ \t\n]+ |
.+
%%

int yywrap()
{
if(!a)
printf("Substring not found");
return 1;
}

int main()
{
yyin=fopen("test.txt", "r");
yylex();
return 0;
}

Input : my name is Escobar viruj his


Output :

8
7. WAP to convert text into uppercase.

Input parameters :A text file containing both lower and upper case alphabets.
Output parameters :Whole text file converted into uppercase by matching the ASCII
values.
Program listing :

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

lower [a-z] //regular definition

%%
{lower} {printf("%c",yytext[0]-32);} //converting a character in upper case if it is in
lower case
. | \n ECHO;
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}

Input : this text is in lower case


Output :

9
8. WAP to check for valid identifier.

Input :Text file containing keywords, numbers, identifiers.


Output : Recognizing a valid identifier. (it starts with an alphabet or underscore but not
with a digit)
Program listing :

%{
#include <stdio.h>
int a=0; //initially no valid identifier present
%}

%%
if|else|while|int|switch|for|char {printf("%s : Keyword", yytext);}
[a-zA-z_]+[a-zA-z0-9_]* {printf("%s : Valid identifier", yytext), a=1;}
[0-9]* {printf("%s : Number", yytext);}
[ \t\n]+ |
.+
%%

int yywrap()
{
if(!a)
printf("No valid identifier found");
return 1;
}

int main()
{
yyin=fopen("identifier.txt", "r");
yylex();
return 0;
}

Input :
if
_123as
123
viruj

10
Output :

11
9. WAP to check given string is verb or noun.

Input parameters :Text file containing verbs and nouns.


Output parameters :Categorizing a word as noun or verb
Program listing :

%{
#include <stdio.h>
%}

%%
[\t\n ]+
is|am|are|were|was|be|being|been|do|does|did|will|would|should|can|could|has|have|had|cat
ch|go {printf("%s : is a verb\n",yytext);} //matching a verb
bed|cat|movie|train|country|book|phone|match|speaker|man|mountain|state|ocean|cat|milk|r
ice {printf("%s : is a noun\n",yytext);} //matching a noun
[a-zA-z]+ {printf("Might be noun or verb");}
.
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("q9.txt", "r");
yylex();
return 0;
}

Input :cat
Output :

12
10. WAP to identify signed or unsigned integer.

Input parameters :Text file containing a number.


Output parameters :If the number is smaller or equal to 2147483647, then it is signed
otherwise unsigned. Also all negative numbers are signed.
Program listing :

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

13
#include <math.h>
void check(char *); //function declaration
%}

%%
[-][0-9]+ {printf("%s : Signed", yytext);}
[0-9]+ check(yytext); //function calling
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("q10.txt", "r");
yylex();
return 0;
}

void check(char *a) //function definition


{
unsigned int len=strlen(a), num=0;
for(int i=0;i<len;i++)
num=num*10+(a[i]-'0');
if(num<=(pow(2,31)-1))
printf("%s : Signed", a);
else
printf("%s : Unsigned", a);
}

Input :
2147483647
21474836479
-123

Output :

14
11 . WAP to print a string that starts and ends with “a”.

Input parameters :Text file containing words.


Output parameters :Identifying those words that starts and ends with ‘a’.
Program listing :

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

%%

15
a[a-z]*a[ ] {printf("%s", yytext);}//checking each string with the given contraints
[ \t\n]+
.
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("q11.txt", "r");
yylex();
return 0;
}
Input :

adjglsfdgaasgfgcadfgdfaa

Output :

12 . WAP to count number of lines, words and characters in text file.

Input parameters :Text file containing lines, words and characters.


Output parameters :Number of lines, words, characters in the file.
Program listing :

%{
#include <stdio.h>
int nline=0, nchar=0, nwords=0; //initializing the variables
%}

%%

16
[a-zA-z]+ {
nwords++;
nchar=nchar+yyleng; //yyleng is the length of the character array
}
\n {nline++, nchar++;}
. {nchar++;} //incrementing when a single line character is matched
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("test.txt", "r");
yylex();
printf("Number of words : %d\n", nwords);
printf("Number of lines : %d\n", nline);
printf("Number of characters : %d\n", nchar);
return 0;
}

Input :

Hello my name is viruj


?>: oops

Output :

17
13 . WAP to convert decimal to hexadecimal.

Input parameters :A text file containing a decimal number.


Output parameters :Hexadecimal equivalent of the decimal number
Program listing :

%{
#include <stdio.h>
#include <string.h>
void convert(char *); //function declaration
%}

%%
[0-9]+ convert(yytext); //function calling
%%

18
int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("q13.txt", "r");
yylex();
return 0;
}

void convert(char *a) //function definition


{
int num=0, len=strlen(a),arr[100]={},i=0,tmp;
for(int j=0;j<len;j++)
num=num*10+(a[j]-'0');
while(num>16) //forming the hexadecimal number and storing in an array
{
tmp=num/16;
arr[i++]=num%16;
num=tmp;
}
num>=10 ?printf("%c",(55+num)) : printf("%d",num);
i--;
while(i>=0)
{
if(arr[i]>=10)
{
char y=55+arr[i];
printf("%c",y);
}
else
printf("%d",arr[i]);
i--;
}
}

Input :
2482

19
123
14

Output :

20

You might also like