Why nested comments not allowed?

Karl Heuer karl at haddock.ima.isc.com
Mon Feb 26 14:40:09 AEST 1990


In article <4601 at jarthur.Claremont.EDU> dfoster at jarthur.Claremont.EDU (Derek R. Foster) writes:
>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.  [So, when it is desirable to
>put them in a string constant, they should be encoded] in some way that
>breaks up the /* and */ pairs ... my favorite so far is this:
>  #define CS "/""* "
>  #define CE " *""/"
>  printf("before comment"CS"comment"CE"after comment");

If we're talking about C, then of course this is not necessary since the
scanner already knows about strings.  Therefore, I assume we're talking about
a hypothetical language which is a lot like C, but which has nestable comments
and therefore must worry about the interaction between comments and strings.

Surely if such a language existed, it would also have the escape sequences
`\*' (literal star) and `\/' (literal slash), so you could simply write
  printf("before comment/\* comment *\/after comment");
(note that `\?' was added to ANSI C for essentially this reason).

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list