Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Generating C++ code

Reply
Thread Tools

Generating C++ code

 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      10-09-2012
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
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      10-10-2012
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
 
Reply With Quote
 
 
 
 
Etienne Robillard
Guest
Posts: n/a
 
      10-11-2012
On Wed, 10 Oct 2012 20:12:36 -0700
Tim Roberts <(E-Mail Removed)> wrote:

> Jean-Michel Pichavant <(E-Mail Removed)> wrote:
> >
> >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 ?

>
> 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).
> --
> Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
> Providenza & Boekelheide, Inc.
> --
> http://mail.python.org/mailman/listinfo/python-list


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/Interfa...ption_language
http://en.wikipedia.org/wiki/SWIG
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating HTML code in the code-behind lisa@starways.net ASP .Net Web Controls 2 09-25-2005 05:24 PM
Generating HTML code in the code-behind lisa@starways.net ASP .Net Building Controls 2 09-25-2005 05:24 PM
td tag generating faulty code Naveen K Kohli ASP .Net 1 05-13-2005 09:00 PM
Generating HTML code =?Utf-8?B?Y25pY2ts?= ASP .Net 2 03-22-2005 02:24 PM
Generating Javascript from a button in Code-behind Nevyn Twyll ASP .Net 4 03-06-2004 08:31 AM



Advertisments