Delete Comments from Source Code

I Want to delete the comments from my C source code file writing sourc

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 8403 Views - Last Post: 12 July 2008 - 08:40 AM Rate Topic: -----

#1 santosh234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-July 08

Delete Comments from Source Code

Posted 09 July 2008 - 05:32 AM

I have a program written in C.

Now I want to delete the comments from my C source code file and that is again using C source code program file.
Is This A Good Question/Topic? 0
  • +

Replies To: Delete Comments from Source Code

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Delete Comments from Source Code

Posted 09 July 2008 - 05:35 AM

Well go ahead and delete them then...

:?:
Was This Post Helpful? 0
  • +
  • -

#3 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Delete Comments from Source Code

Posted 09 July 2008 - 06:58 AM

You have two cases

if "//"
    delete starting from "//" till "/n"
else if "/*"
    delete starting from "/*" till "*/" //you may need to delete many lines


Is that what you want!!!!
Was This Post Helpful? 0
  • +
  • -

#4 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Delete Comments from Source Code

Posted 09 July 2008 - 07:00 AM

It sounds like he wants a program that can strip all comments from a source file. Short of writing on, I haven't run in to a program like that. Comments tend to be useful, well, assuming they were added for a good reason.
Was This Post Helpful? 0
  • +
  • -

#5 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Delete Comments from Source Code

Posted 09 July 2008 - 08:03 AM

If you want a program to go through a source code file for instance, let's say for the sake of argument a text file. If you use some basic file in/out functions provided in the Standard Template Library you should be able to very easily extract certain delimiters in this case the "//" or "/*' or '*/'. Another possibility would be to use a stack implementation somewhat similar to what compilers use to check errors in basic code.
Was This Post Helpful? 1

#6 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Delete Comments from Source Code

Posted 09 July 2008 - 09:32 AM

I have a text editor that has the option to delete all comments...
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Delete Comments from Source Code

Posted 09 July 2008 - 09:53 AM

I think NotePad++ has that option, come to think of it.
Was This Post Helpful? 0
  • +
  • -

#8 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: Delete Comments from Source Code

Posted 09 July 2008 - 10:38 AM

View Postcaptainhampton, on 9 Jul, 2008 - 10:03 AM, said:

If you want a program to go through a source code file for instance, let's say for the sake of argument a text file. If you use some basic file in/out functions provided in the Standard Template Library you should be able to very easily extract certain delimiters in this case the "//" or "/*' or '*/'. Another possibility would be to use a stack implementation somewhat similar to what compilers use to check errors in basic code.


He specifically said he needs a C application to do this which would rule out STL. The only option really is to go through the file line by line and look for comments. You can't just look at the beginning of the line either as a comment can start at any point in the line. In the case of a C compliant comment (/* - */) you could have code after the end of a comment (though I certainly wouldn't encourage such programming practices.) If you want it to handle all cases then you have to consider the fact that //, /*, and */ are not comments if they are within quotations (meaning they get printed if in a print statement or something similar.) It's not a very difficult application but it is a little more complicated then it would seem when first conceived due to a few potential pitfalls.
Was This Post Helpful? 0
  • +
  • -

#9 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Delete Comments from Source Code

Posted 09 July 2008 - 11:16 AM

A string tokenizer and or stack implemented in C could do the trick I think.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Delete Comments from Source Code

Posted 09 July 2008 - 05:10 PM

Hmm... awk is written in C, you can use that:

cat foo.cpp | awk '{FS="//"; if (!(NF>1 && $1=="")) { print $1; } }' | awk '{RS=""; FS=""; gsub(/\/\*.*\*\//,"");print}'



If you want to write your own C program to do this, you'll have to offer up some of your own source code first, I'm afraid.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#11 bizzehdee  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 54
  • Joined: 06-April 08

Re: Delete Comments from Source Code

Posted 10 July 2008 - 02:35 PM

what it sounds like, is that someone wants to remove all the comments from someone elses application, change the variable names and filenames, and pass it off as their own.
Was This Post Helpful? 0
  • +
  • -

#12 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Delete Comments from Source Code

Posted 10 July 2008 - 02:43 PM

I thought that, but they might just want to create super-small source files :)
Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Delete Comments from Source Code

Posted 10 July 2008 - 02:54 PM

1st off, I wouldn't strip comments, like previously pointed out they are extremely useful. As well, when a program is compiled, the comments do not exist in the executable, so they don't add to the file size.

Anyhow, that being said. If I were to write this program I would open 2 files. One is the file with the source code, & the 2nd is the output file. Then start reading one character at a time from the source code file. When a slash is found, if the next character is another slash or an astricks, I would turn on a delete flag (number one) if two slashes are found, & another delete flag (number two) if , & then stop sending output to the output file. Then if delete flag number one is set, unset it when the end of the line is reached, & if delete flag number two is set, leave it set until the ending */ is found.
Was This Post Helpful? 0
  • +
  • -

#14 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Delete Comments from Source Code

Posted 10 July 2008 - 04:04 PM

Here's an example of stripping comments out of a source file (this works for C and C++ style comments):

#define forever for(;;)/>

void strip_comments(FILE* infile, FILE* outfile)
{
    int state = 0;
    forever {
        int c = fgetc(infile);
        if(feof(infile)) break;
        int output = 0;
        RESCAN: switch(state) {
            case 0:
               if(c == '/') state = 1;
               else if(c == '\"') { state = 6; output = 1; }
               else output = 1; 
               break;

            case 1:
               if(c == '/') state = 2;
               else if(c == '*') state = 3;
               else { output = 2; state = 0; }

            case 2:
               if(c == '\n' || c == '\r') { c = '\n'; output = 1; state = 4; }
               break;

            case 3:
               if(c == '*') state = 5;
               break;

            case 4:
               if(c != '\n' && c != '\r') { state = 0; goto RESCAN; }
               break;

            case 5:
               if(c == '/') state = 0;
               else state = 3;
               break;
               
            case 6:
            	if(c == '\"') state = 0;
            	else if(c == '\\') state = 7;
            	output = 1;
            	break;
            	
            case 7:
            	output = 1;
            	state = 6;
            	break;
            	
        }

        switch(output) {
            case 2:
                fputc('/', outfile);
            case 1:
                fputc(c, outfile);
                break;
        }
    }

    return;
}


Was This Post Helpful? 0
  • +
  • -

#15 papajai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-July 08

Re: Delete Comments from Source Code

Posted 11 July 2008 - 09:26 AM

why delete comments? professor and other programmers want documentations right?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2