[comp.lang.c] A Deficiency of the C Preprocess

wagner@iaoobelix.UUCP (12/27/86)

/***** iaoobelix:comp.lang.c / csun!aeusemrs /  1:54 am  Dec 23, 1986*/
Subject: Re: A Deficiency of the C Preprocessor

> Clayton,  if you are running Unix System V, all of your problems
> are solved;  there is another preprocessor called 'm4' that will
> do what you want, and then some. (This might also be true of other systems.)
...
> 						Mike Stump

Sure, you can use M4 to get these repetitions. Yet, it is  impos-
sible  to  use  iterative or (essentially) recursive macro expan-
sions for a C program preprocessor. The best  idea  would  be  to
write  a small LISP or PROLOG program to parse your C code and to
build up a syntactic structure (lists or trees) what shouln't  be
too sophisticated.  Then, use full LISP or PROLOG for the manipu-
lation of your programs and generate from  the  resulting  struc-
tures  a  new C program. Of course, you can write source code op-
timizers, lint-like programs, bug finders and a lot of other nice
programs in such an environment...

But: The main intention of the C preprocessor is not to provide a
full  programming  language for macro rewrites as you can find it
e.g. in LISP. Cpp (and also M4) is a small (!), useful instrument
for  simple  1:1  substitutions  not  requiring  any recursion or
iteration, since source code EXPANSIONS (i.e. transformations en-
larging the amount of code by an unknown factor) of the kind men-
tioned  in  the  original  note  do  not  necessarily  improve  a
program's efficiency by producting a huge block of constant data.
A small loop WILL consume some run-time but if the initializer is
so  large  that  a  simple  iteration  over all the elements will
amount to more than half a second, you won't do this  initializa-
tion by means of source code expansion anyway!

BTW: Even if cpp supported constant time repetitions,  this  does
NOT  require  knowledge  about data structures since it is just a
textual replacement (<foo> gets replaced by <foo>  <delim>  <foo>
...  <foo>).   And  if  you  are  going to implement this kind of
iteration, please use a syntax like DUP(n, form) or DUP(n,  form,
delim).

Juergen Wagner,			(USENET)   ...!unido!iaoobel!wagner
					        wagner@iaoobel.UUCP
				Fraunhofer Institute IAO, Stuttgart