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:
_______________________________________________
Caml-list mailing list. Subscription management: