Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: jehenrik <jehenrik@y...>
Subject: Re: [Caml-list] Caml4p... help
> I've just started playing with Caml4p today and I'm already at a dead
> end. 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 |]

Your implementation is basically right, but it has a couple of small 
errors.  Superficially, LINDENT should be LIDENT.  Change both of 
those.  You have an extraneous "|" symbol after str: [[.  Fix that.  The 
harder to see mistake is that you accidentally name your entry "str" 
while simultaneously redefining it to be a keyword.  This is bad.  So 
change the name of your entry, for example.  Say "strentry" instead of 
str.  Then it works: (this has some modifications to run in the toplevel)

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

open Pcaml;;

type kind = [
   | `Rule of string
   | `First of kind array
   | `Forall of kind array
] ;;

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

EXTEND
   expr: AFTER "top"
     [[ "str"; n = LIDENT; ":"; s = strentry ->
         <:expr< let $lid:n$ = $s$ in $lid:n$ >> ]];

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

END;;

str pc : [a;b;c];;

# - : _[> `Forall of _[> `Rule of string] array] =
`Forall [|`Rule "a"; `Rule "b"; `Rule "c"|]



Good luck with your project and welcome to the world of camlp4!


Jeff Henrikson




On Tuesday, October 8, 2002, at 09:50 PM, Pietro Abate wrote:

> (*pp camlp4o pa_extend.cmo q_MLast.cmo *)
> open Pcaml;;
>
> type kind = [
>     |`Rule of string
>     |`First of kind array
>     |`Forall of kind array
> ]
>
> let str = Grammar.Entry.create gram "str";;
>
> EXTEND
>   expr: AFTER "top"
>     [[ "str"; n = LINDENT; ":"; s = str ->
>         <:expr< let $lid:n$ = $s$ in $lid:n$ >> ]];
>
>   str: [[
>     | n = LINDENT -> <:expr<`Rule $str:n$>>
>     | "["; s = LIST1 str SEP ";"; "]" -> <:expr<`Forall [|$list:s$|] >>
>     | "{"; s = LIST1 str SEP ";"; "}" -> <:expr<`First 
> [|$list:s$|] >> ]];
>
> END;;

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners