Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: echinuz echinuz <echinuz@yahoo.com>
To: caml-list@inria.fr
Subject: Possible Parsing Bug with camlp4
Date: Tue, 27 Nov 2007 12:22:41 -0800 (PST)	[thread overview]
Message-ID: <557897.35404.qm@web60113.mail.yahoo.com> (raw)

[-- 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 --]

             reply	other threads:[~2007-11-27 20:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-27 20:22 echinuz echinuz [this message]
2007-11-27 20:38 ` [Caml-list] " Bruno De Fraine

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=557897.35404.qm@web60113.mail.yahoo.com \
    --to=echinuz@yahoo.com \
    --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