Version française
Home     About     Download     Resources     Contact us    
Browse thread
camlp4: precedence, LEFTA, NONA, etc.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: blue storm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] camlp4: precedence, LEFTA, NONA, etc.
To my understanding, precendence levels correspond to the level you
define in the grammar, in the given order : in your case, you have
"=", "+" at the same level, with higher precendence than "And" and
"*". You should reverse your rules (starting with the
higher-precendence construnction instead of the atomic values), and
possibly split some of your level into different levels (eg. "<" and
"+") for finer-grained precedence.

See camlp4/Camlp4Parsers/Camlp4OcamlRevisedParser.ml's "expr" rule
(you may look for " expr:" in the source file) for a complete yet
understandable example.

On 3/12/09, Joel Reymont <joelr1@gmail.com> wrote:
> I'm trying to properly set up the precedence in my expression camlp4
> rule.
>
> It's not working properly, though.
>
> (* wrong!!! > has higher prec than and*)
> # parse_with_rule expr "1 > 2 and 3 > 4";;
> - : Easy_ast.expr = Cond (Int 1, GT, And (Int 2, Cond (Int 3, GT, Int
> 4)))
>
> (* right! mul is higher than plus *)
> # parse_with_rule expr "1 + 2 * 3";;
> - : Easy_ast.expr = Plus (Int 1, Mul (Int 2, Int 3))
>
> (* wrong!!! > should be higher than + *)
> # parse_with_rule expr "1 + 2 > 3";;
> - : Easy_ast.expr = Plus (Int 1, Cond (Int 2, GT, Int 3))
>
> (* wrong!!! mul should be higher than and *)
> # parse_with_rule expr "1 * 2 and 3 * 4";;
> - : Easy_ast.expr = Mul (Int 1, And (Int 2, Mul (Int 3, Int 4)))
>
> (* right!!! *)
> # parse_with_rule expr "1 * not 2";;
> - : Easy_ast.expr = Mul (Int 1, Not (Int 2))
>
> Here's my rule. What am I doing wrong?
>
> 	Thanks, Joel
>
> ---
>
>   expr:
>    [ NONA
>      [ (x, _) = INT -> Int x
>      | (x, _) = FLOAT -> Float x
>      | (s, _) = STRING -> Str s
>      | "true" -> Bool true
>      | "false" -> Bool false
>      | a = IDENT; "["; b = exprl; "]"; c = OPT ago ->
>          Var (a, b, c)
>      | a = IDENT; b = OPT ago ->
>        Var (a, [], b)
>      | "("; e = expr; ")" -> Group e
>      ]
>    | LEFTA
>      [ e1 = expr; "="; e2 = expr -> Cond (e1, EQ, e2)
>      | e1 = expr; "<="; e2 = expr -> Cond (e1, LE, e2)
>      | e1 = expr; ">="; e2 = expr -> Cond (e1, GE, e2)
>      | e1 = expr; "<"; e2 = expr -> Cond (e1, LT, e2)
>      | e1 = expr; ">"; e2 = expr -> Cond (e1, GT, e2)
>      | e1 = expr; "<>"; e2 = expr -> Cond (e1, NEQ, e2)
>      | e1 = expr; "+"; e2 = expr -> Plus (e1, e2)
>      | e1 = expr; "-"; e2 = expr -> Minus (e1, e2)
>      | "-"; e = expr -> UniMinus e
>      ]
>    | LEFTA
>      [ e1 = expr; "Or"; e2 = expr -> Or (e1, e2)
>      | e1 = expr; "And"; e2 = expr -> And (e1, e2)
>      | e1 = expr; "Mod"; e2 = expr -> Mod (e1, e2)
>      | e1 = expr; "*"; e2 = expr -> Mul (e1, e2)
>      | e1 = expr; "/"; e2 = expr -> Div (e1, e2)
>      | e = expr; [ "Points"; "Point" ]; i = OPT instr -> Points (e, i)
>      | "Not"; e = expr -> Not e
>      ]
>    ];
>
> ---
> http://tinyco.de
> --- Mac & iPhone
>
>
>
>
>
> _______________________________________________
> 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
>