473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Code, generate thyself!

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
8 2362
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
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
3
by: Aldo | last post by:
hello i'm trying to translate this asp code to php, but i can't get it work right. could anyone help me? thanks! a. ASP CODE ---------------------
16
by: Tran Tuan Anh | last post by:
Dear all: I need your advice on this matter. I am working on a program which takes some pieces of System-C code in and generate some other System-C code. (System-C code is just C++ with some...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
9
by: Henk Verhoeven | last post by:
We are not alone! "Where other MDA tools are generating programmingcode, Codeless chooses not to generate code at all". OK, phpPeanuts is not an MDA tool (it has no fancy modeling GUI). But it...
9
by: Hayato Iriumi | last post by:
Hello, I hear some hypes about creating code to generate C# or VB .NET code, that is, code generation (sounds straight forward enough). I haven't really seen how it's done in real world. I'm...
1
by: A Traveler | last post by:
Hello, i am having this problem. The exact error message is: "Unable to generate code for a value of type 'System.Web.UI.Page'. This error occurred while trying to generate the property value for...
3
by: joshblair | last post by:
Hello, Has anyone ever seen or created such a code generator? I'm looking for a sample of a code generator that will generate code (preferably one that uses C# and the XMLTextWriter) to create...
13
by: Ramon F Herrera | last post by:
I am writing a program that generates source code. See a snippet below. My question is about the use of that growing 'code' variable. Is it efficient? Is is recommended for this case? The code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.