This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

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: Joel Reymont Subject: camlp4: precedence, LEFTA, NONA, etc.
```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

```