[net.lang.c] Self-reproducing C programs

td@alice.UUCP (Tom Duff) (09/19/84)

It is possible to generate (syntactically invalid) C programs whose error message
output from cc is identical to the program input.  An easy way is to use the
following shell script:

echo hi >x
echo bye >junk.c
until cmp -s x junk.c
do	mv x junk.c
	cc junk.c 2>x
done

When this terminates, it will have found a fixed-point of the cc command.
I won't reproduce the output here.  It varies from compiler to compiler,
and can be more than a hundred lines long.

Thanks to Dennis Ritchie and Rob Pike for suggesting this approach.

jim@ism780b.UUCP (10/01/84)

#R:alice:-298800:ism780b:25500021:000:923
ism780b!jim    Sep 30 08:24:00 1984

>It is possible to generate (syntactically invalid) C programs whose error message
>output from cc is identical to the program input.  An easy way is to use the
>following shell script:
>
>echo hi >x
>echo bye >junk.c
>until cmp -s x junk.c
>do      mv x junk.c
>        cc junk.c 2>x
>done
>
>When this terminates, it will have found a fixed-point of the cc command.
>I won't reproduce the output here.  It varies from compiler to compiler,
>and can be more than a hundred lines long.

You mean *if* this terminates.
It seems to me that the "fixed point" is only reached when the file size
limit is reached or if there is an arbitrary limit on the number of error
messages produced, since for most if not all compilers this procedure
is divergent.  Also, a compiler which, .e.g., printed the input line with
the erroneous portion(s) bracketed would diverge on the first line.

-- Jim Balter, INTERACTIVE Systems (ima!jim)

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (10/02/84)

On the UNIX System V VAX pcc, the iteration converges rather quickly
to produce a one-line fixed point (something like:
	junk.c: line 1: syntax error
).  It is certainly true that wordy compilers can diverge.

ka@hou3c.UUCP (Kenneth Almquist) (10/04/84)

For most pcc based compilers, the command cc junk.c will produce a copy
of junk.c if junk.c contains:

"junk.c", line 1: syntax error

					Kenneth Almquist