caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Ampersand in camlp4 grammar extension
@ 2009-01-28  9:39 Paul Steckler
  2009-01-28 20:47 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Steckler @ 2009-01-28  9:39 UTC (permalink / raw)
  To: caml-list

I'm writing a camlp4 grammar extension for OCaml that tries to match
ampersands and rewrite expressions containing them.  That wouldn't
be a good idea if my code contained ordinary ampersands used for conjunctions.
But the code doesn't.

I'm struggling how to match the ampersands in my expression rewrite rules.
I've tried "&" and SYMBOL "&", which don't work -- the preprocessed file contains
the original ampersands.  OTOH, I've written rules which use things like "->" and
work fine.

Any help appreciated.

-- Paul
--
Paul Steckler
National ICT Australia
paul DOT steckler AT nicta.com.au


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Ampersand in camlp4 grammar extension
  2009-01-28  9:39 Ampersand in camlp4 grammar extension Paul Steckler
@ 2009-01-28 20:47 ` Nicolas Pouillard
  2009-01-29  4:36   ` Paul Steckler
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Pouillard @ 2009-01-28 20:47 UTC (permalink / raw)
  To: Paul Steckler; +Cc: caml-list

Excerpts from Paul Steckler's message of Wed Jan 28 10:39:16 +0100 2009:
> I'm writing a camlp4 grammar extension for OCaml that tries to match
> ampersands and rewrite expressions containing them.  That wouldn't
> be a good idea if my code contained ordinary ampersands used for conjunctions.
> But the code doesn't.
> 
> I'm struggling how to match the ampersands in my expression rewrite rules.
> I've tried "&" and SYMBOL "&", which don't work -- the preprocessed file contains
> the original ampersands.  OTOH, I've written rules which use things like "->" and
> work fine.

The answer depends on what you want to achieve, if you want to give to "&"
another syntactic form (no longer infix, or change the priorities), then I
would recommend you to just use "&" in your grammar rules. It will declares it
as a keyword (add it into a table of keywords) and then the token filter
between (between the lexer and the parser) will transform it from SYMBOL to
KEYWORD.

In fact, I've just looked at the lexer and the '&' token is *at the beginning*
emitted using the SYMBOL constructor, however by looking at the OCaml parser
files, one can see that "&" is already used literally, so already declared as
a keyword and so already transformed as a KEYWORD "&". Conclusion use "&" to
match it.

However I can guess in your question that you don't really want to change the
syntax of the binary operator "&", but rather to change it's meaning. In
camlp4 there is a much more sane and easier way to do this using filters.
By the way there is an example in the camlp4 sources that does exactly this.

$ cd camlp4/examples
$ cat apply_operator.ml
open Camlp4.PreCast;
AstFilters.register_str_item_filter
  (Ast.map_expr
    (fun
     [ <:expr@loc< $e1$ & $e2$ >> -> <:expr@loc< $e1$ $e2$ >>
     | e -> e ]))#str_item;
$ ocamlbuild -tags camlp4rf,use_camlp4 apply_operator.cmo
$ cat apply_operator_test.ml
(* To force it to be inlined. If not it's not well typed. *)
let ( & ) = ();;

fun f g h x -> f & g & h x
$ camlp4o ./_build/apply_operator.cmo apply_operator_test.ml
(* To force it to be inlined. If not it's not well typed. *)
let ( & ) = ()
    
let _ = fun f g h x -> f (g (h x))

Best regards,

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] Ampersand in camlp4 grammar extension
  2009-01-28 20:47 ` [Caml-list] " Nicolas Pouillard
@ 2009-01-29  4:36   ` Paul Steckler
  2009-01-29  9:17     ` nicolas.pouillard
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Steckler @ 2009-01-29  4:36 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: caml-list

Hi Nicolas,

> However I can guess in your question that you don't really want to change the
syntax of the binary operator "&", but rather to change it's meaning. In
camlp4 there is a much more sane and easier way to do this using filters.
By the way there is an example in the camlp4 sources that does exactly this.

Yes, this seems to work -- but it would be simpler, in fact, if I could just use
a rule in my grammar extension.

Do you know if there's a way to debug grammar extensions?  Maybe I need to
DELETE a rule from the original grammar, though it's not clear to me which
rule that is.

Thanks,

-- Paul


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] Ampersand in camlp4 grammar extension
  2009-01-29  4:36   ` Paul Steckler
@ 2009-01-29  9:17     ` nicolas.pouillard
  0 siblings, 0 replies; 4+ messages in thread
From: nicolas.pouillard @ 2009-01-29  9:17 UTC (permalink / raw)
  To: Paul Steckler; +Cc: caml-list

Excerpts from Paul Steckler's message of Thu Jan 29 05:36:44 +0100 2009:
> Hi Nicolas,
> 
> > However I can guess in your question that you don't really want to change the
> syntax of the binary operator "&", but rather to change it's meaning. In
> camlp4 there is a much more sane and easier way to do this using filters.
> By the way there is an example in the camlp4 sources that does exactly this.
> 
> Yes, this seems to work -- but it would be simpler, in fact, if I could just use
> a rule in my grammar extension.


> Do you know if there's a way to debug grammar extensions?

I don't think there is a nice way do to it.

> Maybe I need to
> DELETE a rule from the original grammar, though it's not clear to me which
> rule that is.

Look at Camlp4Parsers/Camlp4OCamlRevisedParser.ml

The rule is:
infixop5:
[ [ x = [ "&" | "&&" ] -> <:expr< $lid:x$ >> ] ]
;
               

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-01-29  9:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-28  9:39 Ampersand in camlp4 grammar extension Paul Steckler
2009-01-28 20:47 ` [Caml-list] " Nicolas Pouillard
2009-01-29  4:36   ` Paul Steckler
2009-01-29  9:17     ` nicolas.pouillard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).