Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Generating C++ code

 
 
Stefan Behnel
Guest
Posts: n/a
 
      10-10-2012
Jean-Michel Pichavant, 10.10.2012 11:59:
> Well, the C++ code will end up running on a MIPS on a SOC,
> unfortunately, python is not an option here. The xml to C++ makes a lot
> of sense, because only a small part of the code is generated that way
> (everything related to log & fatal events). Everything else is written
> directly in C++.
>
> To answer Andrea's question, the files are regenerated for every
> compilation (well, unless the xml didn't change, but the xml is highly
> subject to changes, that's actually its purpose)
>
> Currently we already have a python script that translate this xml file
> to C++, but it's done in a way that is difficult to maintain. Basically,
> when parsing the xml file, it writes the generated C++ code. Something
> like:
>
> if 'blabla' in xml:
> h_file.write("#define blabla 55", append="top")
> c_file.write("someglobal = blabla", append="bottom")
>
> This is working, but the python code is quite difficult to maintain,
> there's a lot of escaping going on, it's almost impossible to see the
> structure of the c files unless generating one and hopping it's
> successful. It's also quite difficult to insert code exactly where you
> want, because you do not know the order in which the xml trees are
> defined then parsed.
>
> I was just wondering if a template engine would help. Maybe not.


Depends. Template engines are great for injecting small data snippets into
large static code blocks. They are less good for finely structured code
with conditional insertions and varying code order all over the place.

In Cython, we use a combination of both: a template engine for large code
blocks with small adaptations and line-by-line generated C code for highly
varying code. Works nicely.

As for a template engine, we use Tempita for the more complex stuff
(because the implementation is small and can be embedded) and
string.Template for the simple stuff. If you need something more advanced
straight away, I'd say go for Cheetah.

In case you decide not to use a template engine at all, given that your
input format is XML, consider using lxml to build up your code tree for you
by using custom element classes. Then, generate the code recursively top-down.

http://lxml.de/element_classes.html

Might or might not be a suitable approach, depending on the complexity of
your mapping from XML to code.

Stefan

 
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