Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] camlp4 question: Mixing a printer and Ast.fold

14 views
Skip to first unread message

Richard Jones

unread,
Mar 21, 2008, 11:06:51 AM3/21/08
to caml...@inria.fr
I'm trying to translate Sylvain Le Gall's gettext module to use camlp4
from ocaml 3.10.0. The module is a printer which folds over the AST
looking for certain types of function call by name. A simplified
version is shown below.

This program is supposed to look for all instances of a function named
f applied to a string.

I cannot for the life of me work out how to get this to compile. I've
tried about a dozen different variations of the module names, 'open',
'include' etc. and got a dozen different errors.

----------------------------------------------------------------------
module Id = struct
let name = "pr_gettext"
let version = "$Id$"
end

module Make (Syntax : Camlp4.Sig.Syntax)
: Camlp4.Sig.Printer(Syntax.Ast).S =
struct
module Loc = Syntax.Loc
module Ast = Syntax.Ast

class visitor = object
inherit Ast.fold as super

val t = []
method t = t

method expr = function
| <:expr@loc< f $str:singular$ >> ->
let t = str :: t in
{< t = t >}

| e -> super#expr e
end

let print_interf ?input_file ?output_file _ = ()

let print_implem ?input_file ?output_file ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
List.iter prerr_endline t
end

(* Register the new printer. *)
module M = Camlp4.Register.Printer(Id)(Make)
----------------------------------------------------------------------

ocamlc -I +camlp4 -I /usr/lib64/ocaml/camlp4/Camlp4Parsers -pp camlp4of.opt camlp4lib.cma test.ml -o test.cmo
File "test.ml", line 19, characters 19-19:
Unbound constructor Ast.ExApp

Please let me know how to compile this before I go mad...

Rich.

--
Richard Jones
Red Hat

_______________________________________________
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

Jeremy Yallop

unread,
Mar 21, 2008, 11:44:14 AM3/21/08
to Richard Jones, caml...@inria.fr
Richard Jones wrote:
> I cannot for the life of me work out how to get this to compile. I've
> tried about a dozen different variations of the module names, 'open',
> 'include' etc. and got a dozen different errors.

Here's another variation that compiles and seems to work. I changed the
following:

* the argument type to Make from "Syntax" to "Camlp4Syntax"
* the register functor from "Printer" to "OCamlPrinter"
* the accumulated value in "expr" from "str" to "singular".

Jeremy.

module Id = struct
let name = "pr_gettext"
let version = "$Id$"
end

module Make (Syntax : Camlp4.Sig.Camlp4Syntax)
: Camlp4.Sig.Printer(Syntax.Ast).S =
struct
open Syntax

class visitor = object
inherit Ast.fold as super

val t = []
method t = t

method expr = function
| <:expr@loc< f $str:singular$ >> ->

let t = singular :: t in
{< t = t >}

| e -> super#expr e
end

let print_interf ?input_file ?output_file _ = ()

let print_implem ?input_file ?output_file ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
List.iter prerr_endline t
end

(* Register the new printer. *)

module M = Camlp4.Register.OCamlPrinter(Id)(Make)

Richard Jones

unread,
Mar 21, 2008, 11:52:03 AM3/21/08
to Jeremy Yallop, caml...@inria.fr
On Fri, Mar 21, 2008 at 03:36:11PM +0000, Jeremy Yallop wrote:
> Richard Jones wrote:
> >I cannot for the life of me work out how to get this to compile. I've
> >tried about a dozen different variations of the module names, 'open',
> >'include' etc. and got a dozen different errors.
>
> Here's another variation that compiles and seems to work. I changed the
> following:
>
> * the argument type to Make from "Syntax" to "Camlp4Syntax"
> * the register functor from "Printer" to "OCamlPrinter"
> * the accumulated value in "expr" from "str" to "singular".

Brilliant, that works, thanks!

> | <:expr@loc< f $str:singular$ >> ->
> let t = singular :: t in

The above was just a typo when I was simplifying the program ...

Rich.

--
Richard Jones
Red Hat

_______________________________________________

Richard Jones

unread,
Mar 21, 2008, 11:58:57 AM3/21/08
to Nicolas Pouillard, caml...@inria.fr

Thanks. I've now got pr_gettext working again.

BTW, I think the ability to be able to map & fold over the AST is an
extraordinarily powerful feature, and deserves many more examples and
tutorials. I know that strictly speaking it was possible before (with
Le Gall's ast-analyze extension) but the way it is done in the new
camlp4 makes the code much shorter.

Richard Jones

unread,
Mar 21, 2008, 12:44:46 PM3/21/08
to Nicolas Pouillard, OCaml Mailing List
I'm trying to link the camlp4 module into a binary, by doing:

ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
-o ocaml-xgettext

but my binary always prints out:

Failure: "No implementation printer"

Apparently I need to somehow register the printer as well? I can't
work out how to do that though.

As an aside, is mkcamlp4 supposed to do anything? For me it always
prints this error:

Cannot find file Camlp4.cma

Nicolas Pouillard

unread,
Mar 21, 2008, 12:51:49 PM3/21/08
to Richard W.M. Jones, OCaml Mailing List
Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:

> I'm trying to link the camlp4 module into a binary, by doing:
>
> ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> -o ocaml-xgettext
>
> but my binary always prints out:
>
> Failure: "No implementation printer"
>
> Apparently I need to somehow register the printer as well? I can't
> work out how to do that though.

What version of pr_gettext are you using?

> As an aside, is mkcamlp4 supposed to do anything? For me it always
> prints this error:
>
> Cannot find file Camlp4.cma

Hum mkcamlp4 seems br0ken :(

--
Nicolas Pouillard aka Ertai

signature.asc

Richard Jones

unread,
Mar 21, 2008, 12:56:43 PM3/21/08
to Nicolas Pouillard, OCaml Mailing List
On Fri, Mar 21, 2008 at 05:50:00PM +0100, Nicolas Pouillard wrote:
> Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:
> > I'm trying to link the camlp4 module into a binary, by doing:
> >
> > ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> > -o ocaml-xgettext
> >
> > but my binary always prints out:
> >
> > Failure: "No implementation printer"
> >
> > Apparently I need to somehow register the printer as well? I can't
> > work out how to do that though.
>
> What version of pr_gettext are you using?

Well, no version -- I'm trying to make Sylvain Le Gall's pr_gettext
work with camlp4 3.10. It has to be linked into a standalone binary
called ocaml-xgettext, but I can't work out how to do that.

Previously Sylvain was using mkcamlp4, but that is broken.

Nicolas Pouillard

unread,
Mar 21, 2008, 1:04:39 PM3/21/08
to Richard W.M. Jones, OCaml Mailing List
Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:56:25 +0100 2008:

> On Fri, Mar 21, 2008 at 05:50:00PM +0100, Nicolas Pouillard wrote:
> > Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:
> > > I'm trying to link the camlp4 module into a binary, by doing:
> > >
> > > ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> > > -o ocaml-xgettext
> > >
> > > but my binary always prints out:
> > >
> > > Failure: "No implementation printer"
> > >
> > > Apparently I need to somehow register the printer as well? I can't
> > > work out how to do that though.
> >
> > What version of pr_gettext are you using?
>
> Well, no version -- I'm trying to make Sylvain Le Gall's pr_gettext
> work with camlp4 3.10. It has to be linked into a standalone binary
> called ocaml-xgettext, but I can't work out how to do that.
>
> Previously Sylvain was using mkcamlp4, but that is broken.

I mean between different proposals for pr_gettext.ml, in this thread.

signature.asc

Richard Jones

unread,
Mar 21, 2008, 1:21:43 PM3/21/08
to Nicolas Pouillard, OCaml Mailing List

I was using Jeremy Yallop's simple version (same as your first
version). I got it to work by linking it as follows:

$(OCAMLC) \
-I +camlp4 camlp4lib.cma \
unix.cma \
`$(OCAMLFIND) query -r -predicates byte gettext.extract -i-format` \
`$(OCAMLFIND) query -r -predicates byte gettext.extract -a-format` \
`$(OCAMLFIND) query -r -predicates byte gettext.extract -o-format` \
Camlp4Bin.cmo \
-o $@

Richard Jones

unread,
Mar 21, 2008, 4:19:28 PM3/21/08
to caml...@inria.fr
This is the patch to make ocaml-gettext work against ocaml 3.10.X:

http://www.annexia.org/tmp/ocaml-gettext-0.2.0-20080321.patch

Sylvain Le Gall

unread,
Mar 26, 2008, 5:30:20 AM3/26/08
to caml...@inria.fr
On 21-03-2008, Richard Jones <ri...@annexia.org> wrote:
>
> Thanks. I've now got pr_gettext working again.
>
> BTW, I think the ability to be able to map & fold over the AST is an
> extraordinarily powerful feature, and deserves many more examples and
> tutorials. I know that strictly speaking it was possible before (with
> Le Gall's ast-analyze extension) but the way it is done in the new
> camlp4 makes the code much shorter.
>

And it gives me one reason to drop ast-analyze... This new camlp4
feature, seems indeed very powerful.

Regards,
Sylvain Le Gall

0 new messages