湾址到芜湖汽车时刻表:Can You XML a DSL, And Would You Want To?

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 15:13:17

对于我来说,编程的原则之一始终是:“最适合的工作,使用的编程语言;;如果没有,创建一个新的语言。”我相信这是直接相关的领域特定语言的想法( DSL)现在都风靡一时。 创建一个新的语言,是最适合特定问题域:真正说,这一切,不是吗? 尽管如此,它的报价,我第一次听到我的编程生涯,这是二十年前,当我开始 - 它很可能甚至比旧。 人们怎么去创造一种语言? (我的意思是解析和编译或解释,而不是设计的语言。)我们该怎么做,今天呢? 有没有人问过的问题:

  • “什么编程语言是最适合执行另一个(DS)语言?”
  • “在什么时候将建立和维持解决问题的第二个最好的语言比使用较少的工作一个新的(DS)语言的结果?”
  • “我们能描述一种新的语言,是专门用于创建DSL的,一元的DSL?”

我是导致这些问题后,奥比费尔南德斯在他的博客阅读后,他形容他的团队如何创建一个DSL,描述了一个数据模型。 根据它的上下文,DSL代码将创建创建表的脚本,创建存储过程的脚本,在浏览器中显示或XHTML代码。 他的描述是不是所有的太复杂,我必须说,也许我错过了一些有趣的功能在这里。 但它听起来对我来说,做一些数据转换。 将XML和一些XSLT脚本做的工作呢? 另外,如果你创建了这个DSL,你必须添加的缺点,你必须保持DSL的执行情况以及(即使DSL很容易写在Ruby)。 三DSL的实现,甚至,您可以使用为每个上下文一个。 情境的想法是好的,直到你需要一个额外的功能来实现在一个背景下,将与他人发生冲突。 不幸的是,奥比从来没有得到回答这个问题,我在我的评论放在他的博客。

我相信很多XML代码写在那里实际上可以被称为DSL在自己的权利。 有人写了一天(40000博客我现在再尝试读取之一)XML是Java创建DSL的工具;我同意一些点。 中的某个位置。 我的意思是,我写XML每天(在JSP的形式),所以现在我已经习惯了写条件语句和循环中的XML标签的形式,但它的,如果你认为它难以优雅。 它实际上是丑陋。 (如果你认为这是不难看,去看看在XML编程的下一步:果冻,可执行 XML呸! )

混合数据和代码(原则上)是一件坏事,我认为,不管你的数据和代码混合(如在XML中)启动或启动创建DSL和添加数据(如在奥比的例子)。 我宁愿把XML中的数据(或YAML,或一个数据库,或一些其他形式的结构化数据),并保持代码分开。 这也会给你更好地了解您是否需要处理数据创建一个DSL,如果你可以简单地使用XSLT,。

PS:“只要使用XSLT”? 我只是写下来吗? 我回来了! 我完全可以理解如何写XSLT可以驱动任何理智的人,他自己的DSL代码。 在汇编程序中,如果需要的话!


For me, one of the principles of programming has always been: “Use the programming language best suited for the job; and if there is none, create a new language.” I believe this is directly related to the idea of Domain Specific Languages (DSL) that are all the rage right now. Creating a new language that is best suited for a specific problem domain: that really says it all, doesn’t it? Still, it’s a quote that I first heard when I started my programming career, which was about twenty years ago — and it may very well be even older than that. How did people go about creating a language back then? (I mean parsing and compiling or interpreting it, not designing the language.) And how do we do it today? Has anyone ever asked the questions:

  • “What programming language is best suited for implementing another (DS) language?”
  • “At what point will creating and maintaining a new (DS) language result in less work than using a second-best language for solving the problem?”
  • “Can we describe a new language that is meant specifically for creating DSLs, a meta-DSL?”

I was led to these questions after reading a post by Obie Fernandez on his blog. He describes how his team created a DSL that describes a data model. Depending on its context, the DSL code will either create a table creation script, a script for creating stored procedures, or XHTML code for display in a browser. His description is not all too elaborate, I must say, so maybe I’m missing some interesting feature here. But it sounded to me like nothing more than doing some data transformations. Wouldn’t XML and some XSLT scripts have done the job here? Also, if you create a DSL for this, you have the added disadvantage that you’ll have to maintain the DSL implementation as well (even if the DSL is easy to write in Ruby). Three DSL implementations even, one for each context you can use. The idea of contexts is nice, until you need to implement an extra feature in one context that will conflict with the others. Unfortunately, Obie never got round to answer this question that I put in my comment on his blog post.

I believe a lot of XML code written out there could actually be called a DSL in its own right. Someone wrote the other day (in one of the 40,000 blogs I try to read now and then) that XML is Java’s tool for creating DSLs; and I agree to some point. To some point. I mean, I write XML every day (in the form of JSPs), so by now I’m used to writing conditional statements and loops in the form of XML tags, but it’s hardly elegant if you think of it. It’s actually plain ugly. (And if you think that’s not ugly enough, go take a look at the next step in XML programming: Jelly, executable XML. Yuck!)

Mixing data and code is (in principle) a bad thing, I think; whether you start of with data and mix in code (like in XML) or you start with creating a DSL and add in data (like in Obie’s example). I would prefer to to put the data in XML (or Yaml, or a database, or some other form of structured data) and keep the code separate. This will also give you better insight whether you’ll need to create a DSL for processing the data, of if you can simply use XSLT.

P.S. “Simply use XSLT”? Did I just write that down? I take it back! I can fully understand how having to write XSLT can drive any sane person to code his own DSL. In assembler, if needs be!