[comp.lang.c] Re^4: Why nested comments not allowed?

chris@wacsvax.OZ (chris mcdonald) (02/19/90)

dalenber@cbnewsc.ATT.COM (Russel Dalenberg) writes:

>In article <1523@wacsvax.OZ>, chris@wacsvax.OZ (chris mcdonald) writes:
.....
>	printf("A comment is ended with */");


Ho, hum, now your compiler, lexical analyser can't detect strings either?

--------------------------------------------------------------------------------
Chris McDonald.

Department of Computer Science,   ACSnet:   chris@wacsvax.oz
University of Western Australia,  ARPA:     chris%wacsvax.oz@uunet.uu.net
Mounts Bay Road,                  UUCP:     ..!uunet!murtoa!wacsvax!chris
Crawley, Western Australia, 6009. SDI:      (31.97 +/-10% S, 115.81 +/-10% E)
PHONE:  ((+61) 09) 380 2533       FAX:      ((+61) 09) 382 1688

dalenber@cbnewsc.ATT.COM (Russel Dalenberg) (02/21/90)

In article <1525@wacsvax.OZ>, chris@wacsvax.OZ (chris mcdonald):
> dalenber@cbnewsc.ATT.COM (Russel Dalenberg) writes:
>>	printf("A comment is ended with */");
> 
> Ho, hum, now your compiler, lexical analyser can't detect strings either?

I said: how do you handle the case where the above line of legal C is
commented out.  If it's not commented out, there is no problem.

The "usual" way to comment out such code would be:

/*	printf("A comment is ended with */"); */

Since quotes have *NO* significance within comments, the above comment
ends with the first "*/" found; with *both* nested and non-nested comments!


Russel Dalenberg

att!ihlpb!dalenber
dalenber@ihlpb.att.com

Disclaimers: These opinions are mine, all mine!

dougp@voodoo.ucsb.edu (02/21/90)

-Message-Text-Follows-
In article <1525@wacsvax.OZ>, chris@wacsvax.OZ (chris mcdonald) writes...
>dalenber@cbnewsc.ATT.COM (Russel Dalenberg) writes:
>>In article <1523@wacsvax.OZ>, chris@wacsvax.OZ (chris mcdonald) writes:
>>	printf("A comment is ended with */");
>Ho, hum, now your compiler, lexical analyser can't detect strings either?
Comment this:

printf("\"*/goto hell;/*\"");/* "\" is the escape charactor*/

Build me a parser that handles this situation properly and I might
begin to believe that nesting comments are possible. I personaly 
prefer comment to end of line (like Microsofts //) for commenting
out large blocks of code, it makes it easy to tell (on one of these
damn 25 line displaies) just what has been commented out.

gwollman@jhunix.HCF.JHU.EDU (Garrett A Wollman) (02/21/90)

In article <13771@cbnewsc.ATT.COM> dalenber@cbnewsc.ATT.COM (Russel Dalenberg) writes:
>I said: how do you handle the case where the above line of legal C is
>commented out.  If it's not commented out, there is no problem.
>
>The "usual" way to comment out such code would be:
>
>/*	printf("A comment is ended with */"); */
>
>Since quotes have *NO* significance within comments, the above comment
>ends with the first "*/" found; with *both* nested and non-nested comments!

I was one of those people in Dan Barrett's class.  My program has
a simple hook which can be enabled (call it "STRINGINCOMM") which
can handle such situations...AT A PRICE.  In *EITHER* style, you can
EITHER handle this situation correctly, or you can handle

/* ho hum, this is a quote chatacter -->" */

correctly.  Without knowing the programmer's intentions wrt comments,
it is *impossible* to handle both cases correctly in the same decommenter.

-GAWollman


-- 
"All societies are based on rules to protect pregnant women and children.
 . . . As racial survival is the only universal morality, no other bases
 is possible."           - Lazarus Long [RAH, _TEFL_]
---------------Hopkins doesn't *want* my opinions------------------------

dfoster@jarthur.Claremont.EDU (Derek R. Foster) (02/22/90)

In article <4015@hub.UUCP> dougp@voodoo.ucsb.edu writes:
>-Message-Text-Follows-
>In article <1525@wacsvax.OZ>, chris@wacsvax.OZ (chris mcdonald) writes...
>>dalenber@cbnewsc.ATT.COM (Russel Dalenberg) writes:
>>>In article <1523@wacsvax.OZ>, chris@wacsvax.OZ (chris mcdonald) writes:
>>>	printf("A comment is ended with */");
>>Ho, hum, now your compiler, lexical analyser can't detect strings either?
>Comment this:
>
>printf("\"*/goto hell;/*\"");/* "\" is the escape charactor*/
>
>Build me a parser that handles this situation properly and I might
>begin to believe that nesting comments are possible. I personaly 
>prefer comment to end of line (like Microsofts //) for commenting
>out large blocks of code, it makes it easy to tell (on one of these
>damn 25 line displaies) just what has been commented out.

I can see why this example would be a problem - it's got a lot of potential
for errors if comments are placed anywhere near it. What I don't see is
any reason why it would be any less error-prone whether nested comments
are allowed or not. Just trying to comment this code out will get you
into trouble, no matter WHICH style of comments you use. (Your parser can't
assume that what's inside a comment is really C program code, so it can't
properly deal with an open quotation mark that occurs after a /* , and it
shouldn't assume that it's the start of a string. Therefore, */ will 
end a comment whether it's inside quotation marks or not)

Yes, this example is bad programming (Intentionally so, I assume), but
it's bad whether your compiler allows nested comments or not. (Turbo C allows
either). Having /* or */ inside a string is going to be a potential problem
no matter what. Why not just go

printf("\"*""/goto hell;/""*\"");

Then you are guaranteed of not having a problem. (Personally, I like nested
comments. They've saved me a lot of time and trouble. Sure, there are
examples that can be constructed that will not parse properly using them,
but I have yet to see one that didn't have severe faults just waiting to
happen under "normal" comments too.) If you write code that is fragile enough
to be broken by just moving comments around, the problem is with YOU, not
the parser. Putting /* and */ inside a string is an ugly (and potentially
dangerous) KLUDGE. Yes, it works sometimes, but (like all kludges) for the
wrong reasons, and for reasons that may no longer be valid if you (or
someone else) moves comments around in your program without knowing the
implied rules you set up when you used the kludge. 

The only real reasons there are to do this (put /* or */ in strings) are: 
  (a) making your source code strings visually contiguous (or at least AS
  contiguous as they can be in C, considering all the strange control codes)
  (b) saving a whole TWO characters in your source file.
Reasons NOT to do it:
  (a) You have to figure out whether /* and */ will be interpreted as
  a comment every time you use them. If the answer to this question is
  "Yes", you have to figure out some kludgey way to write your code so
  that the compiler won't interpret them as comments.
  (b) You can frequently no longer comment out certain sections of code
  (c) If you forget where you put /* */ , and later try to comment out
  sections of code, your compiler may (a) barf, (b) compile OK, but something
  you never intended (if, by some unlikely chance, you accidentally
  made your mistakes self-consistent). What happens if you accidentally
  leave out a quotation mark? or a (real) */ ?
  (d) Even if you personally never comment out code, there remains the
  chance that someone else will have to maintain your program. Unless you
  are willing to put warnings in your programs like "don't comment out
  function foobar - it contains comment characters", you're giving them
  a loaded gun.
  (e) There is a perfectly easy way to avoid problems (a) through (d) -
  just have the compiler concatenate strings (i. e. "a/*a" becomes "a*""/a".
  (f) just in case method (e) doesn't work for some specific instance, there are
  plenty of other ways in C to do this as well.

I just don't see that the benefits outweigh the disadvantages. Of course,
examples like this could no longer be used to argue for/against nested
comments...

End-of-line comments are nice too. I wouldn't trade them for normal ones,
however. (I'd prefer to have both)

Derek Foster

dfoster@jarthur.Claremont.EDU (Derek R. Foster) (02/22/90)

In article <4550@jarthur.Claremont.EDU> dfoster@jarthur.Claremont.EDU (Derek R. Foster) writes:
>no matter what. Why not just go
>
>printf("\"*""/goto hell;/""*\"");
>
Another possible (comment-proof) solution that might be easier to read:

#define CS " /""* "
#define CE " *""/ "
printf("before comment"CS"comment"CE"after comment");

Derek Foster

barrett@jhunix.HCF.JHU.EDU (Dan Barrett) (02/23/90)

In article <13771@cbnewsc.ATT.COM> dalenber@cbnewsc.ATT.COM (Russel Dalenberg) writes:
>>>	printf("A comment is ended with */");
>
>I said: how do you handle the case where the above line of legal C is
>commented out.  If it's not commented out, there is no problem.
>
>The "usual" way to comment out such code would be:
>
>/*	printf("A comment is ended with */"); */
>
>Since quotes have *NO* significance within comments, the above comment
>ends with the first "*/" found; with *both* nested and non-nested comments!

	It seems there are 2 issues here.  The first is "How to parse?"
The second is "How to prevent us from tripping ourselves up?

	The statement:

	/*	printf("A comment is ended with */"); */

is easy to parse -- the comment truly closes at the */ in the string.
However, that */ is causing a problem probably unwanted by the programmer.

                                                        Dan

 //////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
| Dan Barrett, Department of Computer Science      Johns Hopkins University |
| INTERNET:   barrett@cs.jhu.edu           |                                |
| COMPUSERVE: >internet:barrett@cs.jhu.edu | UUCP:   barrett@jhunix.UUCP    |
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////

machaffi@fred.cs.washington.edu (Scott MacHaffie) (02/23/90)

In article <4550@jarthur.Claremont.EDU> dfoster@jarthur.Claremont.EDU (Derek R. Foster) writes:
>The only real reasons there are to do this (put /* or */ in strings) are: 

You forgot "writing a C parser which has to recognize comments".

			Scott MacHaffie

dfoster@jarthur.Claremont.EDU (Derek R. Foster) (02/24/90)

In article <10837@june.cs.washington.edu> machaffi@fred.cs.washington.edu.cs.washington.edu (Scott MacHaffie) writes:
>In article <4550@jarthur.Claremont.EDU> dfoster@jarthur.Claremont.EDU (Derek R. Foster) writes:
>>The only real reasons there are to do this (put /* or */ in strings) are: 
>
>You forgot "writing a C parser which has to recognize comments".
>
>			Scott MacHaffie

No, I didn't. You missed the point of my statement. (Don't feel bad; several
other people did too. That's why I'm posting instead of e-mailing you.
Most of the criticisms that I have heard have come from people that
simply misunderstood what I was saying. Was I really that unclear?)

I wasn't saying that these characters should never be used as string data.
I said that they should not be placed LITERALLY in a string, since they may
be mistaken (by the parser) for comments. (Literally as in, "inside quotation
marks, a '*' followed IMMEDIATELY by a '/' or vice versa"). Since the parser
will only recognize these characters as a comment if it sees them one right
after the other, encoding

  printf("before comment /* comment */ after comment");

in some way that breaks up the /* and */ pairs (but still means the same
thing to the compiler, just not the parser) means that the parser won't
ever mistake them for comments. I have posted several suggestions on how
to do this, but in case you missed them, my favorite so far is this:

  #define CS "/""* "
  #define CE " *""/"
  .
  .
  .
  printf("before comment"CS"comment"CE"after comment");

Note that although these lines produce code that is functionally identical
to the first printf, at no point in the code do the characters '/*' or '*/'
appear as literal strings. Therefore, they won't be recognized by the parser
as comments, and so won't cause you problems later on. 

Derek Foster