From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 85431BCAE for ; Fri, 15 Jul 2005 22:24:50 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j6FKOnFT027133 for ; Fri, 15 Jul 2005 22:24:49 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA25153 for ; Fri, 15 Jul 2005 22:24:49 +0200 (MET DST) Received: from out3.smtp.messagingengine.com (out3.smtp.messagingengine.com [66.111.4.27]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j6FKOmor003084 for ; Fri, 15 Jul 2005 22:24:48 +0200 Received: from frontend3.messagingengine.com (frontend3.internal [10.202.2.152]) by frontend1.messagingengine.com (Postfix) with ESMTP id 27D73CBE9E8; Fri, 15 Jul 2005 16:24:47 -0400 (EDT) X-Sasl-enc: hxZZZY+mcTvWE3drigWtvY9tbavdqfgiwaunz8FWb7nR 1121459087 Received: from [172.16.112.115] (burnham.ljcrf.edu [192.231.106.2]) by frontend3.messagingengine.com (Postfix) with ESMTP id 8156A1F9; Fri, 15 Jul 2005 16:24:45 -0400 (EDT) Date: Fri, 15 Jul 2005 13:24:38 -0700 (PDT) From: Martin Jambon X-X-Sender: martin@localhost To: Pietro Abate Cc: caml-list@inria.fr Subject: Re: [Caml-list] camlp4 help In-Reply-To: <20050715083901.GA24763@pulp.anu.edu.au> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 42D81B91.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42D81B90.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 parser:01 parser:01 expander:01 expr:01 expr:01 plexer:01 token:01 grammar:01 pcaml:01 grammar:01 pcaml:01 hashtbl:01 clist:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.2 X-Spam-Level: On Fri, 15 Jul 2005, Pietro Abate wrote: > Hi all, > I'm having problems (once again) with camlp4... > > I'd like to parse and produce a bit of ocaml code out of the > following description : > > --------------------- > CONNECTIVES > "_&_",AssocLeft,And ; > "_v_",AssocLeft,Or > END > > TABLEAU > > RULE "And" > { A & B } > ---------- > A ; B > END > > END > --------------- > > The first time around I used quotes around the "{A & B}" and parsed the > expression with a adhoc parser outside camlp4 (and it's all good and > working). Now I want to remove the quotes tut-cur and do everything > using the pre-processor (quotes make my code quote un-readable). I've > started writing this code below, but I don't know how to proceed... > > The main problem is of course that I've to parse an expression (in rule), > with a generic parser built on the top of the connectives that I've just > declared. Is there a way to tell campl4 : pass everything in between > RULE and END to a function my_parser ? The normal way to do this would be to use a quotation, which is a lexical element (like a string constant). Your example would be written as: [...] TABLEAU <:rule< "And" { A & B } ---------- A ; B >> END So if you create a quotation expander named "rule", it will be allowed to appear where an expr or a patt is expected. You can probably find some simple trick to check that it is not used out of a TABLEAU block if necessary. Then TABLEAU will be parsed as a list of patts or a list of exprs (LIST1 expr instead of LIST1 rule in your code) and you might have to disable some of them using a secondary check, i.e. expand the quotation to some reserved expr or patt such as <:expr< Rule_quotation "text to parse" >> and check that the list you get has only items of this form. And now you have the freedom to convert the "TABLEAU" list of exprs (which for you is just a list of strings to parse) into a str_item containing whatever you like. This strategy is a bit weird, but it should work. An extreme solution would be to patch plexer.ml so that something of the form RULE ... END becomes only one token which would be equivalent to <:rule< ... >>, but modifying the camlp4 sources is probably not what you want. Martin > I was looking for inspiration in M. Jambon excellent tutorial, but > without much luck... > > thanks, > p > > -----------------------(not tested) > let rule = Grammar.Entry.create Pcaml.gram "rule";; > let connective = Grammar.Entry.create Pcaml.gram "connective";; > > type assoc = |AssocLeft |AssocRight |AssocNone ;; > > let conntable = Hashtbl.create 15;; > > EXTEND > Pcaml.str_item: [ > "CONNECTIVES"; clist = LIST1 connective SEP ";"; "END" -> > List.iter Hashtbl.add conntable clist; > <:str_item< value version = "connectives declared" >> > |"TABLEAU"; LIST1 rule; "END" -> > <:str_item< value version = "tableau declared" >> > ]; > > connective: [ > s = STRING; ","; a = UIDENT; ","; r = UIDENT -> (s,a,r) > ]; > > (* how can I write a quotation outside the EXTEND syntax ? *) > (* how does this quotation looks like ? *) > (* Quotation.add my_parser ??? *) > rule: [ > "RULE"; r = my_parser ??? "END" -> do_something r > ]; > > END > ------------------------ > > -- > ++ Blog: http://blog.rsise.anu.edu.au/?q=pietro > ++ > ++ "All great truths begin as blasphemies." -George Bernard Shaw > ++ Please avoid sending me Word or PowerPoint attachments. > See http://www.fsf.org/philosophy/no-word-attachments.html > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- Martin Jambon, PhD http://martin.jambon.free.fr