caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* camlp4: precedence, LEFTA, NONA, etc.
@ 2009-03-11 23:46 Joel Reymont
  2009-03-12  9:13 ` [Caml-list] " blue storm
  0 siblings, 1 reply; 5+ messages in thread
From: Joel Reymont @ 2009-03-11 23:46 UTC (permalink / raw)
  To: O'Caml Mailing List

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






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

* Re: [Caml-list] camlp4: precedence, LEFTA, NONA, etc.
  2009-03-11 23:46 camlp4: precedence, LEFTA, NONA, etc Joel Reymont
@ 2009-03-12  9:13 ` blue storm
  2009-03-12  9:20   ` Joel Reymont
  2009-03-12  9:38   ` Joel Reymont
  0 siblings, 2 replies; 5+ messages in thread
From: blue storm @ 2009-03-12  9:13 UTC (permalink / raw)
  To: Joel Reymont; +Cc: O'Caml Mailing List

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
>


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

* Re: [Caml-list] camlp4: precedence, LEFTA, NONA, etc.
  2009-03-12  9:13 ` [Caml-list] " blue storm
@ 2009-03-12  9:20   ` Joel Reymont
  2009-03-12  9:38   ` Joel Reymont
  1 sibling, 0 replies; 5+ messages in thread
From: Joel Reymont @ 2009-03-12  9:20 UTC (permalink / raw)
  To: blue storm; +Cc: O'Caml Mailing List


On Mar 12, 2009, at 9:13 AM, blue storm wrote:

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


I have it reversed then as I thought the rules went in increasing  
order of precedence :-(. Thanks for correcting me!

---
http://tinyco.de
Mac, C++, OCaml




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

* Re: [Caml-list] camlp4: precedence, LEFTA, NONA, etc.
  2009-03-12  9:13 ` [Caml-list] " blue storm
  2009-03-12  9:20   ` Joel Reymont
@ 2009-03-12  9:38   ` Joel Reymont
  2009-03-12 11:31     ` Mauricio Fernandez
  1 sibling, 1 reply; 5+ messages in thread
From: Joel Reymont @ 2009-03-12  9:38 UTC (permalink / raw)
  To: blue storm; +Cc: O'Caml Mailing List

What about this quote from the camlp4 manual?

"An entry is composed of entry precedence levels, the first one being  
the least precedent and the last one the most."

I'm assuming that my expr counts as one rule where '+' and '=' have  
lesser precedence than 'and' and '*' since those follow after.

On Mar 12, 2009, at 9:13 AM, blue storm wrote:

> 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
> "*".

---
http://tinyco.de
Mac, C++, OCaml




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

* Re: [Caml-list] camlp4: precedence, LEFTA, NONA, etc.
  2009-03-12  9:38   ` Joel Reymont
@ 2009-03-12 11:31     ` Mauricio Fernandez
  0 siblings, 0 replies; 5+ messages in thread
From: Mauricio Fernandez @ 2009-03-12 11:31 UTC (permalink / raw)
  To: caml-list

On Thu, Mar 12, 2009 at 09:38:28AM +0000, Joel Reymont wrote:
>> 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
>> "*".
> What about this quote from the camlp4 manual?
>
> "An entry is composed of entry precedence levels, the first one being  
> the least precedent and the last one the most."
>
> I'm assuming that my expr counts as one rule where '+' and '=' have  
> lesser precedence than 'and' and '*' since those follow after.

In case of doubt, it's better to refer to Camlp4RevisedParser.ml, as said
before (I don't even remember where the camlp4 manual is, I just open that
file routinely when I'm about to do some camlp4 hacking :). In
Camlp4RevisedParser.ml, expr is defined as:

    expr:
      [ "top" RIGHTA
      ...
      | "+" LEFTA
        [ e1 = SELF; op = infixop2; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
      | "*" LEFTA
      ...
        | e1 = SELF; op = infixop3; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
      (infixop3 is *, / or %)
      | "**" RIGHTA
      ...
      | "unary minus" NONA
        [ "-"; e = SELF -> mkumin _loc "-" e
        | "-."; e = SELF -> mkumin _loc "-." e ]

So the levels are indeed listed in increasing order of precedence.

This might be of use, despite being for camlp4 pre-3.10:
http://martin.jambon.free.fr/extend-ocaml-syntax.html#priorities

-- 
Mauricio Fernandez  -   http://eigenclass.org


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

end of thread, other threads:[~2009-03-12 11:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-11 23:46 camlp4: precedence, LEFTA, NONA, etc Joel Reymont
2009-03-12  9:13 ` [Caml-list] " blue storm
2009-03-12  9:20   ` Joel Reymont
2009-03-12  9:38   ` Joel Reymont
2009-03-12 11:31     ` Mauricio Fernandez

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