Code, generate thyself!

T

Thomas Stegen

I have written a code generator. To be more specific it is a
code generator generator. As in a generator that generates
code generators. If you run the generator on its own source
code you get a code generator generator code generator. If you
run it on some other non-generator code you get a code generator
generator generator. Watch your bracketisation in all that. The
idea is to generate code which generate code which looks exactly
like the input to the the generator.


#include <stdio.h>

int main(void)
{
int c;

printf("#include <stdio.h>\n"
"\n"
"int main(void)\n"
"{\n"
"puts(\"");

while((c = getchar()) != EOF)
{
switch(c)
{
case '"':
printf("\\\"");
break;
case '\\':
printf("\\\\");
break;
case '\n':
printf("\\n\\\n");
break;
default:
putchar(c);
}
}

printf("\");\n"
"return 0;\n"
"}\n");
return 0;
}
 
R

Richard Heathfield

Thomas said:
I have written a code generator. To be more specific it is a
code generator generator. As in a generator that generates
code generators. If you run the generator on its own source
code you get a code generator generator code generator. If you
run it on some other non-generator code you get a code generator
generator generator. Watch your bracketisation in all that. The
idea is to generate code which generate code which looks exactly
like the input to the the generator.

Nice. :)

Some years ago, I wrote a code generator generator generator generator
generator generator generator generator generator generator. You can find
it at http://users.powernet.co.uk/eton/c - I hope you like it. Your
compiler may complain at gcg8.c because of line lengths. If so, drop to
gcg6.c instead.



I suppose this is as good a place as any to announce that version 1.0c of
CLINT is now available for brickbats and bug reports.

http://www.rjgh.co.uk

New in this version: string vectors, and some bit "array" macros.

Forthcoming: a leak detector based on the CLINT library (so that the malloc
wrapper can actually be useful at last).
 
S

Sidney Cadot

Thomas said:
I have written a code generator. To be more specific it is a
code generator generator. As in a generator that generates
code generators. If you run the generator on its own source
code you get a code generator generator code generator. If you
run it on some other non-generator code you get a code generator
generator generator. Watch your bracketisation in all that. The
idea is to generate code which generate code which looks exactly
like the input to the the generator.

Ouch, my head hurts :)

You will surely be able to generate big source code with this by
repeated application, but to a compiler they will all look like a very
simple program, just containing the puts() call; string concatenation is
done by the preprocessor instead of the compiler. Just invoke the
preprocessor directly on one of your generated files to see this more
clearly:

$ cpp generated-code.c

For non-trivial input to your program you are bound to exceed at least
the standard's limits on source line length and/or string literal length
(for C99, these are both set at 4095).

Considering all this, your compilation for non-small programs will be
I/O-bound; to get 20 minutes of compilation time on a machine that reads
at just 10 MB/sec (which is quite modest for current hardware), your
compiler must be able to handle a 12GB string literal. Good luck! ;-)

Best regards,

Sidney
 
M

Morris Dovey

Sidney said:
Ouch, my head hurts :)
to get 20 minutes of compilation time on a machine that reads
at just 10 MB/sec (which is quite modest for current
hardware), your compiler must be able to handle a 12GB string
literal.

Sounds like time to implement a code degenerator.

Oh! ( I already am 8^)
 
M

Mantorok Redgormor

Richard Heathfield said:
Nice. :)

Some years ago, I wrote a code generator generator generator generator
generator generator generator generator generator generator. You can find
it at http://users.powernet.co.uk/eton/c - I hope you like it. Your
compiler may complain at gcg8.c because of line lengths. If so, drop to
gcg6.c instead.

What is the technique you use to produce the proper values and construct
a string literal to create an ascii art image?
 
R

Richard Heathfield

Mantorok said:
What is the technique you use to produce the proper values and construct
a string literal to create an ascii art image?

This is really a programming question, not a C question. I suggest you ask
it in comp.programming instead.
 
A

Arthur J. O'Dwyer

This is really a programming question, not a C question. I suggest you ask
it in comp.programming instead.

And in regard to the 'clint' program at
http://users.powernet.co.uk/eton/c
it looks like Richard just ran the image through
a filter to get some pixel values, then mapped those
integer values onto ASCII characters with the right
shapes. Not really a "real" C program at all -- just
a way of "using C" to display a pre-recorded image.
(Woulda been more interesting if the image weren't
hardcoded, Richard... but that's what aalib is for,
right? ;)

-Arthur
 
L

Larry Doolittle

Arthur said:
And in regard to the 'clint' program at
http://users.powernet.co.uk/eton/c
it looks like Richard just ran the image through
a filter to get some pixel values, then mapped those
integer values onto ASCII characters with the right
shapes. Not really a "real" C program at all -- just
a way of "using C" to display a pre-recorded image.
(Woulda been more interesting if the image weren't
hardcoded, Richard... but that's what aalib is for,
right? ;)

Now the real trick, where you could get yourself on-topic
in a hurry, is for the ASCII art to _itself_ be a valid
C program. Barring the trivial
char *a="....ASCII...........HERE........"
"....................ART.................";
style of submission.

- Larry
 
R

Richard Heathfield

Larry said:
Now the real trick, where you could get yourself on-topic
in a hurry, is for the ASCII art to _itself_ be a valid
C program.

If you look at the original program, you'll see that it is a valid C program
/and/, looked at in a funny light and with a slight squint, you can make
out the picture in the source code, even though it's just numbers in an
array at that stage. But of course that's not quite what you're suggesting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top