Is there a computer science theory behind automatic source code generation
I don't know anything about it but the field as a whole is called "automatic programming", which should give you pointers of where to look.