This post was published long ago, when I was a student and an amateur blogger. The links might be outdated and content may not be useful anymore. Please read this content keeping its age in mind.

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

1b. Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file.

%{
int c=0,state=1;
%}

%%
“/” { state=0;}
/” { c++; if (!state) state=1;}
{ if (state==1)
fprintf(yyout,”%s”,yytext);
}
%%

FILE * fp;
main(int argc,char ** argv)
{
if(argc<=1)
{
printf(“nNo file”);
exit(1);
}

fp=fopen(argv[1],"w");

if(!fp)
{
    printf("nNo output file");
    exit(1);
}

yyout=fp;

fp=fopen(argv[1],"r");

if(!fp)
{
    printf("nNo inpput file");
    exit(1);
}

yyin=fp;
yylex();
printf("nNumber of comment lines : %d",c);

}

yywrap()
{
if(state==0)
{
printf(“nUnterminated commennt”);
return 1;
}
}