File: Precedence.pm

package info (click to toggle)
libpegex-perl 0.75-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 908 kB
  • sloc: perl: 3,288; makefile: 43; sh: 2
file content (26 lines) | stat: -rw-r--r-- 627 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use strict;
package Precedence;

sub precedence_rpn {
    my ($self, $expr, $table) = @_;
    my $tail = pop @$expr;
    for my $elem (@$tail) {
        push @$expr, @$elem;
    }
    my ($out, $ops) = ([], []);
    push @$out, shift @$expr;
    while (@$expr) {
        my $op = shift @$expr;
        my ($p, $a) = @{$table->{$op}}{'p', 'a'};
        while (@$ops) {
            my $p2 = $table->{$ops->[0]}{p};
            last if $p > $p2 or $p == $p2 and $a eq 'r';
            push @$out, shift @$ops;
        }
        unshift @$ops, $op;
        push @$out, shift @$expr;
    }
    $self->flatten([@$out, @$ops]);
}

1;