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

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