16
votes

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?

4
The key to this question is balancing what you get against the hoops the language will make you jump through, something that only seems to be explicitly addressed in Roger's answer.Charles Stewart
Lua does have pattern matching out of the box although it is not POSIX reg exps - lua.org/pil/20.2.htmlNick Van Brunt

4 Answers

19
votes

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.

7
votes

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.

5
votes

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.

4
votes

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

EDIT: My new favorite is jint (.net javascriptt interpreter) v easy to use, nice interface. And nobody can complain about the language given that js is the cool language at the moment