The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Domain specific language vs XML

When would you want to implement a domain specific language  instead of just coding up a format in XML?

I am reading through "The Pragmatic Programmer", and in the section on domain languages, they give this example:

From X25LINE1 (Format=ABC123) {
  Put TELSTAR1 (Format=XYZ43B);
  Store DB;
}

and I was immediately struck by how similar it looked to an ANT task.  It could be rewritten

<FROM src="X25LINE1" format="ABC123">
  <PUT dst="TELSTAR1" format="XYZ243B" />
  <STORE dst="DB" />
</FROM>

Obviously a simple example.  Given the ubiquity of XML parsers and libraries, at what point does it make sense to developer a domain language instead?
Bill Keller Send private email
Saturday, August 19, 2006
 
 
None that I can tell. Unless like in ruby you can fold the DSL into code.

I've been hoping for better automatic GUI editoris based on reading in XML schemas. Then people wouldn't have to touch the XML at all. Looking at any large configuration file can make one weak in the editor.
son of parnas
Saturday, August 19, 2006
 
 
The purpose of a domain-specific language is usually as a shorthand -- with all the implicit forms/recursive-structures that implies.  The purpose of XML in this context is usually as an unambiguous instantiation of the grammar of your domain-specific language.  Because it's unambiguous it can be easily compiled to basic instructions for your target machine (but it's not generally the sort of thing that people would want to type regularly because it's so verbose).

Here's an example ...

Your grammar:

E  ::= "(" E ")" | E op E | val;
val ::= '[0-9]+';
op  ::= "+" | "-" | "*" | "/";

Your expression:

(5 + 3) * 10

Your syntax tree:

<E><E><E><val>5</val></E><op>+</op><E><val>3</val></E></E><op>*</op><E><val>10</val></E></E>

Your compiled instruction list:

push 10
push 3
push 5
push +
call
push *
call
Kalani Send private email
Saturday, August 19, 2006
 
 
Your XML is also an example of a DSL, having the same semantics as the one above, but a regularised (XML) syntax.

In any language, it's the semantics that matter most. Take, as an example, Asterisk's DSL ; An equivalent XML could be formulated, but it's easier to use as-is <http://www.onlamp.com/pub/a/onlamp/2003/07/03/asterisk.html> It might look a little terse, but getting used to it takes minutes, and it's extremely usable. An XML version of the same would probably be ten times as verbose without being more readable or providing more functionality.

Personally, I feel (to quote a colleague) that XML combines the inefficiencies of text formats with the ease of binary formats.
Ori Berger
Saturday, August 19, 2006
 
 
This is a topic that's very close to my heart. Over the last several months, I've been working on a domain specific language (and its compiler) for my company.

When I first start explaining it to people, they give me a crazy look and ask "why didn't you just use XML?

And I explain to them that XML is not very practical if you expect human beings to author the code by hand. As a Java programmer, would you want to write this code:

<for>
  <initialization>
    <variable name="i" value="0"/>
  </initialization>
  <condition>
    <variable name="i"/>
    <operator type="LESS_THAN"/>
    <literal type="int" value="10/>
  </condition>
  <incrementation>
    <variable name="i">
    <operator type="PLUS_PLUS">
  </incrementation>
  <codeblock>
    <invoke methodname="doSomething">
      <arguments>
        <argument type="void"/>
      </arguments>
    </invoke>
  </codeblock>
</for>

...when you know you could write this code instead?

for (int i = 0; i < 10; i++) {
  doSomething();
}

Sure, it's always *possible* to represent any structured language as XML. But it's not always the most elegant syntax.

In my DSL, since I'm in complete control of the language and parser, I've added abstract data types and inheritance. Plus, the language has built-in semantics for package & import declarations. So it's easy for a code author to logically organize a project into hundreds of different files.

I could have accomplished the same thing with XML, but I think the DSL is better.

XML is really well suited for object serialization across heterogenous architectures. But beyond that, I don't think it's the right way of doing things.
BenjiSmith Send private email
Saturday, August 19, 2006
 
 
A DSL isn't for programming. It's for specification and then letting backend code generators do the the work.

But your example could be a lot cleaner:

<for var="i" val="1">
  <if var="i" op="<" val="10">
    <plus var="i" val="1"/>
    <call methodname="doSomething"/>
  </if>
</for>

Trying to maintain types like int and void seems overkill.
son of parnas
Saturday, August 19, 2006
 
 
> Asterisk's DSL

It's fine until you get to aggregates.

This: exten => 2000,102,Voicemail(b2000)

Isn't very clear and doesn't allow for any substructure. I've spent enough time pooring through all these "intuitive" configuration files to appreciate consistency. I would still prefer an editor though.
son of parnas
Saturday, August 19, 2006
 
 
Benji - Can you post a short snippet of your DSL?
Bill Keller Send private email
Saturday, August 19, 2006
 
 
"When would you want to implement a domain specific language?"-- When the code will be longer than a few lines. XML is not good to read or write as a programming language, it is better to look at as a data format (generating and processing it programmatically).
Ben Bryant
Saturday, August 19, 2006
 
 
In general, my position on this question rest in the question: "what programming language are you using?"  If you are using one that is easily mutated to support your DSL, then using an "internal" DSL is preferable.  If you are not working in a language that has that flexibility (Java, C#, etc.), then it might be easier to use XML.  I recently used XML in a project in this manner both because I could change it without recompiling the app code, and because the app was written in C#.  I would consider this particular usage to be somewhere between a DSL and a configuration file (because it could be altered on the fly).
Joshua Volz Send private email
Sunday, August 20, 2006
 
 
I've said this elsewhere, and I'll say it again. XML is a metalanguage, not a language in and of itself. To say that you are using XML is really meaningless. You need to instantiate XML to create an XML language.

Therefore, the OP's question should really be "Domain specific non-XML language vs. Domain specific XML language".

For my 2 cents, markup languages are good at marking up text, not much else, really.
Steve Hirsch Send private email
Sunday, August 20, 2006
 
 
Bill Keller:
"Benji - Can you post a short snippet of your DSL?"

Probably not. It's not something we're making commercially available right now. It's just for internal development (for our professional services department and our automated QA).
BenjiSmith Send private email
Monday, August 21, 2006
 
 
Thanks all for the replies!  My day job consists of working on software to manage different educational events, and has become fairly brittle over the years when trying to adapt to different conditions at different times.  Right now, all of the configuration information is stored in a database, but as the "types" of configuration change, the ability to manage that change without extending the schema and supporting code decreases.  So it's not an entirely rhetorical question.  I would like to build a flexible language (whether XML based or not) that can be easily extended as the need arises.  Either that,
or embed an existing language.  Maybe Python or Lua.

Personally, I think that the ANT configuration syntax is ugly.  XML seems to be best suited for cases where the language elements are mostly declarative.  As soon as you start adding imperetive elements, like the for loop that was mentioned, it starts to just look wrong.  And as many have said, once you get above a certain size, XML becomes a hinderance.
Bill Keller Send private email
Monday, August 21, 2006
 
 
There's one advantage of XML versus plaintext grammar + roll-your-own parser: well-formed XML documents impose a natural, easy to parse structure of the syntax, even if the grammar designer is clueless at computer science languages and parsers. On the other hand, using plaintext grammar, one need to be careful to maintain it easily parsable.

Thus, if you predict that the language will change often, consider using XML. That's why XML is so useful for storing "business" application data (schema requirements are subject to changes)

On the other hand, consider XML Schema. This is a W3C recommendation schema language that was originally written in XML syntax, and once published it haven't changed. This was bloated, and compact syntax for XML schema were reinvented.
Curious Implement
Monday, August 28, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz