如何在另一种语言之上开发特定领域的语言?

发布于 2024-08-31 15:32:40 字数 318 浏览 1 评论 0原文

假设我发现了一个用 python 编写的很好的开源软件/库。我想将我创建的一些函数或方法包装成我自己的易于理解的语言。

do porter_stemm(DOC)(DSL)相当于用 python 编写的函数或一系列方法。

我想创建一个易于学习的 DSL,但需要将这个 DSL 翻译成原始的开源软件。

我不确定我是否清楚,但我的意图是:

  1. 创建一种易于学习的代码语言,用户可以使用它来解决特定领域的问题。
  2. 这种简单的语言需要通过一些中间件翻译、编译或解释为原始开源软件的语言(python)。

say i found a good open source software/library written in python. i want to wrap some of the functions or methods that i have created into easy to understand language of my own.

do porter_stemm(DOC) (the DSL) would be equivalent to the function or series of methods written in python.

i want to create a DSL that is easy to learn, but need this DSL translated into the original open source software software.

im not sure if i am clear here but my intention is:

  1. create an easy to learn code language that users can use to solve a problem in a certain niche.
  2. this simple language needs to be translated or compiled or interpretated via some middleware into the original open source software's language (python).

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

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

发布评论

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

评论(2

箜明 2024-09-07 15:32:40

编写 DSL 的语法和解析器,例如,在 Python 中,使用 pyparsing (比传统的 lexx 更简单) -yacc 类似方法)——解析器可以生成一棵具有语义意义的节点的树,然后您可以(更简单)遍历该树并解释它,或者(稍微不那么简单)遍历该树并生成等效的 Python 代码。这是我为大多数宿主语言建议的方法。一些宿主语言(主要是 Lisp 和Scheme)具有强大的宏,因此在这些语言中用宏构建 DSL 更为常见。

将 DSL 嵌入到宿主语言中基本上意味着您实际上并不是在做 DSL,而是在做一个更传统的框架,因此这确实是一种不同的方法(可能更强大,但对于非程序员来说可能不太容易学习;-) 。

Write your DSL's syntax and a parser for it, e.g., in Python, with pyparsing (simpler than the traditional lexx-yacc like approach) -- the parser can produce a tree of semantically meaningful nodes, and then you can (simpler) walk that tree and interpret it, or (a bit less simple) walk that tree and generate equivalent Python code. That's the approach I'd suggest for most host languages. Some host languages (Lisp and Scheme being the main ones) have powerful macros, so building a DSL out of macros is more common in those languages.

Embedding your DSL in the host language basically means you're not really doing a DSL but a more traditional framework, so that's really a different approach (possibly more powerful, but may be not quite as easy to learn for non-programmers;-).

温柔嚣张 2024-09-07 15:32:40

通常,DSL 仍然具有与其宿主语言相同的基本语法,并且只是扩展该语言的功能。所以你的一切仍然是用 Python 编写的,它只是带有一些特定于领域的函数、变量等的 Python。

我对 DSL 的经验主要来自 Lisp 系列语言。在那里,典型的 DSL 可以通过宏来实现,宏生成特定于 DSL 的代码模式。 (另外,还有与 DSL 相关的功能)。关键是,尽管宏和函数可以扩展该语言的功能,但它们仍然以该语言实现。

我想你可以更进一步,用 Python 编写一个解释器 - 但你仍然可以免费获得 Python 可运行性,因为你的解释器将你的自定义语言翻译回 Python。

Typically a DSL would still have the same basic syntax as its host language, and simply extend the language's capabilities. So everything for you is still written in Python, it's just Python with some domain-specific functions, variables, etc.

My experience with DSLs is mostly with the Lisp family of languages. There, a typical DSL might be implemented with Macros, which generate code-patterns that are specific to the DSL. (Plus, functions relating to the DSL). The point is, although the macros and functions may extend the capabilities of the language, they are still implemented in that language.

I suppose you could go a step farther and write an interpreter in Python - but there you still get Python runnability for free, as your interpreter translates your custom language back into Python.

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