I think if you space out your priorities a bit more, right associative operators could be handled by adding a bonus to their weight:
As written: String: 3 ** 2 ** 4 Weights: 2 9 2 9 2
Now change the algorith as follows:
String: 3 ** 2 ** 4 Weights: 20 80 20 81 20 More complex string: String: 3 ** ( 6 - 1 * 4 ) ** 2 Weights: 20 80 90 120 140 120 170 120 90 81 20 String: 3 ** ( 6 - 4 ) ** 2 Weights: 20 80 90 120 140 120 90 81 20 String: 3 ** ( 2 ) ** 2 Weights: 20 80 90 120 90 81 20 String: 3 ** 2 ** 2 Weights: 20 80 20 81 20 String: 3 ** 4 Weights: 20 80 20 String: 81 Weights: 20
Obviously, the weakness of this approach is that if you have too many right associative operators at a level, your priorities collide. You can make "too many" arbitrarily large by using a large multiplier for the original priority scheme.
I'll leave actual implementation up to you.
TGI says moo
In reply to Re: a timid approach to parsing arithmetic expressions in perl
by TGI
in thread a timid approach to parsing arithmetic expressions in perl
by spx2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |