Version française
Home     About     Download     Resources     Contact us    
Browse thread
Problem with precedence declaration in .mly file
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Thomas Gazagnaire <thomas.gazagnaire@i...>
Subject: Re: [Caml-list] Problem with precedence declaration in .mly file
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 }

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