Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31045 > unrolled thread

Generating C++ code

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2012-10-09 18:00 +0200
Last post2012-10-11 06:00 -0400
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Generating C++ code Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-10-09 18:00 +0200
    Re: Generating C++ code Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-10 12:47 +0200
    Re: Generating C++ code Tim Roberts <timr@probo.com> - 2012-10-10 20:12 -0700
      Re: Generating C++ code Etienne Robillard <animelovin@gmail.com> - 2012-10-11 06:00 -0400

#31045 — Generating C++ code

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-10-09 18:00 +0200
SubjectGenerating C++ code
Message-ID<mailman.2008.1349798449.27098.python-list@python.org>
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

[toc] | [next] | [standalone]


#31074

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-10-10 12:47 +0200
Message-ID<ib0gk9-ig5.ln1@satorlaser.homedns.org>
In reply to#31045
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

[toc] | [prev] | [next] | [standalone]


#31100

FromTim Roberts <timr@probo.com>
Date2012-10-10 20:12 -0700
Message-ID<s6ec78h5iq43h3vv6t9lqmohb5ej8pafhv@4ax.com>
In reply to#31045
Jean-Michel Pichavant <jeanmichel@sequans.com> 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, timr@probo.com
Providenza & Boekelheide, Inc.

[toc] | [prev] | [next] | [standalone]


#31107

FromEtienne Robillard <animelovin@gmail.com>
Date2012-10-11 06:00 -0400
Message-ID<mailman.2042.1349949642.27098.python-list@python.org>
In reply to#31100
On Wed, 10 Oct 2012 20:12:36 -0700
Tim Roberts <timr@probo.com> wrote:

> Jean-Michel Pichavant <jeanmichel@sequans.com> 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, timr@probo.com
> 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/Interface_description_language
http://en.wikipedia.org/wiki/SWIG

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web