caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* DELETE_RULE arguments?
@ 2009-07-19 12:34 Serge Leblanc
  2009-07-21 22:35 ` [Caml-list] " Serge Leblanc
  2009-07-22 10:03 ` blue storm
  0 siblings, 2 replies; 5+ messages in thread
From: Serge Leblanc @ 2009-07-19 12:34 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 691 bytes --]

Hi,
In the following scenario, I would like to remove the first rule of
expr. But I can't find the good arguments for the command DELETE_RULE.

$ rlwrap ocaml -I +camlp4 -I +ulex 
# #load "camlp4rf.cma" ;;
# #load "pa_ulex.cma" ;
# open Camlp4.PreCast ;
# Gram.Entry.print Format.std_formatter Syntax.expr ;
# DELETE_RULE Gram Syntax.expr: "lexer"; OPT "|"; LIST0 [ regexp; "->";
expr ] SEP "|" END ; 
Unbound value regexp

Otherwise, is there a way to know if the active syntax, is the revised
or not?

Thank you.
--
Serge Leblanc
gpg --keyserver  hkp://keyserver.ubuntu.com:11371 --recv-keys 0x33243C1B
Fingerprint = 066C 005F 5595 D85C 7673  D969 1DD4 90C4 3324 3C1B

[-- Attachment #1.2: Type: text/html, Size: 1273 bytes --]

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

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

* Re: [Caml-list] DELETE_RULE arguments?
  2009-07-19 12:34 DELETE_RULE arguments? Serge Leblanc
@ 2009-07-21 22:35 ` Serge Leblanc
  2009-07-22  7:26   ` blue storm
  2009-07-22 10:03 ` blue storm
  1 sibling, 1 reply; 5+ messages in thread
From: Serge Leblanc @ 2009-07-21 22:35 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1.1: Type: text/plain, Size: 954 bytes --]

In the attached exemple, the DELETE_RULE raise an exception Not_found.
The statement DELETE_RULE Gram foo: "foo"; LIDENT; END bar is exploded
as follows :

$ camlp4of -str "DELETE_RULE Gram foo: "foo"; LIDENT; bar END" 

Gram.delete_rule foo
  [ Gram.Snterm (Gram.Entry.obj (foo : 'foo Gram.Entry.t));
    Gram.Stoken
      (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT ((_))"));
    Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]


while the running example is the following : 

Gram.delete_rule foo
      [ Gram.Skeyword "foo";
        Gram.Stoken
          (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT _"));
        Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]


How do I write the rule DELETE_RULE to work in this case? 
--
Serge Leblanc
gpg --keyserver  hkp://keyserver.ubuntu.com:11371 --recv-keys 0x33243C1B
Fingerprint = 066C 005F 5595 D85C 7673  D969 1DD4 90C4 3324 3C1B

[-- Attachment #1.1.2: Type: text/html, Size: 1737 bytes --]

[-- Attachment #1.2: pr_ulex.ml --]
[-- Type: text/x-ocaml, Size: 1057 bytes --]


type t1 = A | B
type t2 = Foo of string * t1

let print_t2 = function 
  Foo (s,t) -> let print_t1 = function A -> "A" | B -> "B" 
                in Printf.printf "Foo (\"%s\",%s)\n" s (print_t1 t)

open Camlp4.PreCast

let foo = Gram.Entry.mk "foo"
let bar = Gram.Entry.mk "bar"

EXTEND Gram
  GLOBAL: foo bar;
  foo: [ [ "foo"; i = LIDENT; b = bar -> Foo(i,b) ] ];
  bar: [ [ "?" -> A | "." -> B ] ];
END

let r = Gram.parse_string foo (Loc.mk "<string>") "foo x?"
  in print_t2 r

let _ = Gram.Entry.print Format.std_formatter foo

try
   DELETE_RULE Gram foo: "foo"; LIDENT; bar END
with Not_found -> Format.printf "@[no rule matched!@]@\n" 

(* the correct sentence 
let _ =
  try
    Gram.delete_rule foo
      [ Gram.Skeyword "foo";
        Gram.Stoken
          (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT _"));
        Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]
  with | Not_found -> Format.printf "@[no rule matched!@]@\n"
*)

let _ = Gram.Entry.print Format.std_formatter foo

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

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

* Re: [Caml-list] DELETE_RULE arguments?
  2009-07-21 22:35 ` [Caml-list] " Serge Leblanc
@ 2009-07-22  7:26   ` blue storm
  2009-07-22 12:54     ` Serge Leblanc
  0 siblings, 1 reply; 5+ messages in thread
From: blue storm @ 2009-07-22  7:26 UTC (permalink / raw)
  To: Serge Leblanc; +Cc: caml-list

That was the correct code, except you shouldn't have tested it in the shell :

$ camlp4of -str "DELETE_RULE Gram bar: \"foo\"; LIDENT; bar END"
Gram.delete_rule bar
  [ Gram.Skeyword "foo";
    Gram.Stoken (((fun | LIDENT ((_)) -> true | _ -> false), "LIDENT ((_))"));
    Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]

You can have the exact "LIDENT _" output with (`LIDENT _), though i'm
not sure it changes the end result.

On Wed, Jul 22, 2009 at 12:35 AM, Serge Leblanc<serge.leblanc@orange.fr> wrote:
> In the attached exemple, the DELETE_RULE raise an exception Not_found.
> The statement DELETE_RULE Gram foo: "foo"; LIDENT; END bar is exploded as
> follows :
>
> $ camlp4of -str "DELETE_RULE Gram foo: "foo"; LIDENT; bar END"
>
> Gram.delete_rule foo
>   [ Gram.Snterm (Gram.Entry.obj (foo : 'foo Gram.Entry.t));
>     Gram.Stoken
>       (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT ((_))"));
>     Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]
>
> while the running example is the following :
>
> Gram.delete_rule foo
>       [ Gram.Skeyword "foo";
>         Gram.Stoken
>           (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT _"));
>         Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]
>
> How do I write the rule DELETE_RULE to work in this case?
> --
> Serge Leblanc
> gpg --keyserver  hkp://keyserver.ubuntu.com:11371 --recv-keys 0x33243C1B
> Fingerprint = 066C 005F 5595 D85C 7673  D969 1DD4 90C4 3324 3C1B
> _______________________________________________
> 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
>
>


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

* Re: [Caml-list] DELETE_RULE arguments?
  2009-07-19 12:34 DELETE_RULE arguments? Serge Leblanc
  2009-07-21 22:35 ` [Caml-list] " Serge Leblanc
@ 2009-07-22 10:03 ` blue storm
  1 sibling, 0 replies; 5+ messages in thread
From: blue storm @ 2009-07-22 10:03 UTC (permalink / raw)
  To: Serge Leblanc; +Cc: caml-list

On Sun, Jul 19, 2009 at 2:34 PM, Serge Leblanc<serge.leblanc@orange.fr> wrote:
> Otherwise, is there a way to know if the active syntax, is the revised or
> not?

The way used in Camlp4Parsers/Camlp4ListComprehension.ml is to try to
delete a rule and see if it fails :

  value is_revised =
    try do {
      DELETE_RULE Gram expr: "["; sem_expr_for_list; "::"; expr; "]" END;
      True
    } with [ Not_found -> False ];

It works fine in this case as the grammar rule have to be deleted
anyway, but if you don't want to affect the grammar you could test
parsing for failure. Something like :

  value is_revised =
    try do { Gram.parse_string Syntax.expr Loc.ghost "fun [ x -> x]"; True }
    with [ _ -> False];

Of course, those tests could fail given a syntax that is neither the
classic nor the revised syntax. Eg. if you write an extension adding
the fun [ .. ] syntax to the standard syntax and it would return true.
A safer way to test the used syntaxes is the Register.loaded_modules values :

# #camlp4o;;
# Camlp4.Register.loaded_modules;;
- : string list ref =
{contents =
  ["Camlp4OCamlParserParser"; "Camlp4OCamlRevisedParserParser";
   "Camlp4OCamlParser"; "Camlp4OCamlRevisedParser"]}

# #camlp4r;;
# Camlp4.Register.loaded_modules;
- : ref (list string) =
{val=["Camlp4OCamlRevisedParserParser"; "Camlp4OCamlRevisedParser"]}


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

* Re: [Caml-list] DELETE_RULE arguments?
  2009-07-22  7:26   ` blue storm
@ 2009-07-22 12:54     ` Serge Leblanc
  0 siblings, 0 replies; 5+ messages in thread
From: Serge Leblanc @ 2009-07-22 12:54 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 2181 bytes --]

Unfortunately with ( `_ LIDENT) the statement is also split into "
LIDENT _" in pattern matching:

$ camlp4of -str 'DELETE_RULE Gram bar: "foo"; (`LIDENT _); bar END'
Gram.delete_rule bar
  [ Gram.Skeyword "foo";
    Gram.Stoken (((function | LIDENT _ -> true | _ -> false), "LIDENT
_"));
    Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]


On Wed, 2009-07-22 at 09:26 +0200, blue storm wrote: 

> That was the correct code, except you shouldn't have tested it in the shell :
> 
> $ camlp4of -str "DELETE_RULE Gram bar: \"foo\"; LIDENT; bar END"
> Gram.delete_rule bar
>   [ Gram.Skeyword "foo";
>     Gram.Stoken (((fun | LIDENT ((_)) -> true | _ -> false), "LIDENT ((_))"));
>     Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]
> 
> You can have the exact "LIDENT _" output with (`LIDENT _), though i'm
> not sure it changes the end result.
> 
> On Wed, Jul 22, 2009 at 12:35 AM, Serge Leblanc<serge.leblanc@orange.fr> wrote:
> > In the attached exemple, the DELETE_RULE raise an exception Not_found.
> > The statement DELETE_RULE Gram foo: "foo"; LIDENT; END bar is exploded as
> > follows :
> >
> > $ camlp4of -str "DELETE_RULE Gram foo: "foo"; LIDENT; bar END"
> >
> > Gram.delete_rule foo
> >   [ Gram.Snterm (Gram.Entry.obj (foo : 'foo Gram.Entry.t));
> >     Gram.Stoken
> >       (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT ((_))"));
> >     Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]
> >
> > while the running example is the following :
> >
> > Gram.delete_rule foo
> >       [ Gram.Skeyword "foo";
> >         Gram.Stoken
> >           (((function | LIDENT ((_)) -> true | _ -> false), "LIDENT _"));
> >         Gram.Snterm (Gram.Entry.obj (bar : 'bar Gram.Entry.t)) ]
> >
> > How do I write the rule DELETE_RULE to work in this case?
> > --
> > Serge Leblanc
> > gpg --keyserver  hkp://keyserver.ubuntu.com:11371 --recv-keys 0x33243C1B
> > Fingerprint = 066C 005F 5595 D85C 7673  D969 1DD4 90C4 3324 3C1B


--
Serge Leblanc
gpg --keyserver  hkp://keyserver.ubuntu.com:11371 --recv-keys 0x33243C1B
Fingerprint = 066C 005F 5595 D85C 7673  D969 1DD4 90C4 3324 3C1B


[-- Attachment #1.2: Type: text/html, Size: 3300 bytes --]

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

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

end of thread, other threads:[~2009-07-22 12:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-19 12:34 DELETE_RULE arguments? Serge Leblanc
2009-07-21 22:35 ` [Caml-list] " Serge Leblanc
2009-07-22  7:26   ` blue storm
2009-07-22 12:54     ` Serge Leblanc
2009-07-22 10:03 ` blue storm

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).