Python 与 Lua 的嵌入式脚本/文本处理引擎

发布于 2024-09-24 05:49:23 字数 251 浏览 2 评论 0原文

对于我目前正在进行的一个项目,我希望将脚本引擎嵌入到我的 C++ 代码中,以实现一定的可扩展性。该应用程序将需要大量的文本处理以及在这些脚本中使用正则表达式。

我知道 Lua 在嵌入式脚本方面通常是业界的宠儿,但我也知道它不支持正则表达式(至少开箱即用)。这导致我倾向于使用 python 来嵌入我的语言,因为它似乎在 Lua 背后拥有最好的支持,并且仍然提供强大的正则表达式功能。

这是正确的选择吗?我应该考虑另一种语言吗?我有理由再看一下 Lua 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很糊涂小朋友 2024-10-01 05:49:23

如果您特别需要通常所说的“正则表达式”(根本不是正则表达式),那么您有两个选择:

  1. 使用 Python。它包含的正则表达式与 Perl 的非常相似,sed/grep
  2. 使用 Lua 和外部 PCRE 库另一方面

,如果您需要任何良好的模式匹配,您可以继续使用 Lua,或者:

  1. 使用 Lua 包含的模式匹配,它不属于 grep 传统,但功能相当强大。缺少的功能是子模式替代方案 (|)
  2. 使用 LPEG,它们是 < em>远比正则表达式更强大,而且通常也更快。

正如你所知,我是最后一个的忠实粉丝。它不仅可以让您定义非常复杂但确定性的模式,它还是一个完整的语法工具,您可以使用它来创建整个解析器。如果您愿意,可以用单个多行字符串常量来描述语法,并使用您自己定义的挂钩来捕获数据并构建结构。

我用它来快速破解 JSON 解析器、C 调用树、xPath 库等。

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.

〃安静 2024-10-01 05:49:23

boost.python。如果熟悉 C++ 源代码的人主要是编写脚本的人,您可能会发现这更方便。

即使脚本编写者不熟悉您的特定源,如果他们更熟悉类似 C 的语法(C、C++ 等),他们应该会发现 Python 更容易使用——也许只是稍微好一点,Lua 并不难。无论如何,优秀的程序员可以使用多种语言,但您没有提供有关您的受众的任何信息。

Lua 比 Python 更容易沙箱化,因此如果您必须限制脚本可以执行的操作(例如生成额外的进程、读取文件),则可能会排除 Python。

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.

執念 2024-10-01 05:49:23

我自己将 Lua 合并到我的一个 C 项目中,因此我建议使用 Lua,因为这更容易。

但这取决于您的脚本语言需要具备什么能力。 Lua 成为事实上的游戏脚本语言。如果您需要高级脚本功能,您可能会使用Python,但如果只是为了简单的脚本支持,请选择Lua。据我所知,对于不习惯编写脚本的新手来说,Lua 更容易学习。

我认为,Lua 更轻,如果你需要外部包,你可以添加它们,但重点是,Lua 的原子部分比 Python 小得多。

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.

染年凉城似染瑾 2024-10-01 05:49:23

不要忘记它们的祖父 - tcl,

tcl 有一个 c++ 包装器,这使得嵌入起来非常容易,

我正在当前项目中使用它,

在之前的(c#)项目中我使用 lua 而不是 python。在较旧的 c# 项目中,我使用过 python;
我选择 lua 是因为它的语法对于普通脚本编写者来说更正常(用于 vbscript 或 javascript)。不过,我将在下一个 C# 项目中改回 (iron)python; lua 太晦涩了

对于 c++ 我从现在起将始终使用 tcl

编辑:我最喜欢的是 jint (.net javascriptt 解释器)v 易于使用,界面漂亮。鉴于 js 是目前最酷的语言,没有人可以抱怨这种语言

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文