line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
358
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package ExprParser; |
7
|
1
|
|
|
1
|
|
3
|
use base qw( Parser::MGC ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
461
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
5
|
use Feature::Compat::Try; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# An expression is a list of terms, joined by + or - operators |
12
|
|
|
|
|
|
|
sub parse |
13
|
|
|
|
|
|
|
{ |
14
|
15
|
|
|
15
|
|
14
|
my $self = shift; |
15
|
|
|
|
|
|
|
|
16
|
15
|
|
|
|
|
21
|
my $val = $self->parse_term; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
1 while $self->any_of( |
19
|
26
|
|
|
26
|
|
42
|
sub { $self->expect( "+" ); $self->commit; $val += $self->parse_term; 1 }, |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
57
|
|
20
|
18
|
|
|
18
|
|
29
|
sub { $self->expect( "-" ); $self->commit; $val -= $self->parse_term; 1 }, |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
21
|
|
21
|
15
|
|
|
15
|
|
42
|
sub { 0 }, |
22
|
15
|
|
|
|
|
59
|
); |
23
|
|
|
|
|
|
|
|
24
|
15
|
|
|
|
|
44
|
return $val; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# A term is a list of factors, joined by * or - operators |
28
|
|
|
|
|
|
|
sub parse_term |
29
|
|
|
|
|
|
|
{ |
30
|
26
|
|
|
26
|
|
25
|
my $self = shift; |
31
|
|
|
|
|
|
|
|
32
|
26
|
|
|
|
|
28
|
my $val = $self->parse_factor; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
1 while $self->any_of( |
35
|
36
|
|
|
36
|
|
58
|
sub { $self->expect( "*" ); $self->commit; $val *= $self->parse_factor; 1 }, |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
63
|
|
36
|
29
|
|
|
29
|
|
43
|
sub { $self->expect( "/" ); $self->commit; $val /= $self->parse_factor; 1 }, |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
28
|
|
37
|
26
|
|
|
26
|
|
72
|
sub { 0 }, |
38
|
26
|
|
|
|
|
143
|
); |
39
|
|
|
|
|
|
|
|
40
|
26
|
|
|
|
|
101
|
return $val; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# A factor is either a parenthesized expression, or an integer |
44
|
|
|
|
|
|
|
sub parse_factor |
45
|
|
|
|
|
|
|
{ |
46
|
36
|
|
|
36
|
|
36
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$self->any_of( |
49
|
36
|
|
|
36
|
|
53
|
sub { $self->committed_scope_of( "(", 'parse', ")" ) }, |
50
|
34
|
|
|
34
|
|
51
|
sub { $self->token_int }, |
51
|
36
|
|
|
|
|
133
|
); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if( !caller ) { |
55
|
|
|
|
|
|
|
my $parser = __PACKAGE__->new; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
while( defined( my $line = ) ) { |
58
|
|
|
|
|
|
|
try { |
59
|
|
|
|
|
|
|
my $ret = $parser->from_string( $line ); |
60
|
|
|
|
|
|
|
print "$ret\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
catch ( $e ) { |
63
|
|
|
|
|
|
|
print $e; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |