Python vs Lua for embedded scripting/text processing engine

Wade Tandy picture Wade Tandy · Sep 23, 2010 · Viewed 9.1k times · Source

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?

Answer

Javier picture Javier · Sep 23, 2010

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.