Professional Documents
Culture Documents
Tobeuploaded
Tobeuploaded
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 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.
%{
#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.
%{
#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;
}
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} {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;
}
9
8. WAP to check for valid identifier.
%{
#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.
%{
#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.
%{
#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;
}
Input :
2147483647
21474836479
-123
Output :
14
11 . WAP to print a string that starts and ends with “a”.
%{
#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 :
%{
#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 :
Output :
17
13 . WAP to convert decimal to hexadecimal.
%{
#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;
}
Input :
2482
19
123
14
Output :
20