views:

274

answers:

4

For a project I'm currently working on, I'm looking to embed a scripting engine into my C++ code to allow for some extensibility down the line. The application will require a fair amount of text processing and the use of regular expressions within these scripts.

I know Lua is generally the industry darling when it comes to embedded scripting, but I also know it doesn't support regular expressions (at least out of the box). This is causing me to lean toward python for my language to embed, as it seems to have the best support behind Lua and still offers powerful regex capabilities.

Is this the right choice? Should I be looking at another language? Is there a reason I should give Lua a second look?

+2  A: 

dont forget the grand-daddy of them all - tcl

there is a c++ wrapper for tcl which makes it incredibly easy to embed

i am using it in a current project

in previous (c#) project I used lua over python. In older c# projects I had used python; I chose lua because the syntax is more normal for average scripter (used to vbscript or javascript). However I will change back to (iron)python for next c# project; lua is just too obscure

For c++ I will always use tcl from now on

pm100
+1 for TCL answer!
TokenMacGuy
Tcl was designed from the ground up for this kind of work, and it's still the best. I can only assume that the original authors of Lua simply hadn't heard of it!
Gaius
+10  A: 

if you need specifically what is commonly known as 'regular expressions' (which aren't regular at all), then you have two choices:

  1. go with Python. it's included regexp is similar enough to Perl's and sed/grep
  2. use Lua and an external PCRE library

if, on the other hand, you need any good pattern matching, you can stay with Lua and either:

  1. use Lua's included pattern matching, which aren't in the grep tradition but are quite capable. The missing functionality is subpattern alternatives (|)
  2. use LPEG, which are far more powerful than regexps, and usually faster too.

As you can tell, i'm a big fan of the last. It not only lets you define very complex but deterministic patterns, it's a full grammar tool that you can use to create a whole parser. If you wish, the grammar can be described in a single multi-line string constant, with your own defined hooks to capture data and build your structures.

i've used it to quickly hack a JSON parser, a C call-tree, an xPath library, etc.

Javier
The built-in patterns include a trick that PCRE and the like have a lot of trouble doing: balanced parenthesis matching with `%b()`. Any pair of distinct characters can be used; the item matches strings that start with `(`, end with `)`, and where the `(` and `)` are balanced.
RBerteig
Having never programmed anything more than a simple World of Warcraft extension in Lua myself, can you speak to the abilities to design advanced classes and functions? I know it supports object oriented programming, but I've only heard of it being used for fairly simple applications. I can foresee my developers using this for advanced machine learning functions, as well as network communication. I've done all of these in Python with no problem. Is Lua as straightforward for these things?
Wade Tandy
@Wade Tandy: that's a whole big subject. In fact Lua doesn't 'come with' OOP facilities, but it's syntax supports many different OO styles. for most uses, just a couple of lines to create appropriate metatables is enough. but if you need full static-like class-based inheritance, it quickly becomes a non-trivial project by itself. in my case, i've _very_ seldom wanted class-based OOP. encapsulation and polymorphism are the real advantages of OOP, and you get them almost automatically with a little code discipline
Javier
+5  A: 

Python and C++ integration is greatly helped with boost.python. You may find this much more convenient if those familiar with your C++ source are primarily the ones writing scripts.

Even if the scripters aren't familiar with your particular source, if they are more familiar with C-like syntax (C, C++, etc.), they should find Python easier to use—perhaps only slightly, Lua isn't hard. Good programmers can use a multitude of languages anyway, but you've not given any information about your audience.

Lua is much easier to sandbox than Python, so if you must restrict what scripts can do (e.g. spawn additional processes, read files), that may rule out Python.

Roger Pate
boost.python? Sounds interesting. How good is it at freeing you from worrying about Python's state?
Charles Stewart
@Charles: Internal interpreter state, such as you have to worry about when using Python's C API? I've not yet had to even think about that with boost.python, but I've also not used it extensively.
Roger Pate
Right, and boundary issues with the memory managers. The boost.python FAQ says it doesn't work with multiple threads without a fair bit of care.
Charles Stewart
Does anything work with multiple threads without a fair bit of care? :)
Roger Pate
+4  A: 

Having incorporated Lua in one of my C projects myself, I'll suggest Lua, as this is easier.

But that depends on what your scripting language needs to be capable of. Lua rose to the de-facto scripting language of games. If you need advanced scripting capabilities, you might use Python, but if it's just for easy scripting support, take Lua. From what I've seen, Lua is easier to learn for newbees, that aren't used to scripting.

I'd argue, that Lua is lighter, if you need to have external packages, you can add them, but the point is, the atomic part of Lua, is much smaller than that of Python.

polemon