什么是领域特定语言? 有人用吗? 又以什么方式呢?
我想我正在寻找某种介绍,看看是否有人使用过它。 使用它有什么特别的优点吗?
维基百科:
领域特定语言(DSL)是专用于特定领域的编程语言或规范语言问题域、特定问题表示技术和/或特定解决方案技术。
任何人都可以提供任何具体示例来说明您如何实现它或者它如何在给定场景中发挥作用吗?
I guess I am looking for some kind of intro and see if anybody have used it. Are there any particular advantages of using it?
Wikipedia:
domain-specific language (DSL) is a programming language or specification language dedicated to a particular problem domain, a particular problem representation technique, and/or a particular solution technique.
Can anybody give any specific examples of how you have implemented it or how it can be useful in a given scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
领域特定语言是为处理特定领域或一组关注点而编写的语言。 它们有很多,比如用于描述软件构建的 make、ant 和 rake,或者用于语言构建的 lexx 和 yacc。 近年来,它们变得很流行,因为一些东西的结合使它们更容易构建。 其中最重要的是 Ruby 的日益普及,它具有多种可以轻松构建新 DSL 的功能。
Martin Fowler 是这一想法的大力支持者,如此处。
A domain specific language is a language that's written to deal with a specific domain or set of concerns. There are a lot of them around, like make, ant, and rake for describing software builds, or lexx and yacc for language construction. In recent years, they've become popular as some things have combined to make them easier to build. Big among those things has been the increasing popularity of Ruby, which has several features that make it easy to build new DSLs.
Martin Fowler is a big proponent of the idea, as here.
您可以将 DSL 视为用更通用的编程语言编写的函数的复杂参数。 真正的编程语言会解析 DSL 代码并用它做一些事情,通常,DSL 代码只关注您想要做的什么,而更大的系统会弄清楚如何 >。
DSL 的示例包括所有查询语言(SQL、XPath 等)、所有模板语言(Django、Smarty 等)、shell 脚本,尤其包括 twill 等命令驱动的 Web 浏览器(主要用于自动化测试) )、数据存储和交换语言(XML、YAML 等)以及文档语言(如 LaTex、HTML 或 CSS)。
一些具有非常灵活语法的语言(例如 TCL 和 Lisp)将其 DSL 直接构建到语言中......如果可能的话。 大多数语言使用字符串,通常从外部文件加载。
使用它们有什么特别的优点吗? 将它们用于预期目的是非常有利的,以至于您会在不知情的情况下转向它们,就像您一直在使用(我认为)SQL 或 HTML 而不将它们视为 DSL 一样。
我敢说有足够的 DSL 来满足您可能需要的任何类型的应用程序; 您几乎肯定不需要学习如何编写自己的代码。
You can think of DSLs as complex arguments for functions written in a more general programming language. The real programming language parses the DSL code and does something with it, typically, the DSL code only focuses on the what you want to do, and the larger system figures out the how.
Examples of DSL include all query languages (SQL, XPath, ...), all template languages (Django, Smarty, ...), shell scripts, especially including stuff like twill, a command driven web browser (mostly used for automated test), data storage and exchange languages (XML, YAML, ...), and document languages like LaTex, HTML or CSS.
Some languages with very flexible syntax like TCL and Lisp build their DSL directly into the language... when possible. The majority of languages use strings, usually loaded from external files.
Are there any particular advantages of using them? Using them for their intended purposes is very advantageous to the point you will turn to them without knowing, just like you have been using (I presume) SQL or HTML without thinking of them as DSLs.
I'll dare saying there are enough DSLs out there for any sort of application you may need; you almost certainly don't need to learn how to write your own one.
(解决问题的症结)
我想我第一次在某个地方看到 DSL 及其定义为“领域特定语言”我还认为这是一种我只是没有听说过的特定的具体语言 - 但是,不,它是针对特定特定语言量身定制的通用术语应用领域。
讽刺的是,如果您刚刚听说 TCL 作为一种“工具命令语言”,您可能会认为, DSL,会有很多用于各种工具的 TCL——但是,不,它是特定脚本语言的特定名称。
(addressing the crux of the question)
I think the first time I saw DSL somewhere and its definition as "domain specific language" I also thought it was a particular, concrete language that I just hadn't heard about -- but, no, its a general term for languages that are tailored to a particular application area.
Ironically, if you had just heard about TCL as a "tool command language" you might think, like DSLs, that there would be lots of TCLs for various tools -- but, no, its the specific name of a particular scripting language.
一切都是 DSL...
汇编器:MOV R1 到 R2
编译器:赋值语句 -- A = A + 1,条件 -- IF (TRUE) ...,分支 -- RETURN
HTML: ...描述嵌套结构
TCP/IP:描述发往/发自地址
PDF:描述文本/图像在纸张上的位置
字体:描述字符
我们用来描述特定过程的任何语言都是DSL。 不幸的是,缺乏领域特定的语言来描述甚至我们最基本的流程,因此我们使用我们所拥有的少数语言来描述我们所做的一切。 “压缩我的网站中的所有 html 文件”需要 300 行 3 或 4 种不同语言才能完成。
要构建 DSL,请确定描述您可以记住且不需要文档的流程所需的最少字符数。 请记住,速度和易用性是主要的设计标准。 解析速度非常快,您使用的任何语法都可以,在大多数情况下,我更喜欢自然语言作为我的语法,“在月初支付员工工资”,但特定领域就是这样,特定领域,您确定最好的语法符合问题。
我会避免使用其他可能很方便但不适合问题的解决方案,例如用于定义数据 (XML) 的 HTML。 CSV 非常有用,它可以解决大多数问题。 JSON 不适合易用性部分,如果 CSV 可以解决大多数问题,那么它会增加不必要的复杂性,这有点矫枉过正。 我们经常使用 EXCEL 来进行 DSL,它非常适合描述 65K 到 1M 行以下的小问题,例如树形结构或菜单,A 列是级别,其他列是图标、颜色、标签等(EXCEL 是可编辑的 CSV)。
我发现HTML并没有真正解决页面布局的问题,所以我摆脱了它并定义了一个确实适合的DSL。 我在页面上定义了 6 个区域:HEADER、BODY、FOOTER、LEFT/RIGHT MARGINS 和 LEFT/RIGHT FULL MARGINS。 然后我可以告诉页面生成器将标题栏、状态栏、菜单、表格、表格……添加到特定单元格。 然后,每个单元格都可以分为任意深度的行和列。 任何样式的页面布局都需要几秒钟。
BODY 包含我的员工表
HEADER 包含一个标题栏标题“Hello World”,需要登录到 Collins Software
菜单 DSL 不适合页面布局 DSL,因此我为菜单构建了一个独特的 DSL。
资源我的主菜单
*定义:菜单、m、级别、标签、图标、操作;
m,0,文件;
m,1,open,open.gif,Dialog Open File;
每个问题都是独特的,计算机可以使用任何格式,DSL 是为人类设计的,因此要使其能够被人类理解,这是他们可以做到的输入并用真实的单词生成语言; 因为我们所描述的是真实的人、地点和事物。
Everything is a DSL...
Assembler: MOV R1 to R2
Compilers: Assignment Statements -- A = A + 1, Conditionals -- IF (TRUE) ..., Branch -- RETURN
HTML: ... describe a nested structure
TCP/IP: describe to/from addresses
PDF: describe text/image placement on paper
Fonts: describe characters
Any language that we use to describe a specific process is a DSL. Unfortunately there is a lack of domain specific languages to describe even our most basic processes, so we use the few languages we do have to describe everything we do. "Zip all html files in my web site" requires 300 lines of 3 or 4 different Languages to complete.
To build a DSL determine the minimum number of characters needed to describe a process that you can remember and does not require documentation. Remember that speed and ease of use are the primary design criteria. Parsing is so fast that any syntax you use is fine, I prefer natural language as my syntax in most cases, "Pay Employees at the first of the month", but domain specific is just that, domain specific, you determine the syntax that best fits the problem.
I would stay away from using other solutions that might be convenient but do not fit the problem such as HTML that was used to define Data (XML). CSV is very useful, it fits most problems. JSON does not fit the ease of use portion, it is overkill that adds unnecessary complications were CSV works for most problem. We use EXCEL a lot for DSL, it works great for describing small problems, under 65K to 1M rows, such as a tree structures or menus, column A is the level, other columns are icons, colors, labels and such (EXCEL is an editable CSV).
I found that HTML did not really solve the problem of page layout, so I got rid of it and defined a DSL that does fit. I defined 6 regions on the page, HEADER, BODY, FOOTER, LEFT/RIGHT MARGINS, and LEFT/RIGHT FULL MARGINS. I could then tell the page generator to add a TITLE BAR, STATUS BAR, MENUS, TABLE, FORMS,..., to specific cells. Each of these Cells could then be split into Rows and Columns to any depth. Page layout takes seconds for any style.
BODY contains a Table of my Employees
HEADER contains a Title Bar caption 'Hello World' with login to Collins Software
A Menu DSL don't fit the page layout DSL, so I built a unique DSL for menus.
Resource My Main Menu
*define:menu,m,Level,Label,Icon,Action;
m,0,file;
m,1,open,open.gif,Dialog Open File;
Each problem is unique, the computer can use any format, it is the human that DSLs are designed for, so make it understandable by humans, something they can type in, and make the language out of real words; for it is real people, places, and things that we are describing.
我认为这是一种适合解决特定领域问题的语言。 它可以是某种规则处理语言或服务描述语言。
与领域特定语言 (DSL) 相对的是通用语言。
I think it's a language suited to solve problems for a specific domain. It could be some rule-processing language or service description language.
An opposite to a domain specific language (DSL) is a general-purpose language.
出色地! 上面解释了很多事情。 我将尝试以更简单的方式解释这一点,因为像我这样的人会理解的。
由于通用语言有广泛的用途,DSL 只针对特定领域。 就像 HTML 或 CSS 一样。
您可以说,如果您在一篇论文中写了只有某人或您唯一最好的朋友可以理解而其他人无法理解的说明。 那么它可能是DSL。 但是,如果您用许多人可以理解并遵循的术语编写指令,那么这就不是 DSL。
我曾经为用户创建了一个可以通过计算机的串行端口进行操作的开关板,并且用户想要一个可以在该板上执行的程序,并且继电器开关将相应地打开和关闭。 所以我写了一些说明并告诉用户根据这些说明对该板进行编程。 这是 DSL 的一个例子。 我没有发明一种新的语言,而是创建了一堆字符串,微控制器可以从 EEPROM 中读取这些字符串,并可以相应地进行解析并执行特定的任务。
Well! there is lot of things explained above. I will try to explain this in much simpler way as some one like me come will understand.
As general purpose languages are used for vast purposes the DSL is only made for specific domain. Like HTML or CSS.
You can say if you wrote instructions to a paper which only some person or your only best friend can understand and no one else could. Then it may be a DSL. But if you wrote instruction in such terms which many people could understand and could follow then this is not DSL.
I onces created a Switch board for user which could be operated via Serial port of a computer and User want a program for that board which could be executed on that board and Relay switches will be turned on and off accordingly. So I wrote some instructions and told the user to program that board according to these instructions. This is an example of DSL. I did not invented a new Language rather then I just created bunch of Strings that micro controller could read from EEPROM and could parse accordingly and could perform a specific task.
DSL 是开发供非程序员使用的语言的好方法。 例如,如果您为公司的财务人员提供了 DSL,那么您可以让他们编写他们想要完成的程序,而不是按照他们的规范进行编程。 然后,如果速度太慢,那么您可以采用他们编写的按他们想要的方式工作的内容,用编译语言编写它以加快速度。
A DSL is a good way to develop a language to be used by non-programmers. For example, if you have a DSL for the finance people in a company, then rather than programming to their specification you can just let them write the program they want done. Then, if it is too slow then you can take what they wrote that works as they want, write it in a compiled language to speed it up.
我写了一篇简短的博客文章,讨论为什么我喜欢使用 DSL:
我希望我们使用领域特定语言 (DSL) 更多
在其中,我将 DSL 定义为:
在使用方面,如果您曾经使用过 Ant、结构化查询语言 (SQL) 或级联样式表 (CSS),那么您就使用过 DSL。
我喜欢使用 DSL,因为它们专注于促进特定问题空间解决方案的交流,并且以促进领域专家参与的方式实现这一点。
I've written a brief blog post discussing why I like using DSLs:
I Wish We Used Domain Specific Languages (DSLs) More
In it, I define a DSL as:
In terms of use, if you've ever used Ant, Structured Query Language (SQL), or Cascading Style Sheets (CSS), you've used a DSL.
I like using DSLs because they focus on facilitating the communication of solutions to specific problem spaces, and they do so in a way that promotes the inclusion of domain experts.
python 中的 patsy 是机器学习中使用的 DSL 的一个示例:
https://patsy.readthedocs.io/en/latest/formulas.html#< /a>
它基于 R 中的 DSL 公式:
https://stat.ethz.ch/ R-manual/R-devel/library/stats/html/formula.html
https://cran.r-project.org/web/packages/Formula/vignettes/Formula.pdf
并且 Hadley 在他的高级 R 书中有一个很好的部分,描述了如何构建带 R 的 DSL:
http://adv-r.had.co.nz/dsl.html
一旦深度学习领域稳定下来(甚至现在),我很乐意看到 Apache MXnet 项目中出现类似的情况。 但是,我还没有在提案页面上看到任何相关提案 不过。
An example of a DSL used in Machine Learning is patsy in python:
https://patsy.readthedocs.io/en/latest/formulas.html#
which is based off the formula DSL from R:
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/formula.html
https://cran.r-project.org/web/packages/Formula/vignettes/Formula.pdf
and Hadley has a nice section of his advanced R book that describes how to build a DSL w/R:
http://adv-r.had.co.nz/dsl.html
Once the deep learning field stabilizes somewhat (or even now) I'd love to see something similar arise inside Apache MXnet project. However, I haven't seen any proposal for that on the proposal page yet though.
领域特定语言 (DSL) 的一个简单示例是 HTML,它用于称为基于 Web 的应用程序的特定领域。
One simple example for Domain Specific Language(DSL) is HTML which is used for the particular domain called web-based applications.
我最近刚刚听说 DSL,但发现了一个非常有用的示例:LUNA(以前的 lunascript)。
它是 Asana 团队为他们自己的平台定制的编程语言/框架。
我进一步发现,许多公司都制作自己的框架和语言,以创造适当的竞争优势,一些例子是:
Apple 与 Objective-C这些是特定于领域的,因为您几乎专门将它们用于在这些平台上工作。
我希望这个答案可以帮助您澄清这个概念。
I've just recently heard DSL but find a really helpful example: LUNA (former lunascript).
It's a custom-made programming language/framework made by Asana team for their own platform.
As I further find, many companies make their own frameworks and languages in order to create a proper competitive advantage, some examples are:
Apple with Objective-CThose are domain specific because you'll use them almost exclusively for working on these platforms.
I hope this answer helps you clarify on the concept.
DSL——领域特定语言。 让我们开始什么是域——域是一些定义的区域、范围。 这个域可以是网站外观,您可以为其提供CSS,第二个域可以是网站结构,在这里您可以拥有HTML。
但是,域也可以是公司 X 应用程序。 在这个领域的范围内可以创建一些语言。 语言并不意味着具有自己的语法、语法、编译器或运行时的完全风味的东西。 DSL 可以只是解决领域问题的工具列表。
让我们考虑 OOP 及其模型,通过类和方法将领域对象表示为这些对象的行为。 如果我们创建这样的结构,并赋予这些对象行为,那么我们就可以使用这些概念编写代码。 考虑这个伪代码示例:
上面有多少是 GPL(通用目的语言),有多少是特定领域术语和工具。 这是两者的混合,但所有命令都是特定于域的。 也就是说,我们可以说上面是用 DSL 编写的,其中域是某个应用程序 x。
继续这个例子,我可以创建更多的抽象工具,并且主要通过这些工具执行控制流,考虑(这更多的是 FP,但希望你明白我的意思):
正如你所看到的,我能够抽象出相当多的内容。并使用此抽象来执行控制流。 一切都基于 GPL 并在 GPL 范围内运行。
为了揭示真相,我们都编写 DSL,您可以使用的每个特定于领域的抽象都是这样的。 但大多数这些抽象都不是完整的解决方案,这就是为什么我们不太经常使用这个词的原因。 但如果你有一组工具、功能来抽象你的领域,它们就形成了某种 DSL。
DSL 是什么,很多东西,例如任何提供规则集的框架都是 DSL。 如果你看到有人声称他是 React 开发人员,那么你就知道他是领域特定开发人员,因为 React 正是 DSL,它是使用本机 Web 平台的替代方案。 如果您可以从现有的特定领域工具中组合功能,那么您正在使用 DSL 进行编写。 更深入地了解 React,抱歉所有没有使用此 DSL 的人:D,您可以创建一组组件,并将它们组成构建块,万岁!现在您在 DSL 之上创建了 DSL。
是的,抱歉,这里重复了 DSL 太多次。
DSL - domain specific language. Let's start what is domain - domain is some defined area, scope. This domain can be web site look, and you have for it CSS and second domain can be web site structure, and here you have HTML.
But, domain can be also Company X Application. And in scope of this domain some language can be created. Language does not mean - fully flavored thing with own gramma, syntax, compilator or runtime. DSL can be just a list of tools which solve domain problems.
Lets consider OOP and its model to represent domain objects by classes and methods as those objects behaviors. If we create such structure, and give these objects behaviors, then we can write a code using those concepts. Consider this pseudo code example:
How much from above is GPL (general purpose language) and how much is specific domain terminology and tools. This is a mix of two, but all commands are here domain specific. That said we can say that above is written in DSL where the domain is some application x.
Going forward with this example I can create even more abstract tools, and mostly perform the control flow by those tools, consider (this is more FP, but hope you get what I mean):
As you can see I was able to abstract quite a lot and use this abstraction to perform the control flow. And everything is based on GPL and works in scope of GPL.
To reveal the truth, we all write DSL, every domain specific abstraction which u can use is kind that. But most of those abstraction are not complete solutions, that is why we tent not to use this word too often. But if you have set of tools, functions which abstract your domain, they form kinda DSL.
What is also DSL, many things, for example any framework which provides set of rules is DSL. If you see somebody is claiming he is React developer, then you know he is Domain Specific Developer, as React is exactly DSL which is alternative for using native web platform. If you can compose functionality from existing domain specific tools then you are writing using DSL. Going deeper in React, sorry all folks not from this DSL :D, you can create set of components, and compose them as building blocks, and hurra!, now you made DSL on top of DSL.
Yep repeated DSL too many times here, sorry.
特定于领域的语言以直接使用特定领域的概念和逻辑的语言来表达您的领域流程和知识。
社区确实在增长,但仍达不到其他“主流”技术的水平。
大多数时候,DSL 是为了提高公司内部的生产力,因此他们将其保密并且不分享他们的结果/见解。
这是一次会议,演讲者给出了一些使用 JetBrains MPS 以及 Projectional 技术的 DSL 示例编辑:https://vimeo.com/197381453
Domain-specific languages express your domain processes and knowledge in a language that directly uses the concepts and logic from your particular field.
The community is definitely growing but still not in the level of other "mainstream" technologies.
Most of the time, DSLs are made to improve productivity inside the companies, so they keep it private and don't share their results/insights.
Here is a conference where the speaker gives some examples of DSL using JetBrains MPS, with the technology of Projectional editing: https://vimeo.com/197381453
nextflow(https://github.com/nextflow-io/nextflow
)是groovy编写的一种DSL语言;
nextflow的主要目的是编写用于分析的管道,并轻松地从笔记本电脑转换到HPC到云,并使用conda、docker、singularity重复env
nextflow(https://github.com/nextflow-io/nextflow
) is a kind of DSL language written by groovy;
the main purpose of nextflow is to write pipeline for analysis, and easily trans from laptop to HPC to cloud, and repeat env with conda,docker,singularity