- 求职 : 软件工程师
- 论坛徽章:
- 3
|
看样要抛砖引玉了。
这是一个利用模块的实现:
https://github.com/ingydotnet/pe ... ial/Calculator.swim
- Consider the equation:
- 1 - 2 ^ 3 ^ 4 + 5 * 6 / 7
- Normal precedence and associativity rules make this the same as:
- (1 - (2 ^ (3 ^ 4)) + ((5 * 6) / 7))
复制代码- {
- package Calculator;
- use base 'Pegex::Tree', 'Precedence';
- my $operator_precedence_table = {
- '+' => {p => 1, a => 'l'},
- '-' => {p => 1, a => 'l'},
- '*' => {p => 2, a => 'l'},
- '/' => {p => 2, a => 'l'},
- '^' => {p => 3, a => 'r'},
- };
- sub got_expr {
- my ($self, $expr) = @_;
- $self->precedence_rpn($expr, $operator_precedence_table);
- }
- }
复制代码
|
|