[net.lang.c] define kinda stuff

see@NTA-VAX.ARPA (04/14/86)

David Eppstein (eppstein@cs.columbia.edu, seismo!columbia!cs!eppstein)
writes (11 Apr 86 21:12:26 GMT)

> Obviously the right way to do this is the following:
> 
> 	#define  cat(a,b)  a/**/b
> 	#define  (-:       cat(/,*)
> 	#define  :-)       cat(*,/)

The problem is to define '(-:' as '/*' (start of comment), and :-) '*/'.

Please correct me, but I feel this will not work. Either the cat-definition
will look like 'ab' when it is expanded - obviously wrong -, or it will
look like 'a/**/b', which is wrong also. If you get '//**/*' as the
expansion, this surely isn't a start of a comment. 

The whole thing boils down to whether the preprocessor does recursive 
deletion of comments, which it never has as far as I know. For example:

/*	if (variable != 21) { 		/* Test for ... */

has always been a comment-line, where the first '/*' is an easy
way to discard the whole line. If you put another '*/' at the end of
the line, the 'syntax error' is very near...
	

see@NTA-VAX.ARPA (Stein-Erik Engbr}ten) (04/14/86)

David Eppstein (eppstein@cs.columbia.edu, seismo!columbia!cs!eppstein)
writes (11 Apr 86 21:12:26 GMT)

The problem is to define '(-:' as '/*' (start of comment), and :-) '*/'.

> Obviously the right way to do this is the following:
> 
> 	#define  cat(a,b)  a/**/b
> 	#define  (-:       cat(/,*)
> 	#define  :-)       cat(*,/)

The preprocessor doesn't allow '(-:' as a definition-name 
(Berkeley 4.2/4.3).
	
However, the program below doesn't do what it is supposed to either. 
When the preprocessor has finished, you just get 'main() {'.
	
-->#define cat(a,b)		a/**/b
-->#define start_comm		cat(/,*)
-->#define end_comm		cat(*,/)
-->
-->main()
-->{
-->	start_comm   This is a comment   end_comm
-->	cat(pri,ntf)("This is a nice one...\n");
-->	printf("Hello...\n");
-->}

When in a comment, no expansion is done! 

How do we then define something which is to end a comment? I would say
it is impossible, but is eager to hear of anybody who thinks otherwise.

Stein-Erik Engbr}ten

tainter@ihlpg.UUCP (Tainter) (04/17/86)

> David Eppstein (eppstein@cs.columbia.edu, seismo!columbia!cs!eppstein)
> writes (11 Apr 86 21:12:26 GMT)
> > Obviously the right way to do this is the following:
> > 
> > 	#define  cat(a,b)  a/**/b
> > 	#define  (-:       cat(/,*)
> > 	#define  :-)       cat(*,/)
> The problem is to define '(-:' as '/*' (start of comment), and :-) '*/'.
> Please correct me, but I feel this will not work. Either the cat-definition
> will look like 'ab' when it is expanded - obviously wrong -, or it will
> look like 'a/**/b', which is wrong also. If you get '//**/*' as the
> expansion, this surely isn't a start of a comment. 
> The whole thing boils down to whether the preprocessor does recursive 
> deletion of comments, which it never has as far as I know. For example:
> 
> /*	if (variable != 21) { 		/* Test for ... */
> has always been a comment-line, where the first '/*' is an easy
> way to discard the whole line. If you put another '*/' at the end of
> the line, the 'syntax error' is very near...

Ignoring the unportability of a preprocessor which accepts macros that are
not identifiers.

The assumption the above works on is that BOTH the preprocessor and the
compiler extract comments.  This is a VALID assumption which is very often
violated in the name of efficiency.  The compiler SHOULD strip comments
even if the preprocessor does since you should not assume it will be
preprocessed by a comment stripping preprocessor.  But, SHOULD and does are
different words.

--j.a.tainter

greg@utcsri.UUCP (Gregory Smith) (04/18/86)

In article <2604@brl-smoke.ARPA> see@NTA-VAX.ARPA (Stein-Erik Engbr}ten) writes:
>The problem is to define '(-:' as '/*' (start of comment), and :-) '*/'.
>
>> Obviously the right way to do this is the following:
>> 
>> 	#define  cat(a,b)  a/**/b
>> 	#define  (-:       cat(/,*)
>> 	#define  :-)       cat(*,/)
>
>The preprocessor doesn't allow '(-:' as a definition-name 
>(Berkeley 4.2/4.3).
>	
>However, the program below doesn't do what it is supposed to either. 
>When the preprocessor has finished, you just get 'main() {'.
>	
>-->#define cat(a,b)		a/**/b
>-->#define start_comm		cat(/,*)
>-->#define end_comm		cat(*,/)
>-->
>-->main()
>-->{
>-->	start_comm   This is a comment   end_comm
>-->	cat(pri,ntf)("This is a nice one...\n");
>-->	printf("Hello...\n");
>-->}
True.
>
>When in a comment, no expansion is done! 
>
>How do we then define something which is to end a comment? I would say
>it is impossible, but is eager to hear of anybody who thinks otherwise.
>
>Stein-Erik Engbr}ten

The following method is stolen from one of the winners of last years'
obfuscated-C-code contest ( Col. G. L. Sicherman <decvax!sunybcs!colonel>):

------------------- funny.c ----------------
#define b *
#define start_comm		/b
#define end_comm		b/

main()
{
	start_comm   This is a comment   end_comm
	printf("Hello...\n");
}

-------- cc -E funny.c: --------------------

# 1 "funny.c"




main()
{
		/*   This is a comment   	*/
	printf("Hello...\n");
}

I guess it works because the pre-processor doesn't *know* it is
in a comment. The '/*' doesn't appear until *after* the 'b' is expanded,
and by then the pre-processor is past the '/', so the comment is not
recognized. The following also works (and was used in Col Sicherman's
winning entry):

#define toggle_comment /b/

Of course, this method causes the pre-processor to pass your comments
to the compiler proper.

( By the way, Col Sicherman won the 'worst abuse of the preprocessor'
category ).
-- 
"If you aren't making any mistakes, you aren't doing anything".
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg