Version française
Home     About     Download     Resources     Contact us    
Browse thread
Possible Parsing Bug with camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: echinuz echinuz <echinuz@y...>
Subject: Possible Parsing Bug with camlp4
     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.