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. [VTU SS&OS]

vtu ss&os 1b
Code:

Algorithm:
Step 1: Start
Step 2: Read Input File Name and Output File Name
Step 3: Check the condition for end of Input File. If not end of the input file proceed to Step 4 else go to Step 9 
Step 4: Read the file line by line.
Step 5: Initialization of comment count, cc=0
Step 6: Check if comment line is present, then proceed to Step 7 else go to Step 8
Step 7: Increment comment count, cc=cc+1 and return to Step 3
Step 8:Print the line to output file and return to Step 3
Step 9:Display the number of comment lines.
Step 10: Stop

Explanation:
This program counts number of comment lines in the given input file and copies the content of input file to output file with comment lines omitted. Logic is simple. We redirect the default input output of lex STD i/o yyin and yyout to our specified input and output file. Whenever a comment is encountered we do nothing and continue. Wherever it is not comment, it directs the scanned characters to yyout which is our output file. This way copying is done. Meanwhile counting the comment lines is done as follows

%{
int cc=0;
%}

This is declaration section with a count variable which is initially set to 0.

%x CMNT 
%%
"/*"     {BEGIN CMNT; }

'%x' is used to define a State. and a state is begun by using 'BEGIN statename'.
first line indicates a state declaration named as CMNT.
we will enter this state whenever a comment line (starting with /*) is matched.In the third line, we enter CMNT state on matching '/*' symbol.
Condition checks while being in CMNT state is done as follows:

<CMNT>.  ;
<CMNT>\n ;
<CMNT>"*/" {BEGIN 0; cc++;}

While being in CMNT, if you encounter a single character represented by '.' (dot) is matched, You simply ignore it (using ; semicolon symbol).
While being in CMNT, if you encounter '\n', you ignore it too, since it is not the end of the multi line comment and you need not count it.
But if you encounter '*/' when being in CMNT, it is the end of the comment, so Begin 0 state, (which is the default state of Lex) and increment count by 1. Since this is the end of the commment and you need its count.

This was for multi line comments. Below code is for single line comment:

.*”//”.* {BEGIN 0; cc++;}
%%

First line is the regex for single line comments. Count on matching it and enter into default state (state 0).
Counting job is done. 

main(int argc, char *argv[])
{
if(argc !=3 )
printf(" Usage: %s <src file> <dst file>\n",argv[0]);
return;
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf(" No. of Comment Lines: %d\n",cc);

}

This is main function with command line arguments. argc indicates number of arguments passed. It must be 3. (./a.out, input file, output file). If it is not 3, then print the error message and exit. 
If it is three arguements then,
redirect yyin to argv[1] , specified input file
redirect yyout to argv[2] , specified output file
then call yylex() and at last print the number of comment lines.

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!