Re^4: Why nested comments not allowed?

Derek R. Foster dfoster at jarthur.Claremont.EDU
Sat Feb 24 08:52:28 AEST 1990


In article <10837 at june.cs.washington.edu> machaffi at fred.cs.washington.edu.cs.washington.edu (Scott MacHaffie) writes:
>In article <4550 at jarthur.Claremont.EDU> dfoster at 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



More information about the Comp.lang.c mailing list