views:

115

answers:

5

Can anyone point me to some documentation on how to write scripts in Python (or Perl or any other Linux friendly script language) that generate C++ code from XML or py files from the command line. I'd like to be able to write up some xml files and then run a shell command that reads these files and generates .h files with fully inlined functions, e.g. streaming operators, constructors, etc.

A: 

I actually do some work with a legacy "4GL" development environment that does something similar, and a lot of models on the old 4GL paradigm use C and C++ as the language that they output their generated code in.

That being said, 4GL is pretty much resigned to the dust heap of history. The problem is, when you're machine generating the C code, you're losing the performance gains that would have resulted from using C in the first place, and the code is extremely difficult to maintain. Might as well just write the program in Python in the first place.

Satanicpuppy
I'm not looking for an program converter. I'm looking for a document on how to write up my own Python/Perl/etc script that would simply generate C++ datatypes with basic features like streaming operators based on a short data-type specification. The specification would just list the class name , its members , a base class and some simple documentation for each member. The output would be a header file with a fully inlined class definition. I think Python or Perl should be able to do this easily.
A: 

You could take a look at Shedskin, a project that generates C++ code from Python code.

Depending on what your reasons are, it may be a little pointless as Satanicpuppy pointed out.

Wayne Werner
I'm more interested in just writing up a few simple shell scripts to do basic parsing and code gen, if possible. If not, I'll just handwrite the basic classes myself.
I'm not aware of anything more simple than Shedskin - you'd probably want to just roll your own, though some folks have mentioned XML parsers. I imagine it would be fairly trivial to get something fairly simple working.
Wayne Werner
A: 

Have a look at Cheetah. It's a template engine written in Python.

jopa
I have looked at Cheetah a few time already. Even though it says that it is used for C++ generation, there are no examples of this and no documentation of how it is used for C++. I would like to write my own simple generator for basic data types. If this turns out to be too complicated, then I won't bother with any code-gen tool.
+1  A: 

I'm afraid you will not find an already-built in solution that takes your particular xml or python files and transforms them onto your required output "out of the box".

You will have to implement the parsing, the data treatment and output yourself. Not all by yourself, though; here are some pointers regarding the parsing and output.

Phython comes with 2 different XML parsers (SAX and DOM -scroll down to see some examples). You will have to use one of them in order to read the source files.

For generating the output more easily, you can probably use a templating library, such as StringTemplate, or just generate the code manually, if it's small.

egarcia
A: 

A few years ago I worked on a project to simplify interprocess shared memory management for large scale simulation systems. We used a related approach where the layout of data in shared memory was defined in XML files and a code generator, written in python, read the XML and spit out a set of header files defining structures and associated functions/operators/etc to match the XML description. At the time, I looked at several templating engines and, to my surprise, found it was easier and very striaight-forward to just do it "by hand".

As you read the XML, just populate a set of datastructures that match your code. Header file objects contain classes and clsses contain variables (which may be of other class types). Give each object a printSelf() method that iterates over it's contents and calls printSelf() for each object it contains.

It seems a little daunting at first but once you get started, it's pretty straight forward. Oh, and one tip that helps with the generated code, add an indentation argument to printSelf() and increase it at each level. It makes the generated code much easier to read.

Rakis
Okay, I'll try this out and look at StringTemplate too.
Indeed, using strings for templating bits of what you generate is essential. I did most of my code with the simple % operator and it worked out just fine but the new (yeah, the code is old ;-) StringTemplates should make it even easier.
Rakis