Sss
Sss
Roll No : 60
Section : H
University Roll No : 2019126
Q1. Design a LEX Code to count the number of lines, space, tab-meta character,
and rest of characters in each Input pattern.
%{
#include<stdio.h>
int sc=0,tc=0,lc=0,chc=0;
%}
%%
[\n] lc++;
([ ])+ sc++;
\t tc++;
. chc++;
%%
int yywrap( )
{
return 1;
}
int main()
{
printf("Enter the input:\n");
yylex();
printf("The number of lines=%d\n",lc);
printf("The number of spaces=%d\n",sc);
printf("The number of tabs=%d\n",tc);
printf("The number of characters are=%d\n",chc);
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q2. Design a LEX Code to identify and print valid Identifier of C/C++ in given
Input pattern.
%{
#include<stdio.h>
%}
%%
([a-zA-Z][0-9])+|[a-zA-Z]* {printf("Identifier\n");}
^[0-9]+ {printf("Not a Identifier\n");}
.|\n;
%%
int yywrap()
{
return 1;
}
int main(void)
{
printf("Enter the input:\n");
yylex();
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q3. Design a LEX Code to identify and print integer and float value in given
Input pattern.
%{
#include<stdio.h>
%}
%%
[0-9]+"."[0-9]+ { printf("\nDecimal Number\n");}
[0-9]+ { printf("\nInteger Number\n");}
%%
int yywrap()
{
return 1;
}
int main(void)
{
printf("Enter the input:\n");
yylex();
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q4. Design a LEX Code for Tokenizing (Identify and print OPERATORS,
SEPERATORS, KEYWORDS, IDENTIFERS) in the C-fragment:
%{
#include<stdio.h>
int n=0;
%}
%%
"while"|"if"|"else" {n++;printf("\t keywords : %s", yytext);}
"int"|"float" {n++;printf("\t keywords : %s", yytext);}
[a-zA-Z_][a-zA-Z0-9_]* {n++;printf("\t identifier : %s", yytext);}
"<="|"=="|"="|"++"|"-"|"*"|"+" {n++;printf("\t operator : %s", yytext);}
[(){}|, ;] {n++;printf("\t separator : %s", yytext);}
.;
%%
int yywrap()
{
return 1;
}
int main(void)
{
printf("Enter the input:\n");
yylex();
printf("\n Total no. of tokens are : %d\n",n);
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q5. Design a LEX Code to count and print the number of total characters,
words, white spaces in given 'Input.txt' file.
%{
#include<stdio.h>
int tchar=0,tword=0,tspace=0;
%}
%%
" " {tspace++;tword++;}
[\t\n] tword++;
[^\n\t] tchar++;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("Input.txt","r");
yylex();
printf("Number of character: %d\nNumber of words:%d\nNumber of
spaces %d\n",tchar,tword,tspace);
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q6. Design a LEX Code to replace white spaces of 'Input.txt' file by a single
blank character into 'Output. txt' file.
%{
#include<stdio.h>
%}
%%
[\t" "]+ fprintf(yyout," ");
.|\n fprintf(yyout,"%s",yytext);
%%
int yywrap()
{
return 1;
}
int main(void)
{
yyin=fopen("Input.txt","r");
yyout=fopen("Output.txt","w");
yylex();
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q7. Design a LEX Code to remove the comments from any C-Program given at
run-time and store into 'out.c' file.
%{
#include<stdio.h>
%}
%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
int yywrap()
{
return 1;
}
Q8. Design a LEX Code to extract all html tags in the given HTML file at run
time and store into Text file given at run time.
%{
#include<stdio.h>
%}
%%
\<[^>]*\> fprintf(yyout,"%s\n",yytext);
.|\n;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("input.html","r");
yyout=fopen("output8.txt","w");
yylex();
return 0;
}
Name : Shubam Malhotra
Roll No : 60
Section : H
University Roll No : 2019126
Q9. Design a DFA in LEX Code which accepts string containing even number of
‘a’ and even number of ‘b’ over input alphabet{a,b}.
%{
#include<stdio.h>
%}
reg a(a|b)*b
%%
{reg} {printf("Accepted");}
.* {printf("Not Aceepted");}
%%
int yywrap()
{
return 1;
}
int main(){
printf(“Enter the String of and b only : “);
yylex();
return 0;
}
Q10. Design a DFA in LEX Code which accepts string containing even a
substring “aab” or “baba” over input alphabet{a,b}.
%{
#include<stdio.h>
%}
reg1 (a|b)*aab(a|b)*
reg2 (a|b)*baba(a|b)*
%%
{reg1} {printf("Accepted");}
{reg2} {printf("Accepted");}
.* {printf("Not Accepted");}
%%
int yywrap()
{
return 1;
}
int main()
{
printf("Enter String\n");
yylex();
return 0;
}