caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Jean-Baptiste Jeannin <jeannin@cs.cornell.edu>
Cc: caml-list@inria.fr, Laurie Goffinon <lag267@cornell.edu>,
	Dexter Kozen <kozen@cs.cornell.edu>
Subject: Re: [Caml-list] Camlp4 with different backend
Date: Thu, 31 Mar 2011 23:44:54 +0200	[thread overview]
Message-ID: <AANLkTinO1o2dAHL_Q63=z-=BzHx-4-OjkL2Amfpf5YqN@mail.gmail.com> (raw)
In-Reply-To: <4D94A84F.9010108@cs.cornell.edu>

[-- Attachment #1: Type: text/plain, Size: 3634 bytes --]

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
>
>

[-- Attachment #2: Type: text/html, Size: 4470 bytes --]

      parent reply	other threads:[~2011-03-31 21:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-31 16:14 Jean-Baptiste Jeannin
2011-03-31 19:58 ` Guillaume Yziquel
2011-03-31 21:44 ` Gabriel Scherer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTinO1o2dAHL_Q63=z-=BzHx-4-OjkL2Amfpf5YqN@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=jeannin@cs.cornell.edu \
    --cc=kozen@cs.cornell.edu \
    --cc=lag267@cornell.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).