caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: blue storm <bluestorm.dylc@gmail.com>
To: Joel Reymont <joelr1@gmail.com>
Cc: "O'Caml Mailing List" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] camlp4: precedence, LEFTA, NONA, etc.
Date: Thu, 12 Mar 2009 10:13:44 +0100	[thread overview]
Message-ID: <527cf6bc0903120213k731ee9f5xb6b1104f12f001b@mail.gmail.com> (raw)
In-Reply-To: <20BA0B67-42DD-4E9F-9165-741D91ECCF44@gmail.com>

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
>


  reply	other threads:[~2009-03-12  9:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-11 23:46 Joel Reymont
2009-03-12  9:13 ` blue storm [this message]
2009-03-12  9:20   ` [Caml-list] " 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=527cf6bc0903120213k731ee9f5xb6b1104f12f001b@mail.gmail.com \
    --to=bluestorm.dylc@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=joelr1@gmail.com \
    /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).