Generating C++ code

Discussion in 'Python' started by Jean-Michel Pichavant, Oct 9, 2012.

  1. 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
    Jean-Michel Pichavant, Oct 9, 2012
    #1
    1. Advertising

  2. 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
    Ulrich Eckhardt, Oct 10, 2012
    #2
    1. Advertising

  3. On Wed, 10 Oct 2012 20:12:36 -0700
    Tim Roberts <> wrote:

    > Jean-Michel Pichavant <> 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,
    > 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
    Etienne Robillard, Oct 11, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    4
    Views:
    741
    Nevyn Twyll
    Mar 6, 2004
  2. =?Utf-8?B?Y25pY2ts?=

    Generating HTML code

    =?Utf-8?B?Y25pY2ts?=, Mar 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    418
    Kevin Spencer
    Mar 22, 2005
  3. Naveen K Kohli

    td tag generating faulty code

    Naveen K Kohli, May 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    453
    David Lloyd
    May 13, 2005
  4. Generating HTML code in the code-behind

    , Aug 25, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    141
    jjrdk
    Sep 25, 2005
  5. Generating HTML code in the code-behind

    , Aug 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    150
    jjrdk
    Sep 25, 2005
Loading...

Share This Page