caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Joel Reymont <joelr1@gmail.com>
To: O'Caml Mailing List <caml-list@yquem.inria.fr>
Subject: camlp4: precedence, LEFTA, NONA, etc.
Date: Wed, 11 Mar 2009 23:46:59 +0000	[thread overview]
Message-ID: <20BA0B67-42DD-4E9F-9165-741D91ECCF44@gmail.com> (raw)

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






             reply	other threads:[~2009-03-11 23:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-11 23:46 Joel Reymont [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20BA0B67-42DD-4E9F-9165-741D91ECCF44@gmail.com \
    --to=joelr1@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).