View
222
Download
0
Category
Preview:
Citation preview
7/25/2019 pgms1
1/9
Matching of Hello
%%
{
hello||HELLO{ printf("matching"); }
.*{ printf("not matching"); }
}
main()
{
printf("enter the text:");
yylex();
}
int yywrap()
{
}
7/25/2019 pgms1
2/9
String with every b followed by two a
%%
{
(a*+(baa)*)* { printf(accept); }
.*{printf(not accept); }
}
%%
main()
{
printf(enter the text:);
yylex();
}
int yywrap()
{
}
7/25/2019 pgms1
3/9
Count characters,words and lines
letters [a-zA-Z]
space [ ]
dot [.]
%%
{
int words=1,c=0,sentence=0;
%
}
%%
{
{letters} { c++; }
{space} { words++; }
{dot} { sentence++; }
}
%%
main()
{
printf(enter the text:);
yylex();
printf(no of characters:%d,c);
printf(no of words:%d,words);
printf(no of sentences:%d,sentence);
}
int yywrap()
{ }
7/25/2019 pgms1
4/9
Capitalize keywords
keywords "if"|"else"|"while"|"do"|"for"|"int"|"float"|"char"
words [a-zA-Z]
%{
int i,l;%}
%%
{{keywords} { l=strlen(yytext);
for(i=0;i
7/25/2019 pgms1
5/9
Delete comments
letter [a-z,A-Z,0-9]%{
char a[1000][1000];
int i=0;%}%%
{
"//"{letter}* {strcpy(a[i++]," ");}"/*"+{letter}*+"*/" {strcpy(a[i++]," ");}
.* {strcpy(a[i++],yytext);}
}
%%main()
{
int j;int op;yyin=fopen(diga.txt","r");
yylex();
op=creat("5.txt","w");for(j=0;j
7/25/2019 pgms1
6/9
Accepting string with even number of b followed by odd number of a
%{int c1=0,c2=0;
%}
%%{a|A { c1++; }
b|B { c2++; }
}%%
main()
{
printf("ENTER THE STRING");yylex();
if((c1%2!=0)&(c2%2==0))
printf("accepted");elseprintf("not accepted");
}
int yywrap(){
}
7/25/2019 pgms1
7/9
Email id validation
letters [a-zA-Z0-9]sym "."|"_"
dom "gmail"|"yahoo"|"hotmail"
ext "com"|"co"|"in"|"org"%%{
({letters}+({letters}*+{sym}*)*+[@]+{dom}+[.]+{ext}) { printf("validated"); }
.* { printf("invalid"); }}
%%
main()
{printf("enter the string:");
yylex();
}int yywrap(){
}
7/25/2019 pgms1
8/9
Bracket mismatching
brac "("|"{"|"["%{
char a[50][50];
int flag,i=-1;%}%%
{
{brac} {strcpy(a[++i],yytext);}")" { if(strcmp(a[i],"(")==0)
{flag=1;
i=i-1;}
else{flag=0;
return;} }
"}" { if(strcmp(a[i],"{")==0){flag=1;i=i-1;}
else
{flag=0;return;} }
"]" { if(strcmp(a[i],"[")==0)
{flag=1;
i=i-1;}else{flag=0;
return;} }
}%%
main()
{printf("enter the data:");
yylex();
if(flag==1&&i==-1)printf("matching");else printf("not matching");
}
int yywrap()
{}
7/25/2019 pgms1
9/9
Recommended