[comp.sys.mac.programmer] MPW 2.0 and 3.0/3.1

dinapoli@rodan.acs.syr.edu (Ron DiNapoli) (05/09/90)

A few weeks back I posted a message regarding a problem I was having with
the MPW 3.0 C pre-processor.  This problem reared its ugly head when I at-
tempted to port my MPW 2.0 C code to 3.0.  The majority of responses I re-
ceived advised me to upgrade to MPW C 3.1.  I did this, but the problem 
did not go away.  Well I finally discovered what was going wrong... I had the
following macro defined:

	#define OP(tkdef,tkname,d1,d2) {#tkdef,tkname,d1,d2}

and I wanted to expand the following:

	OP(TK_SSLASH,"//",ASSC_YFX,400)

Anybody see the problem?  In MPW C 3.0 and later, the '//' symbol stands
for a comment.  It also seems that the preprocessor is unable to detect
that this particular comment character is occurring within a string.  
Since it doesn't, the preprocessor sees the open end of a string, and never
finds the closing " (because the real one was 'commented' out) before its
internal buffer overflows.  Once discovered, the fix was easy.  I just 
changed this particular occurrance of OP to:

	OP(TK_SSLASH,"/\/",ASSC_YFX,400)


So, it this a bug?  In my opinion it isn't.  Take for example the following 
macro definition:

#define PRINTIT(prstr) printf("PRINTIT has been called with: %s\n",prstr);

and the following program:

main()
{
	PRINTIT("/* This is a comment */")
	PRINTIT(" This is normal text ")
	PRINTIT(" // Another type of comment")
}

The preprocessor (3.1 C) will output the following:

main()
{
	printf("PRINTIT has been called with: %s\n","");
	printf("PRINTIT has been called with: %s\n"," This is normal text ");

Obviously, it was unable to finish expanding the last macro because of the 
// comment character.  Attempting to further compile this example will 
NOT work!

Note how in the first occurrance of the PRINTIT macro, the text between the
/* */ comment characters does not appear.  This tells me that in the 3.0/3.1
C preprocessor, comments are "taken out" regardless of where they appear.
Thus my problems with the '//' in my string seems consistent with this
philosophy.  For this reason, I would say it is not a bug at all!

Ron D.

dierks@ndcvb.cc.nd.edu (Timothy Dierks) (05/10/90)

From article <3238@rodan.acs.syr.edu>, by dinapoli@rodan.acs.syr.edu (Ron DiNapoli):
> Anybody see the problem?  In MPW C 3.0 and later, the '//' symbol stands
> for a comment.  It also seems that the preprocessor is unable to detect
> that this particular comment character is occurring within a string.  
> 
> Note how in the first occurrance of the PRINTIT macro, the text between the
> /* */ comment characters does not appear.  This tells me that in the 3.0/3.1
> C preprocessor, comments are "taken out" regardless of where they appear.

  And he quoteth from the book of Kernighan and Ritchie, Appendix A,
  Section A2.2:
    The characters /* introduce a comment, which terminates with the
    characters */.  Comments do not nest, and they do not occur within
    string or character literals.
  Here endeth the lesson.

Tim Dierks
dierks@ndcvb.cc.nd.edu
Go Irish!
Only >11< days to those silly hats.

philip@Kermit.Stanford.EDU (Philip Machanick) (05/10/90)

In article <3238@rodan.acs.syr.edu>, dinapoli@rodan.acs.syr.edu (Ron
DiNapoli) writes:
> ... advised me to upgrade to MPW C 3.1.  I did this, but the problem 
> did not go away.  Well I finally discovered what was going wrong... I had the
> following macro defined:
> 
> 	#define OP(tkdef,tkname,d1,d2) {#tkdef,tkname,d1,d2}
> 
> and I wanted to expand the following:
> 
> 	OP(TK_SSLASH,"//",ASSC_YFX,400)
> 
> Anybody see the problem?  In MPW C 3.0 and later, the '//' symbol stands
> for a comment.  It also seems that the preprocessor is unable to detect
> that this particular comment character is occurring within a string.  
> Since it doesn't, the preprocessor sees the open end of a string, and never
> finds the closing " (because the real one was 'commented' out) before its
> internal buffer overflows.  Once discovered, the fix was easy.  I just 
> changed this particular occurrance of OP to:
> 
> 	OP(TK_SSLASH,"/\/",ASSC_YFX,400)
> 
> 
> So, it this a bug?  In my opinion it isn't.  Take for example the following 
> macro definition:
[example deleted]
> Note how in the first occurrance of the PRINTIT macro, the text between the
> /* */ comment characters does not appear.  This tells me that in the 3.0/3.1
> C preprocessor, comments are "taken out" regardless of where they appear.
> Thus my problems with the '//' in my string seems consistent with this
> philosophy.  For this reason, I would say it is not a bug at all!

From Kernighan and Richie (2nd edition; p. 192):
"  Comments do not nest, and they do not occur within string or
   character literals.             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"  ^^^^^^^^^^^^^^^^^^
I haven't checked the behaviour you reported, but if MPW C 3 behaves as
you suggest it does, this IS a bug.

Philip Machanick
philip@pescadero.stanford.edu