System Software Lab Programs ( Lex and Yaac Programs )1b

// June 8th, 2009 // Educational, Engineering, Unix and System Software Lab

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;
}
}

Comments

  1. Shashank says:

    This Answer doesn't solve eliminating the comments!!!!

  2. sandeephegde says:

    Dear Shashank,

    I have not eliminated any comments from the site. Which eliminated comments are you talking about?

  3. Shashank says:

    Hey Sandeep, I'm talking about the question which says that we need to eliminate the comments also along with counting no. of comments…Que goes like this:
    Program to count the numbers of comment lines in a given C program. Also ELIMINATE them and COPY THE RESULTING PROG INTO SEPARATE FILE. Ur prog just counts the comments, the rest of the que is left unsolved…