Sei sulla pagina 1di 9

Matching of Hello

%%
{
hello||HELLO{ printf("matching"); }
.*{ printf("not matching"); }
}
main()
{
printf("enter the text:");
yylex();
}
int yywrap()
{
}

String with every b followed by two a


%%
{
(a*+(baa)*)* { printf(accept); }
.*{printf(not accept); }
}
%%
main()
{
printf(enter the text:);
yylex();
}
int yywrap()
{
}

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()
{}

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<l;i++)
{
printf("%c",yytext[i]-32);
}
}
{words} { printf("%s",yytext); }
}
%%
main()
{
printf("enter the string");
yylex();
}
int yywrap()
{
}

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<i;j++)
{

write(op,a[j],strlen(a[j]));
}
}
yywrap()
{
}

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");
else
printf("not accepted");
}
int yywrap()
{
}

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()
{
}

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()
{
}

Potrebbero piacerti anche