From: Angela Zhu <angela.zhu@cs.rice.edu>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Problem with precedence declaration in .mly file
Date: Tue, 30 Oct 2007 23:11:04 -0500 [thread overview]
Message-ID: <1A83BBA2-37FB-4E88-93C8-27D23FD4C271@cs.rice.edu> (raw)
In-Reply-To: <1193753915.47273d3bb15f2@webmail.in-berlin.de>
[-- Attachment #1: Type: text/plain, Size: 3169 bytes --]
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),
>> * 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 <Past.pprog> 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) }
;
...
[-- Attachment #2: Type: text/html, Size: 44813 bytes --]
next prev parent reply other threads:[~2007-10-31 4:22 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-30 14:00 Angela Zhu
2007-10-30 14:20 ` [Caml-list] " Oliver Bandel
[not found] ` <1193753915.47273d3bb15f2@webmail.in-berlin.de>
2007-10-31 4:11 ` Angela Zhu [this message]
2007-10-31 4:26 ` Angela Zhu
2007-10-31 5:52 ` skaller
[not found] ` <BE3BA36D-7E69-426F-B558-26CBCF9D78F6@cs.rice.edu>
[not found] ` <1193814307.8355.68.camel@rosella.wigram>
2007-10-31 7:16 ` Re: " Angela Zhu
2007-10-31 11:52 ` Peter Ilberg
2007-10-31 13:51 ` Angela Zhu
2007-10-31 14:14 ` Thomas Gazagnaire
2007-10-31 14:40 ` Angela Zhu
[not found] ` <472894EC.8040902@irisa.fr>
[not found] ` <F5672A31-E4C0-488B-B594-F75E3DA262D8@cs.rice.edu>
[not found] ` <47289C47.8020609@irisa.fr>
2007-10-31 15:34 ` Angela Zhu
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=1A83BBA2-37FB-4E88-93C8-27D23FD4C271@cs.rice.edu \
--to=angela.zhu@cs.rice.edu \
--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