Python 中的迷你语言

发布于 2024-08-07 13:06:41 字数 151 浏览 4 评论 0原文

我正在用 Python 创建一个简单的迷你语言解析器,靠近问题域进行编程等等。

不管怎样,我想知道这里的人会如何做这件事——在 Python 中做这种事情的首选方法是什么?

我不会给出我所追求的具体细节,因为目前我只是研究整个领域在 Python 中的易用性。

I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.

Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python?

I'm not going to give specific details of what I'm after because at the moment I'm just investigating how easy this whole field is in Python.

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

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

发布评论

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

评论(6

不弃不离 2024-08-14 13:06:41

Pyparsing 对于编写“小语言”来说很方便。我在 PyCon'06 上发表了关于编写简单冒险游戏的演讲引擎,其中被解析和解释的语言是游戏命令集(“库存”、“拿剑”、“掉落书”等)。 (源代码此处。)

您还可以找到以下链接: pyparsing wiki 上的其他 pyparsing 文章。

Pyparsing is handy for writing "little languages". I gave a presentation at PyCon'06 on writing a simple adventure game engine, in which the language being parsed and interpreted was the game command set ("inventory", "take sword", "drop book", etc.). (Source code here.)

You can also find links to other pyparsing articles at the pyparsing wiki.

梦旅人picnic 2024-08-14 13:06:41

我对 PLY (Python Lex-Yacc) 的经验有限但积极。它将 Lex 和 Yacc 功能组合在一个 Python 类中。您可能想检查一下。

Stackoverflow 研究员 Ned Batchelder 有一个 对其网站上的可用工具进行了很好的概述。还有关于 Python 网站 本身的概述。

I have limited but positive experience with PLY (Python Lex-Yacc). It combines Lex and Yacc functionality in a single Python class. You may want to check it out.

Fellow Stackoverflow'er Ned Batchelder has a nice overview of available tools on his website. There's also an overview on the Python website itself.

二货你真萌 2024-08-14 13:06:41

我会推荐 funcparserlib。它是专门为解析小语言和 DSL 而编写的,它比 pyparsing 更快、更小(请参阅其主页上的统计信息)。极简主义者和函数式程序员应该喜欢 funcparserlib

编辑:顺便说一句,我是这个库的作者,所以我的观点可能有偏见。

I would recommend funcparserlib. It was written especially for parsing little languages and DSLs and it is faster and smaller than pyparsing (see stats on its homepage). Minimalists and functional programmers should like funcparserlib.

Edit: By the way, I'm the author of this library, so my opinion may be biased.

凉城凉梦凉人心 2024-08-14 13:06:41

Python 是一种非常简单且可扩展的语言,我建议只创建一个全面的 python 模块,并针对它进行编码。

我发现当我输入上面的内容时,已经提到了 PLY

Python is such a wonderfully simple and extensible language that I'd suggest merely creating a comprehensive python module, and coding against that.

I see that while I typed up the above, PLY has already been mentioned.

红玫瑰 2024-08-14 13:06:41

如果你现在问我这个问题,我会尝试使用 python 的 textx 库。您可以使用 python 轻松地在其中创建 dsl!优点是它为您创建 AST,并且将词法分析和解析结合在一起。

http://igordejanovic.net/textX/

If you ask me this now, I would try the textx library for python. You can very easily create a dsl in that with python! Advantages are that it creates an AST for you, and lexing and parsing is combined.

http://igordejanovic.net/textX/

旧人九事 2024-08-14 13:06:41

为了提高工作效率,我总是使用解析器生成器,例如 CocoPy (教程)将您的语法转换为(正确的)解析器(除非为了学习,您想手动实现解析器)。

剩下的就是编写实际的解释器/编译器(创建要解释的基于堆栈的字节代码或内存 AST,然后对其进行评估)。

In order to be productive, I'd always use a parser generator like CocoPy (Tutorial) to have your grammar transformed into a (correct) parser (unless you want to implement the parser manually for the sake of learning).

The rest is writing the actual interpreter/compiler (Create stack-based byte code or memory AST to be interpreted and then evaluate it).

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