Generating C++ code

  • Thread starter Jean-Michel Pichavant
  • Start date
J

Jean-Michel Pichavant

Greetings,

I'm trying to generate C++ code from an XML file. I'd like to use a template engine, which imo produce something readable and maintainable.
My google search about this subject has been quite unsuccessful, I've been redirected to template engine specific to html mostly.

Does anybody knows a python template engine for generating C++ code ?

Here's my flow:

XML file -> nice python app -> C++ code
From what I know I could use Cheetah, a generic template engine. I never used it though, I'm not sure this is what I need.
I'm familiar with jinja2 but I'm not sure I could use it to generate C++ code, did anybody try ? (maybe that's a silly question)

Any advice would be appreciated.

JM
 
U

Ulrich Eckhardt

Am 09.10.2012 18:00, schrieb Jean-Michel Pichavant:
I'm trying to generate C++ code from an XML file. I'd like to use a
template engine, which imo produce something readable and
maintainable.
[...]
Here's my flow:

XML file -> nice python app -> C++ code

There is one question that you should answer (or maybe decide?) first:
How close is the XML structure to C++ semantically?

The syntactic level is obviously very different, as one uses XML as
metaformat while the other is C++. The semantic level is rather about
the question if there is e.g. a "<class name='foo'>" that directly
translates to a "class foo {" in C++. If that is the case, the SAX API
should help you, as it basically invokes callbacks for every XML element
encountered while parsing the input stream. In those callbacks, you
could then generate the according C++ code in a way that should be
readable and maintainable with plain Python or some template engine.

You you need to skip back-and-forth over the input, reading the whole
XML as DOM tree would probably be a better approach. Still, the
processing of input is separate from output generation, so you could at
least divide your task before conquering it.

Notes:
- There is also XSLT which can generate pretty much anything from XML,
but it is can't do much more than text replacements triggered by input
matching. The more the output differs semantically from the input, the
more difficult it becomes to use. Also, XSLT tends to become write-only
code, i.e. unreadable.
- I think there was a feature in GCC that allows generating XML from
C++ input, maybe even the reverse. Maybe you could leverage that?


Good luck!

Uli
 
E

Etienne Robillard

I'm a big fan of Cheetah. It's simple but flexible enough to be useful.
Besides the many web projects I've done with it, I also I use it in one
project to generate PHP code (it generates data access objects from a live
database schema).

Also take a look at IDL, for a proper way to handle interface generation in C++. No python or cheetah
required as by definition your interfaces should be portable. What your describing is more or less look
like a hack or something which would be a pain to maintain without SWIG or something more suited for
this purposes than XML.

http://en.wikipedia.org/wiki/Interface_description_language
http://en.wikipedia.org/wiki/SWIG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top