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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id DD334BC6B for ; Tue, 30 Oct 2007 15:00:21 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAPvVJkeAKs4Fnmdsb2JhbACCcotuAgEBBwQGERg X-IronPort-AV: E=Sophos;i="4.21,347,1188770400"; d="scan'208,217";a="5281814" Received: from netscaler2.rice.edu (HELO mh2.mail.rice.edu) ([128.42.206.5]) by mail3-smtp-sop.national.inria.fr with ESMTP; 30 Oct 2007 15:00:20 +0100 Received: from mh2.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh2.mail.rice.edu (Postfix) with ESMTP id E844136D4C2 for ; Tue, 30 Oct 2007 09:00:18 -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 nORgXfblRZjh for ; Tue, 30 Oct 2007 09:00:18 -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 A97CD36D4D0 for ; Tue, 30 Oct 2007 09:00:18 -0500 (CDT) Mime-Version: 1.0 (Apple Message framework v752.3) To: caml-list@yquem.inria.fr Message-Id: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> Content-Type: multipart/alternative; boundary=Apple-Mail-41-697251759 From: Angela Zhu Subject: Problem with precedence declaration in .mly file Date: Tue, 30 Oct 2007 09:00:16 -0500 X-Mailer: Apple Mail (2.752.3) X-Spam: no; 0.00; ocaml:01 parser:01 parser:01 interprets:01 ocaml:01 interprets:01 angela:98 angela:98 monaco:98 monaco:98 wrote:01 wrote:01 helvetica:98 precedence:01 precedence:01 --Apple-Mail-41-697251759 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed 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? Thanks, Angela ------------------------------------------ Dept. of CS, Rice U. http://www.cs.rice.edu/~yz2/ ------------------------------------------ --Apple-Mail-41-697251759 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1
Hi = all,=A0

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 =A0
%left STAR DIVIDE = =A0
%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?

Thanks,
Dept. of CS, Rice U.
http://www.cs.rice.edu/~yz2/
Received: from einhorn.in-berlin.de (localhost [127.0.0.1]) by einhorn.in-berlin.de (8.13.6/8.13.6/Debian-1) with ESMTP id l9UEKIxw031821 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Tue, 30 Oct 2007 15:20:18 +0100 Received: (from www-data@localhost) by einhorn.in-berlin.de (8.13.6/8.13.6/Submit) id l9UEKISd031819 for caml-list@yquem.inria.fr; Tue, 30 Oct 2007 15:20:18 +0100 X-Authentication-Warning: einhorn.in-berlin.de: www-data set sender to oliver@first.in-berlin.de using -f Received: from dslb-088-073-074-138.pools.arcor-ip.net (dslb-088-073-074-138.pools.arcor-ip.net [88.73.74.138]) by webmail.in-berlin.de (IMP) with HTTP for ; Tue, 30 Oct 2007 15:20:18 +0100 Message-ID: <1193754018.47273da2796f7@webmail.in-berlin.de> Date: Tue, 30 Oct 2007 15:20:18 +0100 From: Oliver Bandel To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Problem with precedence declaration in .mly file References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> In-Reply-To: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.6 X-Scanned-By: MIMEDefang_at_IN-Berlin_e.V. on 192.109.42.8 X-Spam: no; 0.00; bandel:01 in-berlin:01 ocaml:01 parser:01 parser:01 interprets:01 angela:98 angela:98 wrote:01 oliver:01 oliver:01 precedence:01 precedence:01 caml-list:01 conflicts:01 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. 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. Ciao, Oliver 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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 64AFDBC6B for ; Wed, 31 Oct 2007 05:22:26 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAHqgJ0eAKs4Fnmdsb2JhbACCcotzAgEBBwQGERg X-IronPort-AV: E=Sophos;i="4.21,349,1188770400"; d="scan'208,217";a="18788396" Received: from netscaler2.rice.edu (HELO mh1.mail.rice.edu) ([128.42.206.5]) by mail4-smtp-sop.national.inria.fr with ESMTP; 31 Oct 2007 05:21:11 +0100 Received: from mh1.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh1.mail.rice.edu (Postfix) with ESMTP id 704F2385E95 for ; Tue, 30 Oct 2007 23:11:10 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.4 at mh1.mail.rice.edu Received: from mh1.mail.rice.edu ([127.0.0.1]) by mh1.mail.rice.edu (mh1.mail.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tywYl01s0Uj8 for ; Tue, 30 Oct 2007 23:11:10 -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 mh1.mail.rice.edu (Postfix) with ESMTP id 87135385E73 for ; Tue, 30 Oct 2007 23:11:09 -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-60-748299630 Message-Id: <1A83BBA2-37FB-4E88-93C8-27D23FD4C271@cs.rice.edu> From: Angela Zhu Subject: Re: [Caml-list] Problem with precedence declaration in .mly file Date: Tue, 30 Oct 2007 23:11:04 -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 atan:01 rhs:01 rhs:01 tokens:01 lexer:01 prec:01 one-to-one:01 mult:01 mult:01 bandel:01 --Apple-Mail-60-748299630 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; 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 open Parsing open ParseError let pi = 4.0 *. atan 1.0;; let get_range n = { pos_start = Parsing.rhs_start_pos n; pos_end = Parsing.rhs_end_pos n; } let unclosed opening_name opening_num closing_name closing_num = raise(Error(Unclosed(get_range opening_num, opening_name, get_range closing_num, closing_name))) %} /* List of all tokens the lexer can output */ ... %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 %% /* Rules for parsing. The parsing rules should generally be in a */ /* one-to-one correspondence with the BNF */ /* type prog = Prog of consDeclare list * varDeclare list * inpDeclare list * sysDeclare list */ prog: exp: LPAREN exp RPAREN { $2 } | LPAREN exp error { unclosed "(" 1 ")" 3 } | 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-60-748299630 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
open = Parsing
open = ParseError

let pi =3D = 4.0 *. atan = 1.0;;

let get_range n =3D = {
=A0 pos_start =3D = Parsing.rhs_start_pos n;
=A0 = pos_end =3D Parsing.rhs_end_pos n;
}

let unclosed = opening_name opening_num closing_name closing_num = =3D
=A0 raise(Error(Unclosed(get_range opening_num, = opening_name,
=A0=A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 get_range closing_num, = closing_name)))
%}

/* List of = all tokens the lexer can output */

...

%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
%%
/* Rules for = parsing. The parsing rules should generally be in = a */
/* = one-to-one = correspondence with = the BNF */
/* = type prog =3D Prog = of consDeclare list = * varDeclare list * inpDeclare list * sysDeclare list = */




prog:

exp:
=A0=A0 = LPAREN exp RPAREN=A0 =A0 =A0 =A0 =A0 { = $2 }
=A0| = LPAREN exp error =A0 =A0 =A0 =A0 =A0 { unclosed = "(" = 1 ")" = 3 }
=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-60-748299630-- 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-- 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.3 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id E0238BC6B for ; Wed, 31 Oct 2007 06:54:11 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAOC0J0fLENaHnmdsb2JhbACOZQIBAQcEBhEY X-IronPort-AV: E=Sophos;i="4.21,349,1188770400"; d="scan'208";a="5305164" Received: from ipmail03.adl2.internode.on.net ([203.16.214.135]) by mail3-smtp-sop.national.inria.fr with ESMTP; 31 Oct 2007 06:54:10 +0100 X-IronPort-AV: E=Sophos;i="4.21,349,1188743400"; d="scan'208";a="178284786" Received: from ppp121-44-98-190.lns10.syd6.internode.on.net (HELO [192.168.1.201]) ([121.44.98.190]) by ipmail03.adl2.internode.on.net with ESMTP; 31 Oct 2007 16:22:01 +1030 Subject: Re: [Caml-list] Problem with precedence declaration in .mly file From: skaller To: Angela Zhu Cc: caml-list@yquem.inria.fr In-Reply-To: <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> Content-Type: text/plain Date: Wed, 31 Oct 2007 16:52:00 +1100 Message-Id: <1193809920.8355.41.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.12.0 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; ocaml:01 angela:98 sourceforge:01 wrote:01 precedence:01 precedence:01 integer:01 caml-list:01 mly:01 grammar:02 grammar:02 declaration:02 zhu:04 hack:04 star:94 On Tue, 2007-10-30 at 23:26 -0500, Angela Zhu wrote: > /***** Precedence Rules *****/ > ... > %left PLUS MINUS > %left STAR DIVIDE > %left ATOB > 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. DO NOT USE THEM. The rules are hard to explain and very badly designed, in other words, they're a hack. Ocaml provides them for compatibility with older yacc like tools. Write your grammar properly instead, in pseudo code: term = factor | term + factor factor = atom | factor * atom atom = INTEGER | ( term ) gives the precedence of * over + and left associativity, without using incomprehensible precedence rules. -- John Skaller Felix, successor to C++: http://felix.sf.net 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=AWL 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 B1CEDBC6B for ; Wed, 31 Oct 2007 08:16:12 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAABfIJ0eAKs4Fnmdsb2JhbACOZAIBAQcEBhEY X-IronPort-AV: E=Sophos;i="4.21,350,1188770400"; d="scan'208";a="3864941" Received: from netscaler2.rice.edu (HELO mh1.mail.rice.edu) ([128.42.206.5]) by mail1-smtp-roc.national.inria.fr with ESMTP; 31 Oct 2007 08:16:11 +0100 Received: from mh1.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh1.mail.rice.edu (Postfix) with ESMTP id E8C37385350; Wed, 31 Oct 2007 02:16:09 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.4 at mh1.mail.rice.edu Received: from mh1.mail.rice.edu ([127.0.0.1]) by mh1.mail.rice.edu (mh1.mail.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EMINBN1DiEiK; Wed, 31 Oct 2007 02:16:09 -0500 (CDT) Received: from [192.168.1.102] (c-98-195-25-217.hsd1.tx.comcast.net [98.195.25.217]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mh1.mail.rice.edu (Postfix) with ESMTP id A7CC238532D; Wed, 31 Oct 2007 02:16:09 -0500 (CDT) In-Reply-To: <1193814307.8355.68.camel@rosella.wigram> References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> <1193809920.8355.41.camel@rosella.wigram> <1193814307.8355.68.camel@rosella.wigram> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <117B8C1B-3586-4A77-B3B6-5FF881BE8B62@cs.rice.edu> Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: 7bit From: Angela Zhu Subject: Re: Re: Re: [Caml-list] Problem with precedence declaration in .mly file Date: Wed, 31 Oct 2007 02:16:07 -0500 To: skaller X-Mailer: Apple Mail (2.752.3) X-Spam: no; 0.00; ocaml:01 expr:01 expr:01 syntax:01 syntax:01 parser:01 angela:98 angela:98 2007,:98 .....:98 wrote:01 wrote:01 precedence:01 integer:01 integer:01 Thanks a lot for your detailed explanation. The problem is that now the AST for my language is getting really big. I am not sure how much work it will take. Best regards, Angela ------------------------------------------ Dept. of CS, Rice Unitersity http://www.cs.rice.edu/~yz2/ ------------------------------------------ On Oct 31, 2007, at 2:05 AM, skaller wrote: > > On Wed, 2007-10-31 at 01:02 -0500, Angela Zhu wrote: >>> >>> >>> DO NOT USE THEM. The rules are hard to explain and very badly >>> designed, in other words, they're a hack. Ocaml provides >>> them for compatibility with older yacc like tools. >>> >>> Write your grammar properly instead, in pseudo code: >>> >>> term = factor | term + factor >>> factor = atom | factor * atom >>> atom = INTEGER | ( term ) >> >> ... Then I need to change my whole AST..... >> :( > > Yes, that's possible. The 'simple' AST isn't efficient, > that is, where you have a variant > > type term = Term_Factor of factor | Term_plus of term * factor > > because of the first case. However this isn't necessary if you just > use something like > > type expr = Integer of int | Apply of string * expr list > > then you can just do: > > term: > | factor { $1 } > | term + factor { Apply ("+",[$1;$3]) } > > and similarly for the other productions. The typing here > is weaker than you may want, for example you can get > nonsense like > > Apply ["*",Integer 1] > > so you might try a safer encoding, eg using > > | Integer of int > | Unary of string * expr > | Binary of string * expr * expr > > > The point is, this AST is still less structured than one > which exactly reflects the syntax tree -- but that is the > point of an 'Abstract' syntax tree (AST). > > Exactly how much work you do in the parser is a difficult > design choice. > > > -- > John Skaller > Felix, successor to C++: http://felix.sf.net > 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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL 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 F1A44BC6B for ; Wed, 31 Oct 2007 12:52:12 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAALYJKEeCpA7unmdsb2JhbACOZAIBAQcEBhEY X-IronPort-AV: E=Sophos;i="4.21,351,1188770400"; d="scan'208";a="3874811" Received: from mailserv97-us.natinst.com ([130.164.14.238]) by mail1-smtp-roc.national.inria.fr with SMTP; 31 Oct 2007 12:52:12 +0100 Received: from mailserv97-us (mailserv97-usa [130.164.14.238]) by mailserv97-us.natinst.com (Postfix) with ESMTP id 948D26740E3; Wed, 31 Oct 2007 06:52:10 -0500 (CDT) Received: from dalek.amer.corp.natinst.com (10.0.44.32) by mailserv97-us (F-Secure/fsigk_smtp/488/mailserv97-us); Wed, 31 Oct 2007 06:52:10 -0600 (CST) X-Virus-Status: clean(F-Secure/fsigk_smtp/488/mailserv97-us) Received: from dalek.dy.natinst.com (peter@localhost.dy.natinst.com [127.0.0.1]) by dalek.dy.natinst.com (8.14.0/8.14.0) with ESMTP id l9VBqAsI015667; Wed, 31 Oct 2007 06:52:10 -0500 (CDT) Received: from localhost (peter@localhost) by dalek.dy.natinst.com (8.14.0/8.14.0/Submit) with ESMTP id l9VBq9p8026606; Wed, 31 Oct 2007 06:52:10 -0500 (CDT) X-Authentication-Warning: dalek.dy.natinst.com: peter owned process doing -bs Date: Wed, 31 Oct 2007 06:52:09 -0500 (CDT) From: Peter Ilberg X-X-Sender: peter@dalek.dy.natinst.com To: Angela Zhu Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Problem with precedence declaration in .mly file In-Reply-To: <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> Message-ID: References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Spam: no; 0.00; ocamlyacc:01 ocamlyacc:01 mult:01 mult:01 2007,:98 angela:98 $1,:98 $1,:98 wrote:01 precedence:01 precedence:01 caml-list:01 ident:01 mly:01 grammar:02 I have no experience with ocamlyacc, but looking at your grammar below, it seems that you don't need the 'value PLUS exp' etc rules. All these cases should be covered already by the 'exp PLUS exp' rules at the beginning and the 'value' rule at the end. Try removing the 'value PLUS exp' rules. Maybe ocamlyacc gets confused if it has two sets of productions that it has to disambiguate with precedence rules. --- Peter On Tue, 30 Oct 2007, Angela Zhu wrote: > 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) } *** do you really need these productions? > | 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) } > > > ; 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=AWL 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 EB800BC6B for ; Wed, 31 Oct 2007 14:51:05 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAKokKEeAKs4Fnmdsb2JhbACOZAIBAQcEBhEY X-IronPort-AV: E=Sophos;i="4.21,351,1188770400"; d="scan'208";a="3879643" Received: from netscaler2.rice.edu (HELO mh1.mail.rice.edu) ([128.42.206.5]) by mail1-smtp-roc.national.inria.fr with ESMTP; 31 Oct 2007 14:51:05 +0100 Received: from mh1.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh1.mail.rice.edu (Postfix) with ESMTP id DEF21385F17; Wed, 31 Oct 2007 08:51:03 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.4 at mh1.mail.rice.edu Received: from mh1.mail.rice.edu ([127.0.0.1]) by mh1.mail.rice.edu (mh1.mail.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mfeOhTO1xVig; Wed, 31 Oct 2007 08:51:03 -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 mh1.mail.rice.edu (Postfix) with ESMTP id B3F43385F07; Wed, 31 Oct 2007 08:51:03 -0500 (CDT) In-Reply-To: References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <21DAF2E3-1E29-476D-BEB3-FD608EBD92A2@cs.rice.edu> Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: 7bit From: Angela Zhu Subject: Re: [Caml-list] Problem with precedence declaration in .mly file Date: Wed, 31 Oct 2007 08:51:01 -0500 To: Peter Ilberg X-Mailer: Apple Mail (2.752.3) X-Spam: no; 0.00; ocamlyacc:01 ocamlyacc:01 syntax:01 angela:98 angela:98 2007,:98 wrote:01 precedence:01 precedence:01 caml-list:01 mly:01 grammar:02 declaration:02 seems:03 zhu:04 On Oct 31, 2007, at 6:52 AM, Peter Ilberg wrote: > > I have no experience with ocamlyacc, but looking at your grammar > below, it seems that you don't need the 'value PLUS exp' etc rules. > All these cases should be covered already by the 'exp PLUS exp' > rules at the beginning and the 'value' rule at the end. > > Try removing the 'value PLUS exp' rules. Maybe ocamlyacc gets > confused if it has two sets of productions that it has to > disambiguate with precedence rules. I removed 'value PLUS exp' rules. The precedence is still not correct, what is more, 1+ t (with t declared) gives a syntax error. Thanks, Angela > > --- Peter 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=none 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 88DA6BC6B for ; Wed, 31 Oct 2007 15:14:21 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.21,351,1188770400"; d="scan'208";a="3880677" Received: from bellamela.irisa.fr ([131.254.11.146]) by mail1-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 31 Oct 2007 15:14:21 +0100 Message-ID: <47288DE3.3000603@irisa.fr> Date: Wed, 31 Oct 2007 15:14:59 +0100 From: Thomas Gazagnaire User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Problem with precedence declaration in .mly file References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> <21DAF2E3-1E29-476D-BEB3-FD608EBD92A2@cs.rice.edu> In-Reply-To: <21DAF2E3-1E29-476D-BEB3-FD608EBD92A2@cs.rice.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; irisa:01 expr:01 expr:01 cheers:01 ocamlyacc:01 ocamlyacc:01 syntax:01 beginner's:01 ocaml:01 bug:01 angela:98 2007,:98 angela:98 beginners:01 token:01 The following code works perfectly for me. Maybe you introduce some undesired precedence rules in your grammar rules. ---- %token INT %token PLUS MINUS DIVIDE STAR ATOB END %start main %type main %left PLUS MINUS %left STAR DIVIDE %left ATOB %% main: | expr END { $1 } expr: | INT { $1 } | expr PLUS expr { $1 + $3 } | expr MINUS expr { $1 - $3 } | expr DIVIDE expr { $1 / $3 } | expr STAR expr { $1 * $3 } | expr ATOB expr { int_of_float ( (float_of_int $1) ** (float_of_int $3) ) } ; --- And then "2^2+7\n" gives me "11" Cheers, Thomas Angela Zhu a écrit : > > On Oct 31, 2007, at 6:52 AM, Peter Ilberg wrote: > >> >> I have no experience with ocamlyacc, but looking at your grammar >> below, it seems that you don't need the 'value PLUS exp' etc rules. >> All these cases should be covered already by the 'exp PLUS exp' rules >> at the beginning and the 'value' rule at the end. >> >> Try removing the 'value PLUS exp' rules. Maybe ocamlyacc gets confused >> if it has two sets of productions that it has to disambiguate with >> precedence rules. > > I removed 'value PLUS exp' rules. > The precedence is still not correct, what is more, 1+ t (with t > declared) gives a syntax error. > > Thanks, > Angela > > > >> >> --- Peter > > _______________________________________________ > 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 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=AWL,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 D231CBC6B for ; Wed, 31 Oct 2007 15:40:50 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAGgwKEeAKs4Fnmdsb2JhbACCcotyAgEBBwQGERg X-IronPort-AV: E=Sophos;i="4.21,351,1188770400"; d="scan'208,217";a="3881931" 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 15:40:36 +0100 Received: from mh2.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh2.mail.rice.edu (Postfix) with ESMTP id 659C81B4159; Wed, 31 Oct 2007 09:40:32 -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 MVp-nRLBUIOK; Wed, 31 Oct 2007 09:40:32 -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 1A94D1B419F; Wed, 31 Oct 2007 09:40:30 -0500 (CDT) In-Reply-To: <47288DE3.3000603@irisa.fr> References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> <21DAF2E3-1E29-476D-BEB3-FD608EBD92A2@cs.rice.edu> <47288DE3.3000603@irisa.fr> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: multipart/alternative; boundary=Apple-Mail-71-786061259 Message-Id: <11C06B30-EA78-40BB-984F-B403DC9BAE68@cs.rice.edu> Cc: caml-list@yquem.inria.fr From: Angela Zhu Subject: Re: [Caml-list] Problem with precedence declaration in .mly file Date: Wed, 31 Oct 2007 09:40:25 -0500 To: Thomas Gazagnaire X-Mailer: Apple Mail (2.752.3) X-Spam: no; 0.00; prec:01 expr:01 expr:01 cheers:01 ocamlyacc:01 ocamlyacc:01 syntax:01 beginner's:01 ocaml:01 bug:01 beginner's:01 ocaml:01 bug:01 prec:01 cheers:01 --Apple-Mail-71-786061259 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed But here are all the precedence rules I have: /***** Precedence Rules *****/ %left GT LT %left PLUS MINUS %left STAR DIVIDE %left ATOB %nonassoc prec_unary_minus -Angela On Oct 31, 2007, at 9:14 AM, Thomas Gazagnaire wrote: > The following code works perfectly for me. Maybe you introduce some =20= > undesired precedence rules in your grammar rules. > > > ---- > > %token INT > %token PLUS MINUS DIVIDE STAR ATOB END > > %start main > %type main > > %left PLUS MINUS > %left STAR DIVIDE > %left ATOB > > %% > > > main: > | expr END { $1 } > > expr: > | INT { $1 } > | expr PLUS expr { $1 + $3 } > | expr MINUS expr { $1 - $3 } > | expr DIVIDE expr { $1 / $3 } > | expr STAR expr { $1 * $3 } > | expr ATOB expr { int_of_float ( (float_of_int $1) ** =20 > (float_of_int $3) ) } > ; > > --- > > And then "2^2+7\n" gives me "11" > > Cheers, > Thomas > > Angela Zhu a =E9crit : >> On Oct 31, 2007, at 6:52 AM, Peter Ilberg wrote: >>> >>> I have no experience with ocamlyacc, but looking at your grammar =20 >>> below, it seems that you don't need the 'value PLUS exp' etc =20 >>> rules. All these cases should be covered already by the 'exp PLUS =20= >>> exp' rules at the beginning and the 'value' rule at the end. >>> >>> Try removing the 'value PLUS exp' rules. Maybe ocamlyacc gets =20 >>> confused if it has two sets of productions that it has to =20 >>> disambiguate with precedence rules. >> I removed 'value PLUS exp' rules. >> The precedence is still not correct, what is more, 1+ t (with t =20 >> declared) gives a syntax error. >> Thanks, >> Angela >>> >>> --- Peter >> _______________________________________________ >> 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 > > _______________________________________________ > 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 > --Apple-Mail-71-786061259 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 But here are=A0 all the = precedence rules I have:

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


-Angela


On Oct 31, = 2007, at 9:14 AM, Thomas Gazagnaire wrote:

The following code works perfectly for me. Maybe you = introduce some undesired precedence rules in your grammar = rules.



%token <int> INT
%token = PLUS MINUS DIVIDE STAR ATOB END

%start main
%type <int> main

%left PLUS = MINUS
%left STAR DIVIDE
%left ATOB

%%


main:
| expr END { $1 }

| INT { $1 }
| expr PLUS expr { $1 + $3 }
| expr MINUS expr { $1 - $3 }
| expr DIVIDE expr { $1 / $3 }
| expr STAR expr { $1 * $3 }
| expr ATOB expr { int_of_float ( (float_of_int $1) = ** (float_of_int $3) ) }
;


And then "2^2+7\n" gives me "11"

Thomas

Angela = Zhu a =E9crit :
On Oct = 31, 2007, at 6:52 AM, Peter Ilberg wrote:

I have no experience with ocamlyacc, but looking at = your grammar below, it seems that you don't need the 'value PLUS exp' = etc rules. All these cases should be covered already by the 'exp PLUS = exp' rules at the beginning and the 'value' rule at the end.

Try = removing the 'value PLUS exp' rules. Maybe ocamlyacc gets confused if it = has two sets of productions that it has to disambiguate with precedence = rules.
I removed 'value PLUS exp' = rules.
The precedence is still not = correct, what is more, 1+ t (with t declared) gives a syntax = error.
Thanks,
Angela

--- = Peter
Caml-list mailing list. Subscription = management:
Caml-list mailing list. Subscription = management:

=

= --Apple-Mail-71-786061259-- 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=AWL,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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id C7323BC6B for ; Wed, 31 Oct 2007 16:34:45 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEc9KEeAKs4Fnmdsb2JhbACCcotyAgEBBwQGERg X-IronPort-AV: E=Sophos;i="4.21,351,1188770400"; d="scan'208,217";a="18807927" Received: from netscaler2.rice.edu (HELO mh1.mail.rice.edu) ([128.42.206.5]) by mail4-smtp-sop.national.inria.fr with ESMTP; 31 Oct 2007 16:34:43 +0100 Received: from mh1.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh1.mail.rice.edu (Postfix) with ESMTP id 29EC1386248; Wed, 31 Oct 2007 10:34:39 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.4 at mh1.mail.rice.edu Received: from mh1.mail.rice.edu ([127.0.0.1]) by mh1.mail.rice.edu (mh1.mail.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4WTFVWUSJiOx; Wed, 31 Oct 2007 10:34:39 -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 mh1.mail.rice.edu (Postfix) with ESMTP id B06A638622F; Wed, 31 Oct 2007 10:34:38 -0500 (CDT) In-Reply-To: <47289C47.8020609@irisa.fr> References: <6C4DFFEF-0A5E-496F-9468-56693FFA4DC2@cs.rice.edu> <1193753915.47273d3bb15f2@webmail.in-berlin.de> <23EC0ABA-12EE-49DE-B76A-1D91BCCAE1BA@cs.rice.edu> <21DAF2E3-1E29-476D-BEB3-FD608EBD92A2@cs.rice.edu> <47288DE3.3000603@irisa.fr> <11C06B30-EA78-40BB-984F-B403DC9BAE68@cs.rice.edu> <472894EC.8040902@irisa.fr> <47289C47.8020609@irisa.fr> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: multipart/alternative; boundary=Apple-Mail-73-789308227 Message-Id: <4EB1D447-02D8-43CA-AA3E-1A8DE7C31CE6@cs.rice.edu> Cc: caml-list@yquem.inria.fr From: Angela Zhu Subject: Re: [Caml-list] Problem with precedence declaration in .mly file Date: Wed, 31 Oct 2007 10:34:32 -0500 To: Thomas Gazagnaire X-Mailer: Apple Mail (2.752.3) X-Spam: no; 0.00; angela:98 angela:98 2007,:98 2007,:98 wrote:01 wrote:01 precedence:01 caml-list:01 mly:01 grammar:02 grammar:02 define:02 define:02 exp:02 exp:02 --Apple-Mail-73-789308227 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Thanks Thomas! And thanks for all the help I got! The problem is solved at this point. Regards, Angela ------------------------------------------ Dept. of CS, Rice Unitersity http://www.cs.rice.edu/~yz2/ ------------------------------------------ On Oct 31, 2007, at 10:16 AM, Thomas Gazagnaire wrote: > Well, I think you have a problem in your grammar rules. Your are > defining the same PLUS, MINUS, STAR and DIVIDE rules in multiple > places in your code. Try to define them once, only in the "exp" > rule. Them, remove in the "exp" rule : --Apple-Mail-73-789308227 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=US-ASCII
Thanks = Thomas!
And thanks for all the help I got!
The = problem is solved at this point.

Regards,
Angela
------------------------------------------
Dept. of CS, Rice Unitersity
http://www.cs.rice.edu/~yz2/

On Oct 31, 2007, = at 10:16 AM, Thomas Gazagnaire wrote:

Well, I think you have a problem in your grammar = rules. Your are defining the same PLUS, MINUS, STAR and DIVIDE rules in = multiple places in your code. Try to define them once, only in the "exp" = rule. Them, remove in the "exp" rule = :

= --Apple-Mail-73-789308227--