Friday, October 3, 2008


Lately I've started to put pressure on my studies. One task I have taken for now is to extend an old academic application (C++), which involves improving the internal format, as well as the interfaces between sub-programs, etc. It is also due to some integration possibilities that we have to rethink the design of the application.

Now, so far I have no experience with C/C++ application development. Java is no-go, as the other academic tools in our landscape (embedded systems) are mostly C++. Therefore I'm thinking that I need some workaround here in order no to violate the overall structure and do it as quick as if I would do it in Java.

As one of my goals is to create a grammar-based input to the application, the natural choice for me is ANTLR. It provides many targets for the input grammar, C++ included. Now if I'm trying to escape C++ coding, and Java is not the language that is appropriate for this task (I think), then I'd rather pick some scripting language.
Python has a nice extension mechanism as well as ANTLR v3 supports the Python target. Also, it is claimed that the Python learning curve is quite flat, which makes sense in my case.

I've found a great tutorial on the C extensions for Python. What amazes me the most, is the ease of the extension compilation. One way is to compile it using gcc from command line. But the other way is to create a simple Python script ( which will do everything for you:

from distutils.core import setup, Extension
module1 = Extension('extension_name', sources = ['myfile.c'])
setup (name = 'PackageName',
version = '1.0',
description = 'my demo package',
ext_modules = [module1])

Now the script can be executed in the same directory where the myfile.c resides: python build.

The only thing I had to do extra on my machine is to install the python-dev package via the Synaptic Package Manager.

What I want to do now, is to create a C++ class hierarchy to model the internal format of the current application, then wrap it with some C++ code that will convert Python types to C++ types and vice versa. And after that I can create a Python binding and ANTLR grammar with Python target to convert the input format using Python code into the C++ classes. Bingo! :)

Disqus for Code Impossible