caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Camlp4 with different backend
@ 2011-03-31 16:14 Jean-Baptiste Jeannin
  2011-03-31 19:58 ` Guillaume Yziquel
  2011-03-31 21:44 ` Gabriel Scherer
  0 siblings, 2 replies; 3+ messages in thread
From: Jean-Baptiste Jeannin @ 2011-03-31 16:14 UTC (permalink / raw)
  To: caml-list; +Cc: Laurie Goffinon, Dexter Kozen

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Camlp4 with different backend
  2011-03-31 16:14 [Caml-list] Camlp4 with different backend Jean-Baptiste Jeannin
@ 2011-03-31 19:58 ` Guillaume Yziquel
  2011-03-31 21:44 ` Gabriel Scherer
  1 sibling, 0 replies; 3+ messages in thread
From: Guillaume Yziquel @ 2011-03-31 19:58 UTC (permalink / raw)
  To: Jean-Baptiste Jeannin; +Cc: caml-list, Laurie Goffinon, Dexter Kozen

Le Thursday 31 Mar 2011 à 12:14:07 (-0400), Jean-Baptiste Jeannin a écrit :
> 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

Not sure I fully understand, but if you want to take your own source
code and generate an OCaml AST and then OCaml code, you can use
printers.

yziquel@seldon:~/git/ocaml/camlp4/Camlp4Printers$ ls
Camlp4AstDumper.ml    Camlp4NullDumper.ml      Camlp4OCamlPrinter.ml
Camlp4AutoPrinter.ml  Camlp4OCamlAstDumper.ml  Camlp4OCamlRevisedPrinter.ml

Off the top of my head:

Camlp4OCamlPrinter generates standard OCaml code.
Camlp4OCamlOCamlRevisedPrinter generates OCaml code in the revised
syntax.
Camlp4OCamlAstDumper dumps the Ast in a not-so-readable format.
Camlp4AutoPrinter behaves like Camlp4OCamlPrinter when the standard
output is a tty, and as Camlp4OCamlAstDumper otherwise. It's the default
if I'm not mistaken.

That may help to replace OCaml's backend with your own interpreter.

-- 
     Guillaume Yziquel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Camlp4 with different backend
  2011-03-31 16:14 [Caml-list] Camlp4 with different backend Jean-Baptiste Jeannin
  2011-03-31 19:58 ` Guillaume Yziquel
@ 2011-03-31 21:44 ` Gabriel Scherer
  1 sibling, 0 replies; 3+ messages in thread
From: Gabriel Scherer @ 2011-03-31 21:44 UTC (permalink / raw)
  To: Jean-Baptiste Jeannin; +Cc: caml-list, Laurie Goffinon, Dexter Kozen

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-03-31 21:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-31 16:14 [Caml-list] Camlp4 with different backend Jean-Baptiste Jeannin
2011-03-31 19:58 ` Guillaume Yziquel
2011-03-31 21:44 ` Gabriel Scherer

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