From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id A9FC9BBAF for ; Thu, 9 Dec 2010 12:29:52 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkEBANJLAE2Brw8EmWdsb2JhbACDWJF0jiQVAQEBAQEICwoHESmvOpBlgSGDNXQEjXwM X-IronPort-AV: E=Sophos;i="4.59,319,1288566000"; d="scan'208";a="91892416" Received: from ext.lri.fr ([129.175.15.4]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 09 Dec 2010 12:29:52 +0100 Received: from localhost (localhost [127.0.0.1]) by ext.lri.fr (Postfix) with ESMTP id 6D9014036A for ; Thu, 9 Dec 2010 12:29:52 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lri.fr Received: from ext.lri.fr ([127.0.0.1]) by localhost (ext.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DC9cqyrbIRbz for ; Thu, 9 Dec 2010 12:29:52 +0100 (CET) Received: from smtp-ng.lri.fr (smtp-ng [129.175.3.73]) by ext.lri.fr (Postfix) with ESMTP id 4910F40252 for ; Thu, 9 Dec 2010 12:29:52 +0100 (CET) Received: from [129.175.4.238] (lri4-238 [129.175.4.238]) by smtp-ng.lri.fr (Postfix) with ESMTP id DA8BD12001F for ; Thu, 9 Dec 2010 12:27:00 +0100 (CET) Message-ID: <4D00BCB1.5030902@lri.fr> Date: Thu, 09 Dec 2010 12:25:37 +0100 From: Romain Bardou User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.11) Gecko/20100805 Icedove/3.0.6 MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Define parser and printer consistently References: <4D005F51.8070701@wp.pl> In-Reply-To: <4D005F51.8070701@wp.pl> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; lri:01 parser:01 parser:01 ocaml:01 minimized:01 infers:01 ocamlyacc:01 ocamllex:01 lexer:01 ocaml:01 syntax:01 syntax:01 wrote:01 parsing:01 caml-list:01 On 09/12/2010 05:47, Dawid Toton wrote: > 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? > To be precise, avoiding duplication is not exactly what I need. I'm > looking for something that would prevent making inconsistent changes to > the parser and the printer. I'm writing a tool called Parsini which, maybe, does what you're looking for. Parsini stands either for "parsing is not interesting" (i.e.: let's have a tool which does it quickly for us and move on to interesting things such as code generation) or for "parser houdini" or something :p From a simple grammar, the tool : - infers and produces an AST ; - produces an ocamlyacc source ; - produces an ocamllex source (optional - you can use your own lexer) ; - produces a main file with : * functions to read your main entries easily from a channel, a file, a string... * functions to pretty-print your AST. Your AST is pretty-printed with the Ocaml syntax, not the syntax of your own language, which I do not know how to do. I have not released the tool yet, so nothing is official nor documented but you might want to take a look. License will be BSD. I've copied the darcs repository on my website : http://romain.bardou.fr/parsini So you should be able to download it easily with : darcs get http://romain.bardou.fr/parsini Have fun, -- Romain Bardou