From: Martin Jambon <martin_jambon@emailuser.net>
To: Pietro Abate <Pietro.Abate@anu.edu.au>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] camlp4 help
Date: Fri, 15 Jul 2005 13:24:38 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.44.0507151232570.1525-100000@localhost> (raw)
In-Reply-To: <20050715083901.GA24763@pulp.anu.edu.au>
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
next prev parent reply other threads:[~2005-07-15 20:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-15 8:39 Pietro Abate
2005-07-15 20:24 ` Martin Jambon [this message]
2005-07-18 10:12 ` [Caml-list] " Hendrik Tews
2005-07-19 5:23 ` Pietro Abate
2005-07-20 7:37 ` Hendrik Tews
2005-07-20 9:57 ` Gerd Stolpmann
2009-03-21 3:41 Camlp4 help Andre Nathan
2009-03-21 15:26 ` [Caml-list] " blue storm
2009-03-21 16:14 ` Andre Nathan
2009-03-21 17:24 ` blue storm
2009-03-21 17:26 ` blue storm
2009-04-13 0:05 Andre Nathan
2009-04-13 19:08 ` [Caml-list] " Andre Nathan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.44.0507151232570.1525-100000@localhost \
--to=martin_jambon@emailuser.net \
--cc=Pietro.Abate@anu.edu.au \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox