delete comments in .c file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Hagan

    #16
    Re: delete comments in .c file

    Jeremy Yallop wrote:[color=blue]
    >
    > Tim Hagan wrote:[color=green]
    > > Jeremy Yallop wrote:[color=darkred]
    > >>
    > >> Tim Hagan wrote:
    > >> > This will replace each /* ... */ style comment with a single space:
    > >> >
    > >> > #include<stdio. h>
    > >> > int main(void){i\
    > >> > nt c,p=-1,k=0,s=0
    > >> > ;while((c=getch ar
    > >> > ())!=EOF){if(s= =0
    > >> > ){if(p=='/'&&c==
    > >> > '*'){s=1;k=2;}e l\
    > >> > se if(c=='\"'&&p
    > >> > !='\\'&&p!='\'' )s
    > >> >=2;}else if(s==1)
    > >> > {if (p=='*'&&c==
    > >> > '/')s=0;}else if(
    > >> > s==2){if(c=='\" '
    > >> > &&p!='\\')s=0;} if
    > >> > (k==1)putchar(' '
    > >> > );if(p>0&&s!=1) {
    > >> > if(!k)putchar(p );
    > >> > if(--k<0)k=0;}p=c
    > >> > ;}putchar(p);re t\
    > >> > urn 0;}
    > >>
    > >> It doesn't handle line-splicing. Also, putchar(-1) is not portable.[/color]
    > >
    > > putchar(-1) is never executed in the above code[/color][/color]

    .... unless one tries to remove the comments from an empty file. :-)
    [color=blue]
    > putchar(-1) is executed if EOF is encountered immediately.[/color]

    So just insert 'if (p > 0)' before the final putchar.

    --
    Tim Hagan

    Comment

    • Richard Heathfield

      #17
      Re: [OT] delete comments in .c file

      Mark A. Odell wrote:
      [color=blue]
      > Now what does this have to do with the C language? The C
      > language does not specify how to delete comments.[/color]

      The Standard says: "Each comment is replaced by one space character." If
      that doesn't specify how to delete comments, I don't know what does.

      --
      Richard Heathfield : binary@eton.pow ernet.co.uk
      "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
      K&R answers, C books, etc: http://users.powernet.co.uk/eton

      Comment

      • Mark McIntyre

        #18
        Re: [OT] delete comments in .c file

        On Fri, 31 Oct 2003 22:47:18 +0000 (UTC), in comp.lang.c , Richard
        Heathfield <dontmail@addre ss.co.uk.invali d> wrote:
        [color=blue]
        >Mark A. Odell wrote:
        >[color=green]
        >> Now what does this have to do with the C language? The C
        >> language does not specify how to delete comments.[/color]
        >
        >The Standard says: "Each comment is replaced by one space character." If
        >that doesn't specify how to delete comments, I don't know what does.[/color]

        I guess Mark's point is that to be sure of being syntactically
        identical to the original you should replace all comments by a space.
        For instance
        int i = 23/* */12;
        should still generate a syntax error . :-)

        What the OP would expect it to do with
        double i = 32 //* */ 4
        ;
        is anyone's guess. I guess you'd have to have a C99 and a C89 mode.
        --
        Mark McIntyre
        CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
        CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


        ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
        http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
        ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

        Comment

        • Stephen Samuel

          #19
          Re: delete comments in .c file

          Here's a perl script which will handle *MOST* sane C code...

          Some things that it will miss (scan manually for it first:

          a double quote inside of single quotes (e.g.)
          char confusion = '"';

          C-99 // comments like this

          I'm sure that some people can come up with other convoluted counter-examples.

          It reads and plays with the entire file, so it will need to hold
          at least two or three copies of it in RAM. (for today's computers,
          that would be some number of megabytes).

          If you want any of the above fixed, feel free to send me a cheque.
          _______________ _______________ _______________ _______
          #!/usr/bin/perl
          $s=join("",<>);
          # printf "[[%s]]\n\n",$s;
          $s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/[[$1 ]]/g;
          printf "[[%s]]\n\n",$s;
          _______________ _______________ _______________ _______
          Yep, That's it... 5 lines including the shell header.

          Timex wrote:[color=blue]
          > I want to delete all comments in .c file.
          >
          > Size of .c file is very big.
          >
          > Any good idea to do this?
          >
          > Please show me example code.[/color]


          --
          Stephen Samuel +1(604)876-0426 samuel@bcgreen. com

          Powerful committed communication. Transformation touching
          the jewel within each person and bringing it to light.

          Comment

          • Irrwahn Grausewitz

            #20
            Re: delete comments in .c file

            Stephen Samuel <stephen_samuel @telus.net> wrote:
            [color=blue]
            > Here's a perl script which will handle *MOST* sane C code...[/color]
            <snip>

            Since when is perl topical in c.l.c?

            BTW:
            Does your "solution" account for comment delimiters inside string
            literals? (I'm unfortunately unable to decrypt the line-noise
            provided.)

            Regards
            --
            Irrwahn
            (irrwahn33@free net.de)

            Comment

            • CBFalconer

              #21
              Re: delete comments in .c file

              *** rude top-posting fixed ***

              Stephen Samuel wrote:[color=blue]
              > Timex wrote:
              >[color=green]
              > > I want to delete all comments in .c file.
              > >
              > > Size of .c file is very big.
              > >
              > > Any good idea to do this?
              > >
              > > Please show me example code.[/color]
              >
              > Here's a perl script which will handle *MOST* sane C code...
              >
              > Some things that it will miss (scan manually for it first:
              >
              > a double quote inside of single quotes (e.g.)
              > char confusion = '"';
              >
              > C-99 // comments like this
              >
              > I'm sure that some people can come up with other convoluted
              > counter-examples.
              >
              > It reads and plays with the entire file, so it will need to
              > hold at least two or three copies of it in RAM. (for today's
              > computers, that would be some number of megabytes).
              >
              > If you want any of the above fixed, feel free to send me a
              > cheque.
              > _______________ _______________ _______________ _______
              > #!/usr/bin/perl
              > $s=join("",<>);
              > # printf "[[%s]]\n\n",$s;
              > $s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/[[$1 ]]/g;
              > printf "[[%s]]\n\n",$s;
              > _______________ _______________ _______________ _______
              > Yep, That's it... 5 lines including the shell header.[/color]

              Please do not top-post.

              The following AFAIK does not have the above faults, and does not
              need to store any file copies, in fact not even any line copies.
              It will probably be at least an order of magnitude faster.

              /* File uncmntc.c - demo of a text filter
              Strips C comments. Tested to strip itself
              by C.B. Falconer. 2002-08-15
              Public Domain. Attribution appreciated
              report bugs to <mailto:cbfalco ner@worldnet.at t.net>
              */

              /* With gcc3.1, must omit -ansi to compile eol comments */

              #include <stdio.h>
              #include <stdlib.h>

              static int ch, lastch;

              /* ---------------- */

              static void putlast(void)
              {
              if (0 != lastch) fputc(lastch, stdout);
              lastch = ch;
              ch = 0;
              } /* putlast */

              /* ---------------- */

              /* gobble chars until star slash appears */
              static int stdcomment(void )
              {
              int ch, lastch;

              ch = 0;
              do {
              lastch = ch;
              if (EOF == (ch = fgetc(stdin))) return EOF;
              } while (!(('*' == lastch) && ('/' == ch)));
              return ch;
              } /* stdcomment */

              /* ---------------- */

              /* gobble chars until EOLine or EOF. i.e. // comments */
              static int eolcomment(void )
              {
              int ch, lastch;

              ch = '\0';
              do {
              lastch = ch;
              if (EOF == (ch = fgetc(stdin))) return EOF;
              } while (!(('\n' == ch) && ('\\' != lastch)));
              return ch;
              } /* eolcomment */

              /* ---------------- */

              /* echo chars until '"' or EOF */
              static int echostring(void )
              {
              putlast();
              if (EOF == (ch = fgetc(stdin))) return EOF;
              do {
              putlast();
              if (EOF == (ch = fgetc(stdin))) return EOF;
              } while (!(('"' == ch) && ('\\' != lastch)));
              return ch;
              } /* echostring */

              /* ---------------- */

              int main(void)
              {
              lastch = '\0';
              while (EOF != (ch = fgetc(stdin))) {
              if ('/' == lastch)
              if (ch == '*') {
              lastch = '\0';
              if (EOF == stdcomment()) break;
              ch = ' ';
              putlast();
              }
              else if (ch == '/') {
              lastch = '\0';
              if (EOF == eolcomment()) break;
              ch = '\n';
              putlast(); // Eolcomment here
              // Eolcomment line \
              with continuation line.
              }
              else {
              putlast();
              }
              else if (('"' == ch) && ('\\' != lastch)
              && ('\'' != lastch)) {
              if ('"' != (ch = echostring())) {
              fputs("\"Unterm inated\" string\n", stderr);
              fputs("checking for\
              continuation line string\n", stderr);
              fputs("checking for" "concat string\n", stderr);
              return EXIT_FAILURE;
              }
              putlast();
              }
              else {
              putlast();
              }
              } /* while */
              putlast(/* embedded comment */);
              return 0;
              } /* main */


              --
              Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net> USE worldnet address!


              Comment

              • Ed Morton

                #22
                Re: delete comments in .c file



                Timex wrote:[color=blue]
                > I want to delete all comments in .c file.
                >
                > Size of .c file is very big.
                >
                > Any good idea to do this?
                >
                > Please show me example code.[/color]

                Try "ncsl": http://www.lucentssg.com/displayProduct.cfm?prodid=33
                It strips all comments and indentation so just run an indenter (e.g.
                "indent") or a C beautifier (e.g. "cb" - google for "cb download
                beautifier" and take your pick) on the output to get it back in readable
                format. Disclaimer - I've never used this specific download of "ncsl",
                I've just used the version provided on UNIX boxes within Lucent.

                Ed.

                Comment

                • Stephen Samuel

                  #23
                  Re: delete comments in .c file

                  Here's a perl script which will handle *MOST* sane C code...

                  Some things that it will miss (scan manually for it first:

                  a double quote inside of single quotes (e.g.)
                  char confusion = '"';

                  C-99 // comments like this

                  I'm sure that some people can come up with other convoluted counter-examples.

                  It reads and plays with the entire file, so it will need to hold
                  at least two or three copies of it in RAM. (for today's computers,
                  that would be some number of megabytes).

                  If you want any of the above fixed, feel free to send me a cheque.
                  _______________ _______________ _______________ _______
                  #!/usr/bin/perl
                  $s=join("",<>);
                  # printf "[[%s]]\n\n",$s;
                  $s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/[[$1 ]]/g;
                  printf "[[%s]]\n\n",$s;
                  _______________ _______________ _______________ _______
                  Yep, That's it... 5 lines including the shell header.

                  One bug: Quoted strings have a space inserted after them.
                  Again: fixable, but not worth the trouble for free.



                  Timex wrote:[color=blue]
                  > I want to delete all comments in .c file.
                  >
                  > Size of .c file is very big.
                  >
                  > Any good idea to do this?
                  >
                  > Please show me example code.[/color]


                  --
                  Stephen Samuel +1(604)876-0426 samuel@bcgreen. com

                  Powerful committed communication. Transformation touching
                  the jewel within each person and bringing it to light.

                  Comment

                  • Stephen Samuel

                    #24
                    Re: delete comments in .c file

                    Here's a perl script which will handle *MOST* sane C code...

                    Some things that it will miss (scan manually for it first:

                    a double quote inside of single quotes (e.g.)
                    char confusion = '"';

                    C-99 // comments like this

                    I'm sure that some people can come up with other convoluted counter-examples.

                    It reads and plays with the entire file, so it will need to hold
                    at least two or three copies of it in RAM. (for today's computers,
                    that would be some number of megabytes).

                    If you want any of the above fixed, feel free to send me a cheque.
                    _______________ _______________ _______________ _______
                    #!/usr/bin/perl
                    $s=join("",<>);
                    # printf "[[%s]]\n\n",$s;
                    $s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/[[$1 ]]/g;
                    printf "[[%s]]\n\n",$s;
                    _______________ _______________ _______________ _______
                    Yep, That's it... 5 lines including the shell header.

                    One bug: Quoted strings have a space inserted after them.
                    Again: fixable, but not worth the trouble for free.



                    Timex wrote:[color=blue]
                    > I want to delete all comments in .c file.
                    >
                    > Size of .c file is very big.
                    >
                    > Any good idea to do this?
                    >
                    > Please show me example code.[/color]


                    --
                    Stephen Samuel +1(604)876-0426 samuel@bcgreen. com

                    Powerful committed communication. Transformation touching
                    the jewel within each person and bringing it to light.

                    Comment

                    • Stephen Samuel

                      #25
                      Re: delete comments in .c file

                      Irrwahn Grausewitz wrote:[color=blue]
                      > Stephen Samuel <stephen_samuel @telus.net> wrote:
                      >
                      >[color=green]
                      >>Here's a perl script which will handle *MOST* sane C code...[/color]
                      >
                      > <snip>
                      >
                      > Since when is perl topical in c.l.c?[/color]
                      It's a C solution .. But Perl is written in C, so if you like,
                      I can just
                      #include <perl-source.c>
                      [color=blue]
                      > BTW:
                      > Does your "solution" account for comment delimiters inside string
                      > literals? (I'm unfortunately unable to decrypt the line-noise
                      > provided.)[/color]

                      Yes. It accounts for comment delimiters in quotes and quote
                      delimiters in comments (One side effect is that double quote
                      strings have a space added after them. Given the way that I
                      wrote it, it was a choice between that, replacing comments with
                      Nothing (possible to cause syntax errors) or added complexity.)

                      It also handles quoted double-quotes inside of strings.

                      It does NOT handle double-quote or comment-start delimiters inside
                      of single-quotes (char literals), but that would be easy enough to add.


                      --
                      Stephen Samuel +1(604)876-0426 samuel@bcgreen. com

                      Powerful committed communication. Transformation touching
                      the jewel within each person and bringing it to light.

                      Comment

                      • Joona I Palaste

                        #26
                        Re: delete comments in .c file

                        Stephen Samuel <stephen_samuel @telus.net> scribbled the following:[color=blue]
                        > Irrwahn Grausewitz wrote:[color=green]
                        >> Stephen Samuel <stephen_samuel @telus.net> wrote:[color=darkred]
                        >>>Here's a perl script which will handle *MOST* sane C code...[/color]
                        >>
                        >> <snip>
                        >>
                        >> Since when is perl topical in c.l.c?[/color]
                        > It's a C solution .. But Perl is written in C, so if you like,
                        > I can just
                        > #include <perl-source.c>[/color]

                        Are Perl implementations *required* to be written in C? And are
                        Perl implementations *required* to ship with the source code?

                        --
                        /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                        \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                        "'So called' means: 'There is a long explanation for this, but I have no
                        time to explain it here.'"
                        - JIPsoft

                        Comment

                        • Stephen Samuel

                          #27
                          Re: delete comments in .c file

                          CBFalconer wrote:[color=blue]
                          > *** rude top-posting fixed ***
                          >[/color]
                          Hmm.. This must be a relatively recent addition to usenet
                          ettiquete (i.e. in the last decade or so).

                          Appologies. I'm an old foggie, and it's probably been an decade
                          since I've posted here.

                          --
                          Stephen Samuel +1(604)876-0426 samuel@bcgreen. com

                          Powerful committed communication. Transformation touching
                          the jewel within each person and bringing it to light.

                          Comment

                          • Irrwahn Grausewitz

                            #28
                            Re: delete comments in .c file

                            "Stephen Samuel" wrote:
                            [color=blue]
                            > Irrwahn Grausewitz wrote:[color=green]
                            > > Since when is perl topical in c.l.c?[/color]
                            > It's a C solution[/color]

                            Err, no.
                            [color=blue]
                            >.. But Perl is written in C, so if you like,
                            > I can just
                            > #include <perl-source.c>[/color]

                            Non-standard header file. ;-)
                            [color=blue][color=green]
                            > > Does your "solution" account for comment delimiters inside string
                            > > literals?[/color]
                            >
                            > Yes.[/color]

                            Nice.
                            [color=blue]
                            > It accounts for comment delimiters in quotes and quote
                            > delimiters in comments (One side effect is that double quote
                            > strings have a space added after them. Given the way that I
                            > wrote it, it was a choice between that, replacing comments with
                            > Nothing (possible to cause syntax errors) or added complexity.)[/color]

                            Hm. AFAICT that shouldn't cause much trouble, OK.
                            [color=blue]
                            > It also handles quoted double-quotes inside of strings.[/color]

                            ITYM something like "\""?
                            [color=blue]
                            > It does NOT handle double-quote or comment-start delimiters inside
                            > of single-quotes (char literals), but that would be easy enough to
                            > add.[/color]

                            Fair enough.
                            But still there might be "strange" cases caused where your script may
                            fail. Consider:

                            /* gotcha! *\
                            /

                            A C preprocessor would have deleted the <backslash><n ew-line> sequence
                            in translation phase 2 *before* the tokenization and comment replacement
                            takes place in phase 3. And if the backslash is written as a trigraph
                            sequence we need to "fake" translation phase 1 as well... :-(

                            Admittedly, these are rare situations, but you see: sophisticated
                            comment replacement in C files isn't /that/ easy after all, you have to
                            provide quite an amount of preprocessor functionality to get it right.

                            Best Regards
                            --
                            Irrwahn

                            PS: Please don't email me if you already posted
                            your reply to the newsgroup; thank you.

                            Comment

                            • Keith Thompson

                              #29
                              Re: delete comments in .c file

                              Joona I Palaste <palaste@cc.hel sinki.fi> writes:
                              [...][color=blue]
                              > Are Perl implementations *required* to be written in C? And are
                              > Perl implementations *required* to ship with the source code?[/color]

                              <OT>
                              Perl is pretty much defined by its implementation, not by a language
                              standard. The implementation (there's basically only one) is written
                              in C. It's distributed under one of two open source licenses, both of
                              which require the source to be available (but not necessarily shipped
                              with the binaries).

                              This is probably incorrect in some minor details. If I had posted to
                              a more appropriate newsgroup, someone would jump in and correct me.
                              </OT>

                              --
                              Keith Thompson (The_Other_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
                              Schroedinger does Shakespeare: "To be *and* not to be"

                              Comment

                              • Joona I Palaste

                                #30
                                Re: delete comments in .c file

                                Keith Thompson <kst@cts.com> scribbled the following:[color=blue]
                                > Joona I Palaste <palaste@cc.hel sinki.fi> writes:
                                > [...][color=green]
                                >> Are Perl implementations *required* to be written in C? And are
                                >> Perl implementations *required* to ship with the source code?[/color][/color]
                                [color=blue]
                                > <OT>
                                > Perl is pretty much defined by its implementation, not by a language
                                > standard. The implementation (there's basically only one) is written
                                > in C. It's distributed under one of two open source licenses, both of
                                > which require the source to be available (but not necessarily shipped
                                > with the binaries).[/color]
                                [color=blue]
                                > This is probably incorrect in some minor details. If I had posted to
                                > a more appropriate newsgroup, someone would jump in and correct me.
                                > </OT>[/color]

                                OK, I have to concede with that, but Samuel's answer still wasn't
                                sufficient. Writing #include <perl_source. h> at the top of the Perl
                                file will change the program into a mix-and-match of C and Perl,
                                which will not compile as either language.

                                --
                                /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                                \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                                "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
                                - Bob Wiley

                                Comment

                                Working...