By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Code, generate thyself!

P: n/a
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;
}

--
Thomas.

Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Thomas Stegen wrote:
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).

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2

P: n/a
Thomas Stegen wrote:
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

Nov 13 '05 #3

P: n/a
Sidney Cadot wrote:
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^)

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 13 '05 #4

P: n/a
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bp**********@hercules.btinternet.com>...
Thomas Stegen wrote:
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.


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


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).


--
nethlek
Nov 13 '05 #5

P: n/a
Mantorok Redgormor wrote:
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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6

P: n/a

On Mon, 17 Nov 2003, Richard Heathfield wrote:

Mantorok Redgormor wrote:

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.


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
Nov 13 '05 #7

P: n/a
Arthur J. O'Dwyer wrote:
On Mon, 17 Nov 2003, Richard Heathfield wrote:
Mantorok Redgormor wrote:
>
> 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.

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
Nov 13 '05 #8

P: n/a
Larry Doolittle wrote:
Arthur J. O'Dwyer wrote:
On Mon, 17 Nov 2003, Richard Heathfield wrote:
Mantorok Redgormor wrote:
>
> 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.

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.


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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.