[Haskell-cafe] Parsers are monadic?

Claus Reinke claus.reinke at talk21.com
Thu Jul 5 07:02:53 EDT 2007

>>> (b) i like my combinator grammars to be reversible, so that a single
>>>     grammar specification can be used for both parsing and
>>>     unparsing/pretty-printing.  that means i have to define the
>>>     details myself anyway.

> the latest such experiment is not necessarily the simplest variant, 

for instance, it explicitly gives semantic functions and their inverses,
which i've found to be useable compromise in practice. but if you 
apply a bit of generic programming to associate an ast node with
its constructor and children (or if you use an "untyped" representation
of the ast, like 'data AST = Node ConstructorTag [AST]'), with
constructor tags that can be compared for equality, you can avoid 
even that bit of duplication.

also, i should have mentioned that the same idea, of using a single
grammar specification in parse or unparse mode, opens up a 
variety of other application possibilities. i already touched upon
syntax-directed editing (use a zipper combined with i/o to allow
interactive navigation of the ast; associate each type of ast node
with its grammar rule; unparse the current node with its rule to
display it, let the user edit, then parse the input with the rule for
the current node, and continue navigation). 

another interesting option is to use combinator grammars to 
specify dialogue protocols: if one annotates the positions in 
grammar rules where activity switches between dialogue 
partners, such as server and client communicating according 
to some protocol, then both server and client can run the same 
grammar code, in complementary modes, using the switch
points to move from producing to expecting dialogue, and
vice versa. i've attached a silly little example, using which:

    $ runhaskell Dialogue.hs server
    HTTP/1.1 200
    Content-Length: 15
    Content-Type: text/plain
    $ runhaskell Dialogue.hs server | runhaskell.exe Dialogue.hs  client

as i said, many opportunities, and i've sometimes wondered
why noone else seems to use this technique. perhaps i'll get
around to writing it up some day, after all these years.. 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Dialogue.hs
Type: application/octet-stream
Size: 1148 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070705/0e0634d1/Dialogue.obj

More information about the Haskell-Cafe mailing list