caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Possible Parsing Bug with camlp4
@ 2007-11-27 20:22 echinuz echinuz
  2007-11-27 20:38 ` [Caml-list] " Bruno De Fraine
  0 siblings, 1 reply; 2+ messages in thread
From: echinuz echinuz @ 2007-11-27 20:22 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 4427 bytes --]

     Consider the following simple calculator using camlp4:

---------------------------------------------------------------------
$ cat pa_calc_quote.ml 
open Camlp4.PreCast;;

let expr_of_string = Syntax.Gram.parse_string Syntax.expr_eoi;;

type alg=
| Add of alg*alg
| Sub of alg*alg
| Mul of alg*alg
| Div of alg*alg
| Pow of alg*alg
| Int of int
;;

module AlgGram = MakeGram(Lexer);;
let expression = AlgGram.Entry.mk "expression";;
let expression_eoi = AlgGram.Entry.mk "expression_eoi";;
Camlp4_config.antiquotations := false;;

EXTEND AlgGram
GLOBAL: expression expression_eoi;
expression_eoi:
    [[ e=expression; `EOI -> Printf.printf "eoi\n"; e]];
expression:
    [ "add" LEFTA
        [ x=SELF; "+"; y=SELF -> Printf.printf "add\n";<:expr< Add($x$,$y$) >>
        | x=SELF; "-"; y=SELF -> Printf.printf "sub\n";<:expr< Sub($x$,$y$) >>]
    | "mul" LEFTA
        [ x=SELF; "*"; y=SELF -> Printf.printf "mul\n";<:expr< Mul($x$,$y$) >> 
        | x=SELF; "/"; y=SELF -> <:expr< Div($x$,$y$) >>]
    | "pow" RIGHTA
        [ x=SELF; "**"; y=SELF-> Printf.printf "pow\n";<:expr< Pow($x$,$y$) >>]
    | "simple" NONA
        [x=INT -> Printf.printf "int\n"; <:expr< Int $int:x$ >> 
        |"("; x=SELF; ")" -> Printf.printf "par\n"; x
        | `ANTIQUOT(("" | "expression"),x) -> expr_of_string _loc x]
    ];
END;;

let expand_alg_quot_expr loc _loc_name_opt quotation_contents =
    AlgGram.parse_string expression_eoi loc quotation_contents;;
Syntax.Quotation.add "alg" Syntax.Quotation.DynAst.expr_tag expand_alg_quot_expr;;
Syntax.Quotation.default := "alg";;
---------------------------------------------------------------------

On the top level, the following program parses fine, despite the `EOI requirement:

---------------------------------------------------------------------
$ ocaml -I +camlp4 camlp4of.cma ./pa_calc_quote.cmo
        Objective Caml version 3.10.0

        Camlp4 Parsing version 3.10.0

# open Pa_calc_quote;;
# let x= << 1+ >>;;
int
eoi
val x : Pa_calc_quote.alg = Int 1
---------------------------------------------------------------------

Now, consider the same program above with addition commented out:

---------------------------------------------------------------------
$ cat ./pa_calc_quote.ml 
open Camlp4.PreCast;;

let expr_of_string = Syntax.Gram.parse_string Syntax.expr_eoi;;

type alg=
| Add of alg*alg
| Sub of alg*alg
| Mul of alg*alg
| Div of alg*alg
| Pow of alg*alg
| Int of int
;;

module AlgGram = MakeGram(Lexer);;
let expression = AlgGram.Entry.mk "expression";;
let expression_eoi = AlgGram.Entry.mk "expression_eoi";;
Camlp4_config.antiquotations := false;;

EXTEND AlgGram
GLOBAL: expression expression_eoi;
expression_eoi:
    [[ e=expression; `EOI -> Printf.printf "eoi\n"; e]];
expression:
    [ "add" LEFTA
        [ (*x=SELF; "+"; y=SELF -> Printf.printf "add\n";<:expr< Add($x$,$y$) >>
        | *)x=SELF; "-"; y=SELF -> Printf.printf "sub\n";<:expr< Sub($x$,$y$) >>]
    | "mul" LEFTA
        [ x=SELF; "*"; y=SELF -> Printf.printf "mul\n";<:expr< Mul($x$,$y$) >> 
        | x=SELF; "/"; y=SELF -> <:expr< Div($x$,$y$) >>]
    | "pow" RIGHTA
        [ x=SELF; "**"; y=SELF-> Printf.printf "pow\n";<:expr< Pow($x$,$y$) >>]
    | "simple" NONA
        [x=INT -> Printf.printf "int\n"; <:expr< Int $int:x$ >> 
        |"("; x=SELF; ")" -> Printf.printf "par\n"; x
        | `ANTIQUOT(("" | "expression"),x) -> expr_of_string _loc x]
    ];
END;;

let expand_alg_quot_expr loc _loc_name_opt quotation_contents =
    AlgGram.parse_string expression_eoi loc quotation_contents;;
Syntax.Quotation.add "alg" Syntax.Quotation.DynAst.expr_tag expand_alg_quot_expr;;
Syntax.Quotation.default := "alg";;
---------------------------------------------------------------------

The exact same quotation above, run on the top level, now produces a parsing error:

---------------------------------------------------------------------
$ ocaml -I +camlp4 camlp4of.cma ./pa_calc_quote.cmo
        Objective Caml version 3.10.0

        Camlp4 Parsing version 3.10.0

# open Pa_calc_quote;;
# let x= << 1+ >>;;
# let x= << 1+ >>;;
While expanding quotation "alg" in a position of "expr":
  Parse error: illegal begin of expression_eoi
---------------------------------------------------------------------

Is this behavior expected or is this a bug?

       
---------------------------------
Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how.

[-- Attachment #2: Type: text/html, Size: 6047 bytes --]

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

* Re: [Caml-list] Possible Parsing Bug with camlp4
  2007-11-27 20:22 Possible Parsing Bug with camlp4 echinuz echinuz
@ 2007-11-27 20:38 ` Bruno De Fraine
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno De Fraine @ 2007-11-27 20:38 UTC (permalink / raw)
  To: echinuz echinuz; +Cc: Caml-list ml


On 27-nov-07, at 21:22, echinuz echinuz wrote:
> Is this behavior expected or is this a bug?

I (for one) didn't expect this behavior, check my bug reports from June:
http://caml.inria.fr/mantis/view.php?id=4329
http://caml.inria.fr/mantis/view.php?id=4330

Unfortunately though, there hasn't been any work on these bug reports.

Regards,
Bruno


--
Bruno De Fraine
Vrije Universiteit Brussel
Faculty of Applied Sciences, DINF - SSEL
Room 4K208, Pleinlaan 2, B-1050 Brussels
tel: +32 (0)2 629 29 75
fax: +32 (0)2 629 28 70
e-mail: Bruno.De.Fraine@vub.ac.be


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

end of thread, other threads:[~2007-11-27 20:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-27 20:22 Possible Parsing Bug with camlp4 echinuz echinuz
2007-11-27 20:38 ` [Caml-list] " Bruno De Fraine

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