%{
#include<stdio.h>
int count=0,flag=0,m=0;
%}
%%
((\/)(\/).*"\n") {count++;}
("/*")(.*|("\n")*) {flag=1;}
"*/" {if(flag==1){count++;flag=0;}}
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\/)).*) {m++;ECHO;}
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\*)).*((\*)(\/))) {m++;ECHO}
%%
main()
{
yyin=fopen("sample.c","r");
yyout=fopen("sample2.c","w");
yylex();
printf("no of comments=%d m=%d",+count,m);
}
where sample.c is your C program and output will be stored in sample2.c
STEP1:
YAC program includes three basic parts Definition,rules and subroutines respectively. These are separated by %% symbol as in the above program.
While compiling entire program will be converted to C program. then it is executed. Definition part will be just placed in the manner how it is present.
STEP2:
. matches any character other than new line.
* matches 0 or more number of preceding character.
[ ] matches any character within it
$ matches the end of the line.
| implies or ex: a|b implies either a or b
( ) groups a series of regular expressions
STEP3:
((\/)(\/).*"\n") implies--> matches // followed by any number of character or digit and ends with a new line. which is nothing but a single line comment.
\ used to nullify the effect of / which is present next to i.
( ) is used to combine one or more regular expressions.
("/*")(.*|("\n")*) matches /* followed by any number of character or digit or any number of new line. which is nothing but a multi-line comment.
("/*") implies it must start with /*
(.*|("\n")*) implies any number of character or digit or any number of new line. ..
STEP4:
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\/)).*) it matches the single line comments if they are present in printf statements. These comments should not be removed. so at the end it will ask you to add into the resulting program.
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\*)).*((\*)(\/))) matches the multi- line comments if they are present in printf statements. These comments should not be removed. so at the end it will ask you to add into the resulting program.
STEP5:
sample.c is your input file
sample2.c is your resulting comment free c file. program must be stored with .l extension.
to compile and run in lex follow the commands:
$lex programname.l
$cc lex.yy.c -ll
$./a.out
let sample.c be then for an efficient algorithm you
should get output as:
#include<stdio.h> #include<stdio.h>
// this is a exaple void main()
void main() {
{ int a,b,c;
int a,b,c; c=a+b;
//to add numbers printf(" // then c=%d ",c);
/* to store printf(" now /* __*/ c=%d",c);
the result getch();
in the c */ }
c=a+b;
printf(" // then c=%d ",c);
printf(" now /* __*/ c=%d",c);
getch();
// end
}
just check whether your program is efficient or not.....
#include<stdio.h>
int count=0,flag=0,m=0;
%}
%%
((\/)(\/).*"\n") {count++;}
("/*")(.*|("\n")*) {flag=1;}
"*/" {if(flag==1){count++;flag=0;}}
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\/)).*) {m++;ECHO;}
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\*)).*((\*)(\/))) {m++;ECHO}
%%
main()
{
yyin=fopen("sample.c","r");
yyout=fopen("sample2.c","w");
yylex();
printf("no of comments=%d m=%d",+count,m);
}
where sample.c is your C program and output will be stored in sample2.c
STEP1:
YAC program includes three basic parts Definition,rules and subroutines respectively. These are separated by %% symbol as in the above program.
While compiling entire program will be converted to C program. then it is executed. Definition part will be just placed in the manner how it is present.
STEP2:
. matches any character other than new line.
* matches 0 or more number of preceding character.
[ ] matches any character within it
$ matches the end of the line.
| implies or ex: a|b implies either a or b
( ) groups a series of regular expressions
STEP3:
((\/)(\/).*"\n") implies--> matches // followed by any number of character or digit and ends with a new line. which is nothing but a single line comment.
\ used to nullify the effect of / which is present next to i.
( ) is used to combine one or more regular expressions.
("/*")(.*|("\n")*) matches /* followed by any number of character or digit or any number of new line. which is nothing but a multi-line comment.
("/*") implies it must start with /*
(.*|("\n")*) implies any number of character or digit or any number of new line. ..
STEP4:
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\/)).*) it matches the single line comments if they are present in printf statements. These comments should not be removed. so at the end it will ask you to add into the resulting program.
([pP][rR][Ii][Nn][Tt][Ff][(].*((\/)(\*)).*((\*)(\/))) matches the multi- line comments if they are present in printf statements. These comments should not be removed. so at the end it will ask you to add into the resulting program.
STEP5:
sample.c is your input file
sample2.c is your resulting comment free c file. program must be stored with .l extension.
to compile and run in lex follow the commands:
$lex programname.l
$cc lex.yy.c -ll
$./a.out
let sample.c be then for an efficient algorithm you
should get output as:
#include<stdio.h> #include<stdio.h>
// this is a exaple void main()
void main() {
{ int a,b,c;
int a,b,c; c=a+b;
//to add numbers printf(" // then c=%d ",c);
/* to store printf(" now /* __*/ c=%d",c);
the result getch();
in the c */ }
c=a+b;
printf(" // then c=%d ",c);
printf(" now /* __*/ c=%d",c);
getch();
// end
}
just check whether your program is efficient or not.....
Hi, thank you for this informative post. I like your site and your posts on your website. I just wanted to thank you and wish you good luck for your this work. Wish you more success.
ReplyDeleteSocial Media Marketing Services in Delhi
Really good but when I tried something like /* "Hello World ! */, the program didn't delete the string
ReplyDeleteThank You and that i have a keen offer: Whole Home Renovation Cost home reno costs
ReplyDeleteCool and I have a super supply: How To Design House Renovation split level remodel
ReplyDelete