Velocity Reviews - Computer Hardware Reviews

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

Thread Tools

Re: Generating C++ code

Michael Torrie
Posts: n/a
On 10/09/2012 10:00 AM, Jean-Michel Pichavant wrote:
> 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.
> Any advice would be appreciated.

There are two ways I can see to do this.

One is to use XSLT. XML is designed to be transformed easily, and XSLT
is the standard vehicle to do this transformation. You can use XSLT to
translate an XML document to HTML, for example, or to another schema of
XML, or to something non-XML like plain text, Tex, or even C++ code.
XSLT itself is turing complete, so complicated, logical transformations
are possible. Since I know of no existing transformation templates for
emitting C++, or any other language, you'll have to write it yourself.
Yes it won't be python code per se, though you can drive the XML XSLT
engine from python using a module called lxml. Anyway, XSLT is
complicated (I have done nothing in it myself), but it is designed for
this kind of thing.

Secondly, you could just use a python XML module of some kind, read the
document in, traverse the nodes and emit code. This is basic compiler
theory, though your source code is likely already in an abstract syntax
tree (XML). And I bet you could make it one pass, as you can rely on
the compiler to check symbol names for you. Compilers are not simple
beasts, but they are fun to write, and very educational. You get to
learn about grammars and recursive descent parsing (though the XML
library basically does both for you).

Reply With Quote

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 ASP .Net Web Controls 2 09-25-2005 05:24 PM
Generating HTML code in the code-behind 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