Where can I find such programs? I think yacc and lex are this kind of programs? I'd like to see example codes especially in C language.
What you are looking for is called Metaprogramming: http://en.wikipedia.org/wiki/Metaprogramming
A good article here (some examples with c) : http://www.ibm.com/developerworks/linux/library/l-metaprog1.html
I assume that for the program to generate programs, it'll have to read in some sort of specification and then generate the program from that. Have a look at MPS from JetBrain. Sort of like a high level program generator. It allows you to create your own DSL and then from that DSL tell it how to generate Java programs.
Another way is to use dynamic languages like Groovy or Ruby and use their meta-programming capability go generate programs. This seems like the way to go viz. to use dynamic languages to define a DSL and then use that DSL to generate solutions using the source language.
If the program that you are going to generate is quite static which minor changes then you can use a templating tool like Stringtemplate to generate the program.
If you can be more specific as to what you are trying to do, perhaps I can give a more detailed explanation.
Well, generating executable code on the fly is what a lot of programs do. Runtime environments (such as the JRE or the CLR) are only a special case of such a program, basically everything that involves a JIT (Just-In-Time) compiler is "a program that writes programs".
There are programming languages having meta-programming capabilities such as .NET/Java Reflection (run-time meta) or C++ templates (compile-time meta).
LINQ is a general purpose query language that works by compiling queries represented by expression trees into executable code at runtime.
Self modifying code is used in special computations for performance optimizations.
At a fundemental level, this is what every compiler does. It parses the human-readable code and transforms it into assembly language (and therefore bytecode) that the computer can understand.
One good example on the CPAN is the ORLite module.
This ostensibly provides a light weight ORM that is specific to SQLite and designed for databases embedded inside (desktop etc) applications.
What it actually does is scan the schema of the SQLite database and then generate an entire customised tree of classes to provide an entire different ORM API for each database.
#include <stdio.h>
int main(int argc, char * argv[]) {
FILE * f = fopen("source.c", "w");
if (!f) {
exit(1);
}
fprintf(f,
"#include <stdio.h>\n"
"int main(int argc, char * argv[]) {\n"
"\tprintf(\"Hello World\\n\");\n"
"\treturn 0;\n"
"}\n");
fclose(f);
printf("Check out source.c\n");
return 0;
}
Are you looking to generate stencils for programming?
The Eclipse editor can assist in generating a lot of stencil code for new functions based on old ones.
There are many applications that generate programs based on a set of data or criteria. It's a matter of connecting libraries and functions based on input data.