Version française
Home     About     Download     Resources     Contact us    
Browse thread
camlp4 help
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] camlp4 help
On Fri, 15 Jul 2005, Pietro Abate wrote:

> Hi all,
> I'm having problems (once again) with camlp4...
>
> I'd like to parse and produce a bit of ocaml code out of the
> following description :
>
> ---------------------
> CONNECTIVES
>     "_&_",AssocLeft,And ;
>     "_v_",AssocLeft,Or
> END
>
> TABLEAU
>
>     RULE "And"
>     { A & B }
>     ----------
>       A ; B
>     END
>
> END
> ---------------
>
> The first time around I used quotes around the "{A & B}" and parsed the
> expression with a adhoc parser outside camlp4 (and it's all good and
> working).  Now I want to remove the quotes tut-cur and do everything
> using the pre-processor (quotes make my code quote un-readable).  I've
> started writing this code below, but I don't know how to proceed...
>
> The main problem is of course that I've to parse an expression (in rule),
> with a generic parser built on the top of the connectives that I've just
> declared. Is there a way to tell campl4 : pass everything in between
> RULE and END to a function my_parser ?

The normal way to do this would be to use a quotation, which is a lexical
element (like a string constant).
Your example would be written as:

[...]
TABLEAU

    <:rule< "And"
    { A & B }
    ----------
      A ; B
    >>

END


So if you create a quotation expander named "rule", it will be allowed to
appear where an expr or a patt is expected. You can probably find some
simple trick to check that it is not used out of a TABLEAU block if
necessary.
Then TABLEAU will be parsed as a list of patts or a list of exprs (LIST1
expr instead of LIST1 rule in your code) and you might have to disable
some of them
using a secondary check, i.e. expand the quotation to some reserved expr
or patt such as <:expr< Rule_quotation "text to parse" >>
and check that the list you get has only items of this form.
And now you have the freedom to convert the "TABLEAU" list of exprs
(which for you is just a list of strings to parse) into a
str_item containing whatever you like.

This strategy is a bit weird, but it should work.
An extreme solution would be to patch plexer.ml so that something of
the form RULE ... END becomes only one token which would be equivalent to
<:rule< ... >>, but modifying the camlp4 sources is probably not what you
want.


Martin


> I was looking for inspiration in M. Jambon excellent tutorial, but
> without much luck...
>
> thanks,
> p
>
> -----------------------(not tested)
> let rule = Grammar.Entry.create Pcaml.gram "rule";;
> let connective = Grammar.Entry.create Pcaml.gram "connective";;
>
> type assoc = |AssocLeft |AssocRight |AssocNone ;;
>
> let conntable = Hashtbl.create 15;;
>
> EXTEND
>   Pcaml.str_item: [
>     "CONNECTIVES"; clist = LIST1 connective SEP ";"; "END" ->
>       List.iter Hashtbl.add conntable clist;
>       <:str_item< value version = "connectives declared" >>
>     |"TABLEAU"; LIST1 rule; "END" ->
>         <:str_item< value version = "tableau declared" >>
>   ];
>
>   connective: [
>     s = STRING; ","; a = UIDENT; ","; r = UIDENT -> (s,a,r)
>   ];
>
>   (* how can I write a quotation outside the EXTEND syntax ? *)
>   (* how does this quotation looks like ? *)
>   (* Quotation.add my_parser ??? *)
>   rule: [
>     "RULE"; r = my_parser ??? "END" -> do_something r
>   ];
>
> END
> ------------------------
>
> --
> ++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
> ++
> ++ "All great truths begin as blasphemies." -George Bernard Shaw
> ++ Please avoid sending me Word or PowerPoint attachments.
>    See http://www.fsf.org/philosophy/no-word-attachments.html
>
> _______________________________________________
> 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
>

--
Martin Jambon, PhD
http://martin.jambon.free.fr