From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p2VLjKIg000333 for ; Thu, 31 Mar 2011 23:45:20 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqoCAJb1lE3RVdg2kGdsb2JhbACYJTKGBoYZWAgUAQEBAQkJDQcUBCGIeRucOopWgiOFDC+IXAEBAwWFZgSBe4sWiRE6gxQ X-IronPort-AV: E=Sophos;i="4.63,278,1299452400"; d="scan'208";a="104170982" Received: from mail-qw0-f54.google.com ([209.85.216.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 31 Mar 2011 23:45:14 +0200 Received: by mail-qw0-f54.google.com with SMTP id 9so2976563qwc.27 for ; Thu, 31 Mar 2011 14:45:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=UVyxIbyElhMwUmABz4FiI+9NVntWL2VETWMZhBlVUww=; b=EcN4Z2OygjkootgTveNQPXPqHKcMZYhBdgRUP1DHMfVMYkzi/Ufyn3VKQVxCpaxGoZ 3Gr5WMW1fuAn0+orZj+FJJ847f+H5U/fWHNvL9OShiXHTpvEMD3LJGmLFLUxmivdQWdm Z31/WoSfpIRufstXVX7HNPF1bnNrMfRY61RLc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=dBqq3XijV2I7JcCcsM5uTwBR7H1jBKE464L1n3AWspoAnBolmHYMqHgfK+LzSCI8xS XVMmeSOmRe0X4lTp9Zno/dFc7eL65QAhOrLdvSTpV7uC8ydl8pUeXvMLOe0U1SVzth6c XucxazkZ5wbu6i5lKTxCdk16g5AsNmRhIfBUw= Received: by 10.229.75.141 with SMTP id y13mr2796141qcj.15.1301607914222; Thu, 31 Mar 2011 14:45:14 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.235.146 with HTTP; Thu, 31 Mar 2011 14:44:54 -0700 (PDT) In-Reply-To: <4D94A84F.9010108@cs.cornell.edu> References: <4D94A84F.9010108@cs.cornell.edu> From: Gabriel Scherer Date: Thu, 31 Mar 2011 23:44:54 +0200 Message-ID: To: Jean-Baptiste Jeannin Cc: caml-list@inria.fr, Laurie Goffinon , Dexter Kozen Content-Type: multipart/alternative; boundary=00235429cb345d347f049fce356c Subject: Re: [Caml-list] Camlp4 with different backend --00235429cb345d347f049fce356c Content-Type: text/plain; charset=ISO-8859-1 Camlp4 provides different things : 1. a general library to implement grammars and grammar extensions/modifications 2. parsers, using that library, for the OCaml syntax (actually, syntaxes) 3. plumbing to get the parsed OCaml AST to the various OCaml compiler tools 4. tools to meta-generate useful transformation passes on big algebraic datatypes (used in the lib on OCaml ASTs, can be used to implement further syntactic transformation passes, or on your own AST for another language/structure) You can reuse 1. and 4. to write parsers and transformation on any language you would like to parse, not just OCaml. There is a small example of such use on the Camlp4 wiki : http://brion.inria.fr/gallium/index.php/Full_parser_tutorial You may also be interested in Jake Donham's post on using Camlp4 for parsing: http://ambassadortothecomputers.blogspot.com/2010/05/reading-camlp4-part-6-parsing.html If you want to see a bigger example, you can look into the Camlp4 distribution for the OCaml parser implemented using Camlp4 facilities : it's in Camlp4Parsers/Camlp4OCamlRevisedParser.ml (it's a parser for the revised syntax; the standard syntax is defined on top of it, as a syntax extension) I have myself used Camlp4 tools for parsing in the past and have found them quite satisfying. Once you're used to all those uppercase, the parser code is actually rather clean and readable. On my use cases, the resulting parser was shorter than the corresponding code using a combination of ocamllex + ocamlyacc / menhir. It is also much nicer than using stream parsers directly, as it provides facilities that are more powerful than recursive descent without lookahead. The problem with Camlp4 is that its parsing behavior is not as clearly defined (it mostly behaves as a recursive descent parser, except on the nice macros that you can use, or when it does automagic factorisation, but you're not exactly sure what the differences are) and for that reason it's quite hard to debug. That said, yacc parsers also are a pain to debug (though menhir is much nicer in that regard). I have no experience with fancier GLR parsers such as dypgen. I also quite like the Camlp4MapGenerator / Camlp4FoldGenerator. They make simple transforms on big ASTs very simple to use, using object-oriented programing, the visitor pattern and nice default behavior. That's actually my top one example of usefulness of OOP's implementation inheritance. Finally, Camlp4 quotation system is also very nice, but it's more aimed at integrating other language/syntaxes inside an OCaml code. On Thu, Mar 31, 2011 at 6:14 PM, Jean-Baptiste Jeannin < jeannin@cs.cornell.edu> wrote: > Hello, > > We are trying to use camlp4 to replace the front-end (lexer / parser up to > building the Abstract Syntax Tree) of an experimental interpreter for a > language close to OCaml. However we would like to keep our backend (the > interpreter itself), as it behaves very differently from a normal > interpreter, and this behavior cannot be translated into OCaml's code easily > (or even at all). > > However it seems that using camlp4, we can only use OCaml's own backend. In > the camlp4 documentation, I could not find any way of getting the abstract > syntax tree to feed it to another interpreter. Is this true or could camlp4 > serve our purpose? > > Thank you, > > Jean-Baptiste Jeannin > CS department, Cornell University > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --00235429cb345d347f049fce356c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Camlp4 provides different things :
1. a general library to implement gra= mmars and grammar extensions/modifications
2. parsers, using that librar= y, for the OCaml syntax (actually, syntaxes)
3. plumbing to get the pars= ed OCaml AST to the various OCaml compiler tools
4. tools to meta-generate useful transformation passes on big algebraic dat= atypes (used in the lib on OCaml ASTs, can be used to implement further syn= tactic transformation passes, or on your own AST for another language/struc= ture)

You can reuse 1. and 4. to write parsers and transformation on any lang= uage you would like to parse, not just OCaml. There is a small example of s= uch use on the Camlp4 wiki :
=A0 http://brion.inria.fr/gallium/index.php/F= ull_parser_tutorial
You may also be interested in Jake Donham's post on using Camlp4 for pa= rsing:
=A0 http://ambassadortothecomputers.blog= spot.com/2010/05/reading-camlp4-part-6-parsing.html

If you want to see a bigger example, you can look into the Camlp4 distr= ibution for the OCaml parser implemented using Camlp4 facilities : it's= in
=A0 Camlp4Parsers/Camlp4OCamlRevisedParser.ml (it's a parser for= the revised syntax; the standard syntax is defined on top of it, as a synt= ax extension)

I have myself used Camlp4 tools for parsing in the past and have found = them quite satisfying. Once you're used to all those uppercase, the par= ser code is actually rather clean and readable. On my use cases, the result= ing parser was shorter than the corresponding code using a combination of o= camllex + ocamlyacc / menhir. It is also much nicer than using stream parse= rs directly, as it provides facilities that are more powerful than recursiv= e descent without lookahead. The problem with Camlp4 is that its parsing be= havior is not as clearly defined (it mostly behaves as a recursive descent = parser, except on the nice macros that you can use, or when it does automag= ic factorisation, but you're not exactly sure what the differences are)= and for that reason it's quite hard to debug. That said, yacc parsers = also are a pain to debug (though menhir is much nicer in that regard). I ha= ve no experience with fancier GLR parsers such as dypgen.

I also quite like the Camlp4MapGenerator / Camlp4FoldGenerator. They ma= ke simple transforms on big ASTs very simple to use, using object-oriented = programing, the visitor pattern and nice default behavior. That's actua= lly my top one example of usefulness of OOP's implementation inheritanc= e. Finally, Camlp4 quotation system is also very nice, but it's more ai= med at integrating other language/syntaxes inside an OCaml code.


On Thu, Mar 31, 2011 at 6:14 PM, Jean-Ba= ptiste Jeannin <jeannin@cs.cornell.edu> wrote:
Hello,

We are trying to use camlp4 to replace the front-end (lexer / parser up to = building the Abstract Syntax Tree) of an experimental interpreter for a lan= guage close to OCaml. However we would like to keep our backend (the interp= reter itself), as it behaves very differently from a normal interpreter, an= d this behavior cannot be translated into OCaml's code easily (or even = at all).

However it seems that using camlp4, we can only use OCaml's own backend= . In the camlp4 documentation, I could not find any way of getting the abst= ract syntax tree to feed it to another interpreter. Is this true or could c= amlp4 serve our purpose?

Thank you,

Jean-Baptiste Jeannin
CS department, Cornell University

--
Caml-list mailing list. =A0Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


--00235429cb345d347f049fce356c--