Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Caml4p... help
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-10-09 (11:53)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Caml4p... help

On Wed, Oct 09, 2002 at 11:50:23AM +1000, Pietro Abate wrote:

> My goal is to transform this syntax 
>   str pc : [a;b;c]
> into a declaration like that.
>   let pc_str = `Forall [| `Rule a; `Rule b; `Rule c |] 

According to your 2nd message, you need a "let" at toplevel, not
a "". In this case, it is not "expr" that you must extend
but "str_item": indeed, the "let" at toplevel is a structure item,
not an expression.

> ocamlc -c -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I /usr/lib/ocaml/camlp4
> File "", line 18, characters 2-10:
> This expression has type unit but is here used with type MLast.expr

Like Jeff Henrikson said, it is due to the extraneous "|" in the first
rule of "str": you must not put one. It is interpreted as an empty rule,
returning "()". I recognize that it is a pitfall of the syntax of EXTEND.

A remark: the definition of your type "kind" is unuseful in this file.
It will be useful only in the files using this syntax extension.

For the LINDENT, instead of LIDENT, this is not detected at compile
time, but when you use your syntax expander by "camlp4o ./str.cmo"
it displays:

   Lexer initialization error:
   - the constructor "LINDENT" is not recognized by Plexer
   Uncaught exception: Failure("Grammar.extend")

In Jeff's answer the #load "camlp4o.cma";; at the beginning of the file
is unuseful: Camlp4 knows that it is camlp4. On the other hand, the
other loads "q_MLast.cmo" and "pa_extend.cmo" are good and useful, since
they allow you to compile your file just with "-pp camlp4o".

Here is my version of your file (tested):

#load "pa_extend.cmo";;
#load "q_MLast.cmo";;

open Pcaml;;

let str = Grammar.Entry.create gram "str";;

    [ [ "str"; n = LIDENT; ":"; s = str ->
        <:str_item< value $lid:n$ = $s$ >> ] ];
    [ [ n = LIDENT -> <:expr<`Rule $str:n$>>
      | "["; s = LIST1 str SEP ";"; "]" -> <:expr<`Forall [|$list:s$|] >>
      | "{"; s = LIST1 str SEP ";"; "}" -> <:expr<`First [|$list:s$|] >> ] ];

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: