changing C++ comments // to C comments /* 
Author Message
 changing C++ comments // to C comments /*

Subject: Re: changing C++ comments // to C comments /*
Newsgroups: comp.lang.c

Distribution:


: : however is pure C so I want to revert back to regular C style comments.

This thread doesn't belong here, but the reply below is wrong and
therefore should be corrected as the original poster's needs are
worthy of empathy ;-)

: : Is there a unix utility that will make the change automatically, while retaining
: : the general appearance  of the program.

: There are a number of pattern recognition utility available in unix. For your
: problem I would try sed or tr.

: The sed command should look something like this I think.

: sed 's/\/\//\/*/g' oldsource.c > newsource.c

The problem with this is that it merely opens a Comment; it fails to
close one. if you do it this way <with sed> try something like:

sed 's/\/\/\(.*\)/\/\* \1 \*\//g' old.c > new.c

at least that way, you'll capture from // to the end of line and
translate the C++ comment into a /* C */ comment.

might even append this to the command above: && mv new.c old.c

testing the above, here's the input:
------- input file ------
// this line was a C++ comment.
this is text // that started a C++ comment
this is more text
this is nothing.// this a comment.
// comment
text   // oboy comment
----- output file -----
/* this line was a C++ comment. */
this is text /* that started a C++ comment */
this is more text
this is nothing./* this a comment. */
/* comment */
text   /* oboy comment */
--------------------------
Given the above <assuming "//" is always intended as a Comment,
you could easily write a C program to:
read your C programs as input ->> fopen(...,"r"); fgets each line
write each line out to a new file <e.g.: new_%s where %s is the
name of the file you're currently reading> fopen(...,"w");
traverse each line character by character looking for //
when you find that pair, replace the 2nd '/' with '*'
continue thru the line, looking for '\0' or '\n'
insert two characters before '\0' and '\n': '*' and '/'
make sure to put '\0' and '\n' on the line after the 2 characters.
write this line out to the new_%s file with fputs();

: Hope this gets you started at least.
You should have tested your idea; it would have made for lots of work ;-\
: Ute

cheers, gary /* the Sorcerer's Apprentice */
--
"Why do we have to hide from the police, Daddy?" | Truth:
"Because we use vi, honey. They use emacs."      | This .sig is pirated



Wed, 06 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

[snip]
: Given the above <assuming "//" is always intended as a Comment,
: you could easily write a C program to:
: read your C programs as input ->> fopen(...,"r"); fgets each line
: write each line out to a new file <e.g.: new_%s where %s is the
: name of the file you're currently reading> fopen(...,"w");
: traverse each line character by character looking for //
: when you find that pair, replace the 2nd '/' with '*'
: continue thru the line, looking for '\0' or '\n'
: insert two characters before '\0' and '\n': '*' and '/'
: make sure to put '\0' and '\n' on the line after the 2 characters.
: write this line out to the new_%s file with fputs();

The one case where that will fail is where both types of comments occur
on the same line:

// /*This used to be executed, but I've commented out the while block */
// printf( "Testing\n" );

You then get the closing "*/" strings.

Unless you can be sure that this will not happen in your code, you need
to blot out any closing "*/" on lines where a "//" was found.

You may want to test that the // doesn't occur in a string, although that
makes the program much more complex.

--
#####################################################################

Emmenjay Consulting
    Making computers work for you, not the other way around.

PO Box 909                                             Ph 018 240 704
Kensington 2033
AUSTRALIA    
#####################################################################



Fri, 08 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*


-> : however is pure C so I want to revert back to regular C style comments.
->
-> : Is there a unix utility that will make the change automatically, while retaining
-> : the general appearance  of the program.
->
->
-> There are a number of pattern recognition utility available in unix. For your
-> problem I would try sed or tr.
->
-> The sed command should look something like this I think.
->
-> sed 's/\/\//\/*/g' oldsource.c > newsource.c
->
-> The backslashes are needed to protect the forward slashes.
->
-> Hope this gets you started at least.
->

how about:

sed 's|//.*$|/* & */|' oldsource.c > newsource.c

Note that this will regrettably break lines that have '//' inside quotes.
--
                      Brandon Wallace
            Nicholas | Applegate Capital Management

          "I live life face down in the fast lane."



Fri, 08 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

Quote:
Loreti) writes:

> OK, I will post _real_code_ on comp.lang.c ...  I am waiting for your
> flames :-)

> Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html

> /*-----------------------------------------------------------*
>  | File: comcon.c - Last rev. MLO 1995-01-17                 |
>  | comcon <in> <out> copies from <in> to <out>, substituting |
>  | C-style comments for C++-style comments (//...) .         |
>  *------------------------------------------------------+----*

Excellent code, but...

// ...your program \
   does not handle \
   multiline       \
   C++ comments!!

Jari Laaksonen            | Opinions expressed here are mine
ICL Personal Systems Oy   | and mine alone.
(TeamWARE Division)       |
FINLAND                   | My employers opinions can be found at:

=======-------------=======================================(*)====



Tue, 12 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

Quote:


>Loreti) writes:

>> OK, I will post _real_code_ on comp.lang.c ...  I am waiting for your
>> flames :-)

>> Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html

>> /*-----------------------------------------------------------*
>>  | File: comcon.c - Last rev. MLO 1995-01-17                 |
>>  | comcon <in> <out> copies from <in> to <out>, substituting |
>>  | C-style comments for C++-style comments (//...) .         |
>>  *------------------------------------------------------+----*

>Excellent code, but...

>// ...your program \
>   does not handle \
>   multiline       \
>   C++ comments!!

Sorry, these beasts do not exist.  From Stroustrup, The C++
programming language 2nd ed, page 478 (my ARM is at work):

"The characters // start a comment, which terminates at the end of the
line on which they occur."

Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html



Thu, 14 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

[Someone wrote]

Quote:
>>Excellent code, but...
>>// ...your program \
>>   does not handle \
>>   multiline       \
>>   C++ comments!!
>Sorry, these beasts do not exist.  From Stroustrup, The C++
>programming language 2nd ed, page 478 (my ARM is at work):
>"The characters // start a comment, which terminates at the end of the
>line on which they occur."

This really ought to migrate to comp.lang.c++ (crossposted now)...

Unless C++ is more different from C than it used to be, the conversion
of "\\\n" into nothing happens *BEFORE* comments are eaten.  In C,
the eating of backslash-newline is TP 2, and comments are 3, I believe.

Since a lot of C++ compilers are using C preprocessors with one or two extra
features, I'd expect this to still be the case.  In essence, the line
on which they occur looks like
// ...your program   does not handle   multiline        C++ comments!!

-s
--

C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!

The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq



Thu, 14 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*


Quote:
>>// ...your program \
>>   does not handle \
>>   multiline       \
>>   C++ comments!!

>Sorry, these beasts do not exist.  From Stroustrup, The C++
>programming language 2nd ed, page 478 (my ARM is at work):

>"The characters // start a comment, which terminates at the end of the
>line on which they occur."

That is insufficient. Read 'Phases of Preprocessing' on page 607 -
backslash-newline pairs are removed before comments are dealt with, so
the 4 lines above comprise a single line for comment processing. This is
the same general rule that C uses (with the specific difference that //
does not introduce a comment in C).

--
-----------------------------------------


-----------------------------------------



Sat, 16 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*


: [Someone wrote]
: >>Excellent code, but...
: >>// ...your program \
: >>   does not handle \
: >>   multiline       \
: >>   C++ comments!!
: >Sorry, these beasts do not exist.  From Stroustrup, The C++
: >programming language 2nd ed, page 478 (my ARM is at work):
: >"The characters // start a comment, which terminates at the end of the
: >line on which they occur."
: This really ought to migrate to comp.lang.c++ (crossposted now)...
: Unless C++ is more different from C than it used to be, the conversion
: of "\\\n" into nothing happens *BEFORE* comments are eaten.  In C,
: the eating of backslash-newline is TP 2, and comments are 3, I believe.

I agree.
--
_____________________________________________________________________________


  Board Systems                          Desk/voicemail: +44(0) 1202 850951
  Wimborne, Dorset,                      Site reception: +44(0) 1202 850850
  England, BH21 7PP                      Fax:            +44(0) 1202 850988
_____________________________________________________________________________



Sat, 16 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

Quote:
(Peter Seebach) writes:



|> [Someone wrote]
|> >>Excellent code, but...

|> >>// ...your program \
|> >>   does not handle \
|> >>   multiline       \
|> >>   C++ comments!!

|> >Sorry, these beasts do not exist.  From Stroustrup, The C++
|> >programming language 2nd ed, page 478 (my ARM is at work):

|> >"The characters // start a comment, which terminates at the end of the
|> >line on which they occur."

|> This really ought to migrate to comp.lang.c++ (crossposted now)...

|> Unless C++ is more different from C than it used to be, the conversion
|> of "\\\n" into nothing happens *BEFORE* comments are eaten.  In C,
|> the eating of backslash-newline is TP 2, and comments are 3, I believe.

As well as I can interpret the public draft, this is still the case.
The phases of compilation are basically the same as in the C standard.
There is no special rule elsewhere that I can find to invalidate this.
So a priori, there is no change with regards to C.

On the other hand, I wouldn't count on it working with real compilers.
There was some discussion over this in the standards committee, and
the gist of it was: some implementers were reading input line by line,
and wanted to just read the next line whenever they saw a //,
regardless of what followed the //.  (As you no doubt know, compilers
don't really implement all of the phases separately internally.  They
just make sure that it looks as if they did.)

|> Since a lot of C++ compilers are using C preprocessors with one or two extra
|> features, I'd expect this to still be the case.  In essence, the line
|> on which they occur looks like
|> // ...your program   does not handle   multiline        C++ comments!!

I've even seen some that used a C preprocessor without any extra
features;  the // comments stayed in, and were removed by the
compiler.  This resulted in a macro being able to generate a comment!
In fact, this "feature" was often used unintentionally, much to the
surprise of the programmer:

        #define RETURN( x )  return trace( x ) , x // for debugging

        int
        f()
        {
            //  ...
            RETURN( 3 ) ;
        }

And what the compiler saw (after preprocessing) in the next to the
last line was:

            return trace( 3 ) , 3 // for debugging ;

Note where the ; has appeared.  Or rather, disappeared.  And imagine
how the beginning C++ programmer feels when the compiler gives the
error message "missing semicolon" for this line.

As a result of such problems, I still only use C style comments in
macros.
--

GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, tudes et ralisations en logiciel orient objet --
                -- A la recherche d'une activit dans une region francophone



Sat, 16 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

Quote:



>>>// ...your program \
>>>   does not handle \
>>>   multiline       \
>>>   C++ comments!!

>>Sorry, these beasts do not exist.  From Stroustrup, The C++
>>programming language 2nd ed, page 478 (my ARM is at work):

>>"The characters // start a comment, which terminates at the end of the
>>line on which they occur."

>That is insufficient. Read 'Phases of Preprocessing' on page 607 -
>backslash-newline pairs are removed before comments are dealt with, so
>the 4 lines above comprise a single line for comment processing. This is
>the same general rule that C uses (with the specific difference that //
>does not introduce a comment in C).

OK, I tought that that was not legal C++.  So, here comes the updated
version of comcon.c --- followed by a sample file.  Enjoy...
---------------------------------Begin file: comcon.c
/*-----------------------------------------------------------*
 | File: comcon.c - Last rev. MLO 1995-11-30                 |
 | comcon <in> <out> copies from <in> to <out>, substituting |
 | C-style comments for C++-style comments (//...) .         |
 *------------------------------------------------------+----*
 | Author: Maurizio Loreti, aka MLO or (HAM) I3NOO      |
 | Work:   University of Padova - Department of Physics |
 |         Via F. Marzolo, 8 - 35131 PADOVA - Italy     |
 | Phone:  ++39 (49) 827-7216   FAX: ++39 (49) 827-7102 |

 | WWW:    http://mvxpd5.pd.infn.it/wwwcdf/mlo.html     |
 *------------------------------------------------------*/

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

int process(FILE*, FILE*);

int main(
  int argc,
  char *argv[]
){
  FILE *fpin, *fpout;
  int status = EXIT_FAILURE;
  const char filerr[] = "%s: couldn't open file \"%s\" for %sput";

  if (argc != 3) {
    fprintf(stderr, "Usage:\t%s <input file> <output file>\n", argv[0]);
    fputs("\tCopy, with conversion of C++ comments to C comments.\n", stderr);
  } else {
    if ((fpin = fopen(argv[1], "r")) == 0) {
      fprintf(stderr, filerr, argv[0], argv[1], "in");
    } else {
      if ((fpout = fopen(argv[2], "w")) == 0) {
        fprintf(stderr, filerr, argv[0], argv[2], "out");
        fclose(fpin);
      } else {
        status = process(fpin, fpout);
        fclose(fpin);
        fclose(fpout);
      }
    }
  }
  return status;

Quote:
}

int process(
  FILE* fpin,                   /* Input file */
  FILE* fpout                   /* Output file */
){
  int c;                        /* Next character */
  int lastc;                    /* Previous character */
  unsigned int state = 0;
  unsigned long ln = 0;         /* Line number */

/**
 | process() is implemented as a finite state machine.
 | The fgetc() loop is exited at EOF; the standard guarantees that every
 | input line has a trailing '\n', so I don't check e.g. for state == 4
 | at the end (a C++ comment in the last line, without a newline and ended
 | by EOF.  I don't care for errors like e.g. an EOF inside a string; these
 | situations are flagged by the compiler both in the input and in the
 | output file.
**/

  while ((c = fgetc(fpin)) != EOF) {
    switch (state) {
/**
 | state 0: normal input
**/
      case 0:
        switch (c) {
          case '\"':            /* String */
            state = 1;
            break;
          case '\'':            /* Character constant */
            state = 2;
            break;
          case '/':             /* To be investigated */
            state = 3;
            break;
        }
        break;
/**
 | state 1: inside a string (found a ")
**/
      case 1:
        switch (c) {
          case '\"':            /* End of the string */
            state = 0;
            break;
          case '\\':            /* Escaped character */
            state = 7;
            break;
        }
        break;
/**
 | state 2: inside a character constant (found a ')
**/
      case 2:
        switch (c) {
          case '\'':            /* End of the character constant */
            state = 0;
            break;
          case '\\':            /* Escaped character */
            state = 8;
            break;
        }
        break;
/**
 | state 3: we have found a /, maybe this is a comment...
**/
      case 3:
        switch (c) {
          case '/':             /* C++ comment; change to C-style */
            state = 4;
            c = '*';
            break;
          case '*':             /* C-style comment */
            state = 5;
            break;
          default:              /* Just a slash... */
            state = 0;
            break;
        }
        break;
/**
 | state 4: we are inside a C++ comment (found a sequence //).
 | When a newline is found, check for comment continuation on the
 | next line.
**/
      case 4:
        switch (c) {
          case '\n':            /* End of comment; change to C-style */
            if (lastc != '\\') {
              fputs(" */", fpout);
              if (ferror(fpout)) {
                perror("Write error");
                return EXIT_FAILURE;
              }
              state = 0;
            }
            break;
          case '*':
            state = 9;
            break;
        }
        break;
/**
 | state 5: we are inside a C comment
**/
      case 5:
        if (c == '*') state = 6;
        break;
/**
 | state 6: we are inside a C comment, and there is an additional *; maybe
 |          this is the end of the comment...
**/
      case 6:
        if (c == '/') state = 0;
           else       state = 5;
        break;
/**
 | state 7: an escaped character inside a string, like "...\"..."
**/
      case 7:
        state = 1;
        break;
/**
 | state 8: an escaped character inside a character constant, like '\"'
**/
      case 8:
        state = 2;
        break;
/**
 | state 9: after a star in a C++ comment - maybe a trouble...
**/
      case 9:
        if (c == '/') {
          fprintf(stdout, "Please, check line %lu for troubles...\n", ln+1);
          state = 4;
        }
        break;
    }

/**
 | Output 'c' to file, and save it in 'lastc'
**/
    fputc((lastc = c), fpout);
    if (ferror(fpout)) {
      perror("Write error");
      return EXIT_FAILURE;
    }
    if (c == '\n') ln++;
  }

  if (ferror(fpin)) {
    perror("Read error");
    return EXIT_FAILURE;
  }

  fprintf(stdout, "%lu lines written\n", ln);
  return EXIT_SUCCESS;

Quote:
}

---------------------------------Begin file: comcon.txt
This is a sample file for comcon.c :

/* This is a C style comment */
// This is a C++ style comment
" This is a string with embedded /* and */ "
" This is a string with embedded // "
" This is a string with an escaped quote \" , /* and */ "
" This is a string with an escaped quote \" and // "
// This is a C++ \
   comment on \
   several lines.
'\"' and an escaped character too
/* * // ... and more complicated things */
// The following line must be signalled:
// /* Trouble in line no. 15 */
---------------------------------End included files.

Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html



Mon, 18 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

Quote:
Loreti) writes:

<snip>
|> OK, I tought that that was not legal C++.  So, here comes the updated
|> version of comcon.c --- followed by a sample file.  Enjoy...

It does not treat the following correctly

// The following line contains a comment at the end
x = x++; /\
/ The most common bogus question.

If C++ follows C's phases, \ newlines are removed first!. Also, I do not know
if C++ has trigraphs: they are also recognized before anything else. So, any
\ can be replaced by ??/

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Mon, 18 May 1998 03:00:00 GMT  
 changing C++ comments // to C comments /*

Quote:
Loreti) writes:
> ---------------------------------Begin file: comcon.txt
> This is a sample file for comcon.c :

> /* This is a C style comment */
> // This is a C++ style comment
> " This is a string with embedded /* and */ "
> " This is a string with embedded // "
> " This is a string with an escaped quote \" , /* and */ "
> " This is a string with an escaped quote \" and // "
> // This is a C++ \
>    comment on \
>    several lines.
> '\"' and an escaped character too
> /* * // ... and more complicated things */
> // The following line must be signalled:
> // /* Trouble in line no. 15 */
> ---------------------------------End included files.

Here are some even more complicated things for your test file:

// first without comment
char s1[] = "string      \
             continues";

// same commented
// char s2[] = "string      \
             continues";

This line...
// /* comment */

..would be after converting like:
/* /* comment * / */

Some C++ preprocessors interpret the following case differently:

// C++ multiline   \ <white space> <nl>
   comment.

Jari Laaksonen            | Any opinions expressed here are mine
ICL Personal Systems Oy   | and mine alone unless stated otherwise.
(TeamWARE Division)       |
FINLAND                   | My employers opinions can be found at:

=======-------------=======================================(*)====



Tue, 19 May 1998 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Change C++ comments to C-comments

2. changing C++ comments // to C comments /*

3. Converting C++ comments to C comments

4. Need comments on commenting program

5. Multi-Line Comments Nested in Single Line Comments

6. comments on comments

7. Newbie: separate big .cs file into small .cs files

8. MS pls comment: Undocumented MFC changes in VC6.0

9. Changing Classwizards default comments

10. C# type xml comments in C++?

11. Looking for a C/C++ comment formatting tool

12. C++ to C comment replacement

 

 
Powered by phpBB® Forum Software