
Writing a DSL with Python - jgalvez
http://blog.fmeyer.org/entry/writing_a_domain_specific_language_dsl_with_python
======
ramanujan
I would recommend pyparsing for this. There was an excellent article on it in
Python Magazine a few months ago.

Some links:

1\. O'Reilly mini-book on pyparsing (probably best single ref)
<http://oreilly.com/catalog/9780596514235/>

2\. Quick example of pyparsing <http://eikke.com/pyparsing-introduction-bnf-
to-code/>

3\. Main pyparsing page (i know it's _not_ very aesthetically pleasing. The
code is much better, I promise) <http://pyparsing.wikispaces.com/>

4\. Links to more on pyparsing here
<http://pyparsing.wikispaces.com/Publications>

Python Magazine is excellent, by the way. Definitely worth the purchase price
as they include a zip file with code. (Mandatory disclaimer: I have no stake
in it at all. Just found it very useful, that's all).

------
extension
Note that there is a world of difference between an in-language DSL and one
with its own syntax that must be parsed, so much difference that they really
should have different names. The former can effortlessly exchange data with
and enjoy all the features of the host language. I'm convinced that the latter
will one day be an artifact of less enlightened times.

~~~
tomlu709
By convention the in-language form is called an internal DSL, the other an
external ditto.

~~~
eru
Or embedded.

~~~
draegtun
or Pidgin ;-)

ref: <http://www.bofh.org.uk/2007/08/08/domain-specific-pidgin>

------
spahl
A very interesting DSL for network packet manipulation is
Scapy(<http://www.secdev.org/projects/scapy/>).

It uses python itself as a DSL to make things like packet forging easy:

    
    
      >>> Ether()/IP()/TCP()/"GET / HTTP/1.0\r\n\r\n"
      <Ether type=0x800 |IP frag=0 proto=TCP |<TCP |<Raw load='GET / HTTP/1.0\r\n\r\n' |>>>>
    

Here the / operator was overloaded to stack layers.

------
hedgehog
Wow. If you ever wanted curly braces or multi-line lambdas in Python this
looks like the way to do it.

~~~
pavelludiq
What exactly would people do with multi-line lambdas? I would definitely
refactor any lambda with statements or more than one expression in it, give it
a name, or use a list comprehension, so I've never seen a problem with them.
Can anybody give me an example of a piece of code they think would look better
if python had multi-line lambdas?

~~~
rw
If you give something a name, yet still only use it once, what's the point? It
is just semantic clutter to do that (unless, of course, you can't use
multiline lambdas...).

~~~
CaptSolo
There is no big harm in giving something a name. Giving a name also helps if
you find a need to reuse it later. Plus a name can give a hint to what this
code is for.

~~~
rw
Yet, if you look in functional programming code (e.g. Haskell, Scheme), you'll
find many anonymous lambdas (that in python would be > 1 line). Why would good
coders bother doing that?

~~~
eru
If something is small and enough to be anonymous, it's usually simply enough
to be expressed without binding a variable.

(See point-free style.)

~~~
rw
Obviously. Show us how to do point-free style in Python. We're talking about
Python's lame implementation of anonymous functions, not something that it
doesn't have any implementation of.

~~~
eru
Oh, I thought you were talking about programmers' habits in Haskell.

------
srwh
antlr, antlr, antlr

~~~
n8agrin
Maybe provide a link next time?

<http://www.antlr.org/>

