Mailing List Archive

Python as a parser (newbie)
I am creating a program that needs to have scripting support. I've
looked at the faqs and cannot get the answer. Can I use Python's
scripting ability to parse the input I need to give it, and execute
c/c++ functions realtime in my code?

Any help would be appreciated.

Thanks,
Ryan Phillips
Python as a parser (newbie) [ In reply to ]
Ryan> I am creating a program that needs to have scripting support.
Ryan> I've looked at the faqs and cannot get the answer. Can I use
Ryan> Python's scripting ability to parse the input I need to give it,
Ryan> and execute c/c++ functions realtime in my code?

Yes. There are many ways to parse input. Which way will depend largely
upon your input. You can often get a long way by simply using string.split
and rummaging around ad hoc in the tokens it returns. Other more complex
parsing tasks can be handled by regular expressions (the re module). If the
mustard is still too thick to cut, more powerful parsing systems are
available as well, such as Aaron Watters' kwParsing package
(http://www.chordate.com/kwParsing/) and John Aycock's parser generator
(http://www.foretec.com/python/workshops/1998-11/proceedings/papers/aycock-little/aycock-little.html).

As for calling C/C++ functions realtime, "that's what tiggers do best". You
can manually or mostly automatically write wrappers for function or class
libraries that make your C/C++ APIs callable from Python code. For more
information, check out the Python Extending and Embedding documentation
(http://www.python.org/doc/current/ext/ext.html) and the SWIG web site
(http://www.swig.org/).

Skip Montanaro | http://www.mojam.com/
skip@mojam.com | http://www.musi-cal.com/~skip/
847-475-3758
Python as a parser (newbie) [ In reply to ]
From the sound of this, you don't really want to create a new parser. You
just want to embed the Python scripting language in your application, and
export functionality from your application to the embedded python
interpreter.

I have done this with ArcView GIS. It was quite painless. Basically, just
look at the docs: there is a reference on the API for embedding, and a
tutorial on extending Python through modules written in C. It is really
just a matter of of smearing those two ideas together. You application
initializes the Python interpreter, then publishes some of its internal
routines as a Python module, and then whenever your application calls python
code, that Python code will be able to call back to your application through
that module.

It is one of Python's strengths that this is so easy.