>I'm going to define a parser and a printer for a simple grammar. >Is there a way to define both of them in a single construct using some >existing OCaml tool? > >For example, I have a keyword "function". The usual parser would contain >a mapping like: >"function" -> `Function >and the straightforward printer would do: >`Function -> "function" > >What is the best way to combine these definitions, so that duplication >would be minimized? Take a look at Boomerang: http://www.seas.upenn.edu/~harmony/ >From the overview: Boomerang is a programming language for writing lenses—well-behaved bidirectional transformations—that operate on ad-hoc, textual data formats. Every lens program, when read from left to right, describes a function that maps an input to an output; when read from right to left, the very same program describes a "backwards" function that maps a modified output, together with the original input, back to a modified input. Lenses have been used to solve problems across a wide range of areas in computing including: [...] in parsers and pretty printers