Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why don't C comments nest?

Reply
Thread Tools

Why don't C comments nest?

 
 
Eric Sosman
Guest
Posts: n/a
 
      11-12-2011
On 11/12/2011 6:10 AM, BartC wrote:
>
>
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:j9khmm$2qa$(E-Mail Removed)...
>> On 11/11/2011 5:14 PM, BartC wrote:

>
>> strcat(p, "'"); /* can't use " here */

>
> Sorry I don't understand your point here.


See below.

>> If you want comments to nest and *not* to be tripped up by examples
>> like Kaz' `/* char *comment_end = "/*"; */', then you need to insist
>> that the comment's content be lex-able, and comments like the one in
>> my example would become impossible.

>
> But if someone wants to write a /* ... */ comment then they will be tripped
> up by anything containing a */:


... which Kaz' example does not. Repeat: Kaz' example does not
contain a */ inside the comment. Look more closely.

His example works fine in today's C, but would get into trouble
in a C where comments nested. Why? Because you're on the horns of
a dilemma:

- You can see the quoted /* as the start of a nested comment,
causing you to interpret the */ as the end of that nested
comment,

or

- You must lex the internals of the comment to recognize that
that "/*" is a string literal and not the start of an inner
comment.

If you take the first course, then the */ ends the internal comment
but leaves the external comment still alive, still eating all the
rest of the source file. If you take the second, then you must
disallow comments of the kind I illustrated above, because your
lexer will trip over the unmatched ' and/or " characters.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      11-12-2011
"Eric Sosman" <(E-Mail Removed)> wrote in message
news:j9lrai$mm$(E-Mail Removed)...
> On 11/12/2011 6:10 AM, BartC wrote:


>> But if someone wants to write a /* ... */ comment then they will be
>> tripped
>> up by anything containing a */:

>
> ... which Kaz' example does not. Repeat: Kaz' example does not
> contain a */ inside the comment. Look more closely.


Yes I know it doesn't. And I gave a counter-example which looked like this:

/* char *comment_end = "*/"; */

(which also happens to match the variable more accurately, but that's
another matter...)

> rest of the source file. If you take the second, then you must
> disallow comments of the kind I illustrated above, because your
> lexer will trip over the unmatched ' and/or " characters.


And as things are now, we must disallow comments like my example.

I can't see why one is any worse than the other.

--
Bartc

 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      11-12-2011
On 11-Nov-11 15:54, Keith Thompson wrote:
> C99 changed the syntax to permit a trailing comma in an enum
> declaration. It would have made sense to permit a trailling
> comma for *any* comma-separated list. (Probably not for the comma
> operator, though.)


Why not? If ";" is a valid statement, then why shouldn't ",;" be valid?

S


--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
Lauri Alanko
Guest
Posts: n/a
 
      11-12-2011
In article <j9lvd7$mn0$(E-Mail Removed)>,
Stephen Sprunk <(E-Mail Removed)> wrote:
> Why not? If ";" is a valid statement, then why shouldn't ",;" be valid?


Because the comma operator separates expressions, not statements.


Lauri
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-12-2011
On 11/12/2011 8:57 AM, BartC wrote:
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:j9lrai$mm$(E-Mail Removed)...
>> On 11/12/2011 6:10 AM, BartC wrote:

>
>>> But if someone wants to write a /* ... */ comment then they will be
>>> tripped
>>> up by anything containing a */:

>>
>> ... which Kaz' example does not. Repeat: Kaz' example does not
>> contain a */ inside the comment. Look more closely.

>
> Yes I know it doesn't. And I gave a counter-example which looked like this:
>
> /* char *comment_end = "*/"; */
>
> (which also happens to match the variable more accurately, but that's
> another matter...)
>
>> rest of the source file. If you take the second, then you must
>> disallow comments of the kind I illustrated above, because your
>> lexer will trip over the unmatched ' and/or " characters.

>
> And as things are now, we must disallow comments like my example.
>
> I can't see why one is any worse than the other.


Would you like to be able to use a contraction like

if (p < endp) /* don't run off the end! */

in a comment? Then you don't want comments that nest.

Comments are not for suppressing blocks of code; comments are
for commentary.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-12-2011
On 11/12/2011 9:21 AM, Stephen Sprunk wrote:
> On 11-Nov-11 15:54, Keith Thompson wrote:
>> C99 changed the syntax to permit a trailing comma in an enum
>> declaration. It would have made sense to permit a trailling
>> comma for *any* comma-separated list. (Probably not for the comma
>> operator, though.)

>
> Why not? If ";" is a valid statement, then why shouldn't ",;" be valid?


Do you think `*;' should be valid?

(If so, how many operands have been omitted?

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      11-12-2011
On 2011-11-12, Lauri Alanko <(E-Mail Removed)> wrote:
> In article <j9lvd7$mn0$(E-Mail Removed)>,
> Stephen Sprunk <(E-Mail Removed)> wrote:
>> Why not? If ";" is a valid statement, then why shouldn't ",;" be valid?

>
> Because the comma operator separates expressions, not statements.


That's true, but it does so by being a binary operator which
requires two operands.

The ; is not an operator, but a punctuator which terminates.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      11-12-2011
"Eric Sosman" <(E-Mail Removed)> wrote in message
news:j9m058$sil$(E-Mail Removed)...
> On 11/12/2011 8:57 AM, BartC wrote:


>> And as things are now, we must disallow comments like my example.
>>
>> I can't see why one is any worse than the other.

>
> Would you like to be able to use a contraction like
>
> if (p < endp) /* don't run off the end! */


(I still don't see what the problem would be with this example; what's
supposed to happen when that line in enclosed in another /*...*/ comment?)

> in a comment? Then you don't want comments that nest.
>
> Comments are not for suppressing blocks of code; comments are
> for commentary.


Nevertheless that's what the OP wants to do. Does the Standard specifically
tell us what content is allowed in a comment, and what is frowned upon?

--
Bartc



 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-12-2011
On 11/12/11 11:54 PM, BartC wrote:
> "Kaz Kylheku"<(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2011-11-11, BartC<(E-Mail Removed)> wrote:

>
>>> Perhaps have #comment ... #end then

>>
>> That's a useless syntactic sugar for #if 0; furthermore, it's badly
>> named since this feature is not for commenting. Your "comment" has
>> to be written in valid C preprocessor tokens.

>
> That was before I realised that #if 0 didn't work as I expected, ie. ignore
> everything except what was necessary to find a matching #endif.


Removing the code works as expected.

--
Ian Collins
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      11-12-2011


"Robert Wessel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 12 Nov 2011 18:17:36 -0000, "BartC" <(E-Mail Removed)> wrote:
>
>>"Eric Sosman" <(E-Mail Removed)> wrote in message


>>> if (p < endp) /* don't run off the end! */

>>
>>(I still don't see what the problem would be with this example; what's
>>supposed to happen when that line in enclosed in another /*...*/ comment?)

>
> The problem in this case is that everything starting with the
> apostrophe is the (start) of a character constant, thus if you've
> tokenized that by the normal C rules, the */ will be lost as part of
> the contents of the character constant.


OK. So the assumption is that a nested comment would tokenise it's contents.

However I don't think that's necessary. And wouldn't work anyway.

>>> Comments are not for suppressing blocks of code; comments are
>>> for commentary.

>>
>>Nevertheless that's what the OP wants to do. Does the Standard
>>specifically
>>tell us what content is allowed in a comment, and what is frowned upon?

>
>
> Yes, the second sentence of the following covers it:
>
> "6.4.9 Comments
> Except within a character constant, a string literal, or a comment,
> the characters /* introduce a comment. The contents of such a comment
> are examined only to identify multibyte characters and to find the
> characters */ that terminate it."


In other words, anything is allowed, including source code, of C or anything
else.

So the suggestion that comments shouldn't contain source code is a style
issue that shouldn't be the concern of the Standard or imposed by a
compiler.

And reading in-between the lines, everything is allowed except */, even if
the latter was inside a literal, or inside a //-comment.

The only thing needed to make nested comments possible, is to disallow /* as
well as */, unless they are there to start or terminate a comment.

That means you won't be able to have /* as part of comment text, whether by
itself or part of a literal; is that really that big a deal?

--
Bartc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
A program to replace all JS comments with JSP comments in jsp files tungchau81@yahoo.com Javascript 4 06-03-2006 02:00 PM
A program to replace all JS comments with JSP comments in jsp files tungchau81@yahoo.com Java 0 06-02-2006 06:35 AM
Comments format: comments extending over multi-line Monk C Programming 10 04-20-2005 05:09 PM



Advertisments