From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=HTML_MESSAGE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 50351BC6B for ; Wed, 31 Oct 2007 06:05:12 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJ6pJ0eAKs4Fnmdsb2JhbACCcotzAgEBBwQGERg X-IronPort-AV: E=Sophos;i="4.21,349,1188770400"; d="scan'208,217";a="3862240" Received: from netscaler2.rice.edu (HELO mh2.mail.rice.edu) ([128.42.206.5]) by mail1-smtp-roc.national.inria.fr with ESMTP; 31 Oct 2007 06:03:19 +0100 Received: from mh2.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh2.mail.rice.edu (Postfix) with ESMTP id 419581B415D for ; Tue, 30 Oct 2007 23:26:20 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.4 at mh2.mail.rice.edu Received: from mh2.mail.rice.edu ([127.0.0.1]) by mh2.mail.rice.edu (mh2.mail.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id k9bG+idjr4Bf for ; Tue, 30 Oct 2007 23:26:20 -0500 (CDT) Received: from [10.194.94.87] (unknown [10.194.94.87]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mh2.mail.rice.edu (Postfix) with ESMTP id 7FC341B415A for ; Tue, 30 Oct 2007 23:26:19 -0500 (CDT) Mime-Version: 1.0 (Apple Message framework v752.3) In-Reply-To: <1193753915.47273d3bb15f2@webmail.in-berlin.de> References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> Content-Type: multipart/alternative; boundary=Apple-Mail-62-749211815 Message-Id: <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> From: Angela Zhu Subject: Re: [Caml-list] Problem with precedence declaration in .mly file Date: Tue, 30 Oct 2007 23:26:16 -0500 To: caml-list@yquem.inria.fr X-Mailer: Apple Mail (2.752.3) X-Spam: no; 0.00; bandel:01 ocaml:01 parser:01 parser:01 interprets:01 prec:01 mult:01 mult:01 bandel:01 ocaml:01 interprets:01 prec:01 angela:98 angela:98 2007,:98 --Apple-Mail-62-749211815 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On Oct 30, 2007, at 9:18 AM, Oliver Bandel wrote: Zitat von Angela Zhu : Hi all, I have some problem with precedence declaration in OCaml parser. If I what to say exponential(ATOB) is prior to *(STAR) and / (DIVIDE), * and / are prior to +(PLUS) and -(MINUS), I wrote the following in the parser: /***** Precedence Rules *****/ ... %left PLUS MINUS %left STAR DIVIDE %left ATOB ... But I still have the following problems: (1) It appears that the parser reads "test = 2^2 + 7;" as "test = 2^9" instead of "test = 4+7", which would follow the conventional order of operations. (2)It also interprets "test = (1^2)/3 + 1;" as "test = (1 ^ 2 / (3 + 1));" Can any one help me to see why it happens? Why the precedence rules doesn't work? [...] Precedences also can be created by sophisticated organization of the grammar rules. But I want to avoid this. So, if your grammar rules may have a contradictory meaning, then your parser works not as expected. In general I would use the precedence-declarations only, when you run into parser conflicts, if you don't use them. When developing a grammr, I would recommend, first to start with the grammar rules, and add precedence-/associatitivity- declarations, at the end, if really necessary. What is the rest of your mly-file? A complete example would be helpful. Here is part of my .mly file: Beside the precedence issue, everything works fine. %{ open Past ... %} ... %token PLUS %token STAR %token MINUS %token DIVIDE %token AND %token OR ... %token ATOB /* A^B: exponential */ ... /***** Precedence Rules *****/ %left PLUS MINUS %left STAR DIVIDE %left ATOB %nonassoc prec_unary_minus %start prog %type prog %% prog: ... exp: ... | exp PLUS exp { Add($1, $3) } | MINUS exp { Sub(Value(VFloat(0.0)), $2) } | exp MINUS exp { Sub($1, $3) } | exp DIVIDE exp { Divide($1, $3) } | exp STAR exp { Mult($1, $3) } | exp ATOB exp { Atob($1, $3) } | value PLUS exp { Add(Value($1), $3) } | value MINUS exp { Sub(Value($1), $3) } | value DIVIDE exp { Divide(Value($1), $3) } | value STAR exp { Mult(Value($1), $3) } | value ATOB exp { Atob(Value($1), $3) } ... | IDENT { Id($1) } | value { Value($1) } ; ... --Apple-Mail-62-749211815 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1

On Oct 30, 2007, at 9:18 AM, Oliver Bandel = wrote:
Zitat von Angela Zhu <angela.zhu@cs.rice.edu>:

Hi = all,

I = have some problem with precedence declaration in OCaml = parser.
If I what to say exponential(ATOB) is prior to *(STAR) = and / (DIVIDE),
=A0 * and / are prior to = +(PLUS)=A0 = and = -(MINUS),
I wrote the following in the parser:


/***** Precedence Rules=A0 *****/
...
%left PLUS = MINUS
%left STAR DIVIDE
%left ATOB
...

But I still have the = following problems:
(1) It appears that the = parser
reads "test =3D 2^2 + 7;" as "test =3D 2^9" instead of = "test =3D 4+7", which
would follow the = conventional order of operations.

(2)It also interprets "test = =3D (1^2)/3 + 1;" as "test =3D (1 ^ 2
/ = (3 + 1));"

Can= any one help me to see why it happens? Why the precedence = rules
doesn't work?
[...]

Precedences also can be created by = sophisticated
organization of the grammar = rules.
But I want to avoid = this.

So,= if your grammar rules may have a contradictory
meaning, then your parser works not as = expected.

In = general I would use the precedence-declarations only,
when you run into parser conflicts, if you don't use = them.
When developing a grammr, I would recommend, first to = start
with the grammar rules, and add = precedence-/associatitivity-
declarations, at the end, = if really necessary.


What is the rest of your = mly-file?

A = complete example would be helpful.
Here is = part of my .mly file:
Beside the precedence = issue, everything works fine.

=A0%{

open = Past
...
%}

...

%token PLUS
%token = STAR
%token = MINUS
%token = DIVIDE
%token = AND=
%token OR
...

%token ATOB=A0 /* A^B: exponential */
...


/***** Precedence = Rules=A0 *****/
%left PLUS MINUS = =A0
%left STAR DIVIDE =A0
%left ATOB
%nonassoc = prec_unary_minus


%start = prog

%type = <Past.pprog> prog
%%
prog:
...
exp:
...
=A0| exp PLUS exp { Add($1, = $3) }
=A0| MINUS exp=A0 { Sub(Value(VFloat(0.0)), $2) = }
=A0| exp MINUS exp =A0 =A0 =A0 =A0 =A0 =A0 = = { = Sub($1, $3) = }
=A0| exp DIVIDE exp { = Divide($1, $3) = }
=A0| exp STAR exp=A0 =A0 =A0 =A0 =A0 =A0 =A0 = = { = Mult($1, $3) = }
=A0| exp ATOB exp=A0 =A0 =A0 =A0 =A0 =A0 =A0 = = { = Atob($1, $3) = }


=A0| value PLUS = exp= = = = { = Add(Value($1), $3) = }
=A0| value MINUS exp =A0 =A0 =A0 =A0 =A0 =A0 = = { = Sub(Value($1), $3) = }
=A0| value DIVIDE exp { = Divide(Value($1), $3) = }
=A0| value STAR exp=A0 =A0 =A0 =A0 =A0 =A0 =A0 = = { = Mult(Value($1), $3) = }
=A0| value ATOB exp=A0 =A0 =A0 =A0 =A0 =A0 =A0 = = { = Atob(Value($1), $3) = }


...

=A0| IDENT { Id($1) = }
=A0| value { = Value($1) }

=A0

;


...




= --Apple-Mail-62-749211815--