Expr.pm

filter: Statementstmt 85.5% 89 / 104 Branchbran 78.9% 30 / 38 Conditioncond 87.5% 21 / 24 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 80.0% 8 / 10 Podpod 50.0% 4 / 8 totaltotal 81.9% 159 / 194 CC 28 SCAR 19.3
CC
28
Cov
84%
Elem
10.3
Expr
8.1
NextToken
7.0
CRAP 6.9
« Expr/VarSet.pm ↑ Summary Expr/Rule.pm »
1 #!/usr/bin/perl
2  
3 # Expr.pm - A perl parser or mathematicall expressions.
4 # (c) Copyright 1998 Hakan Ardo <hakan@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  
20 =head1 NAME
21  
22 Math::Expr - Parses mathematical expressions
23  
24 =head1 SYNOPSIS
25  
26 use Math::Expr;
27
28 SetOppDB(new Math::Expr::OpperationDB('<DBFileName>'));
29 $e=Parse("a+4*b-d/log(s)+f(d,e)");
30  
31 =head1 DESCRIPTION
32  
33 Parses mathematical expressions into a tree structure. The expressions
34 may contain integers, real numbers, alphanumeric variable names,
35 alphanumeric function names and most other characters might be used
36 as operators. The operators can even be longer than one character!
37 The only limitation is that a variable or function name may not start
38 on a digit, and not all chars are accepted as operations. To be exact,
39 here is the grammatic (in perl regexp notation):
40  
41 <Expr> = -?<Elem>(<OpChr><Elem>)*
42 <Elem> = <Number>|<Var>|<Function>|\(<Expr>\)
43 <Number> = <Integer>|<Float>
44 <Integer> = \d+
45 <Float> = \d*\.\d+
46 <Var> = [a-zA-Z][a-zA-Z0-9]*(:[a-zA-Z][a-zA-Z0-9]*)?
47 <Function> = [a-zA-Z][a-zA-Z0-9]*\(<Expr>(,<Expr>)*\)
48 <OpChr> = [^a-zA-Z0-9\(\)\,\.\:]+
49  
50 If the - sign is present at the beginning of an <Expr> Then a neg()
51 function is placed around it. That is to allow constructions like
52 "-a*b" or "b+3*(-7)".
53  
54 A variable consists of two parts separated by a ':'-char. The first
55 part is the variable name, and the second optional part is its type.
56 Default type is Real.
57  
58 =head1 METHODS
59  
60 =cut
61  
62 package Math::Expr;
63 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 1 require Exporter;
66 1 ▶use vars qw (@ISA @EXPORT_OK @EXPORT $Pri $OppDB);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 @ISA = qw (Exporter);
69 1 @EXPORT_OK = qw($Pri $OppDB);
70 1 @EXPORT = qw(Parse Priority SetOppDB);
71  
72 1 require Math::Expr::Opp;
73 1 require Math::Expr::Var;
74 1 require Math::Expr::Num;
75 1 require Math::Expr::VarSet;
76 1 require Math::Expr::OpperationDB;
77  
78 1 $Pri={'^'=>50, '/'=>40, '*'=>30, '-'=>20, '+'=>10, '='=>0};
79  
80 =head2 $e=Parse($str)
81  
82 This will parse the string $str and return an expression tree, in the
83 form of a Math::Expr::Opp object (or in simple cases only a
84 Math::Expr::Var or Math::Expr::Num object).
85  
86 =cut
87  
88  
89 =head2 $p = new Math::Expr
90  
91 This is the constructor, it creates an object which later can be used
92 to parse the strings.
93  
94 =cut
95  
96 42 ▶sub Parse {
Subroutine Parse: called CC=2 SCAR=7.1
Pod documented
97 42 my ($str) = @_;
98 42 my $self=bless {};
99  
100 0 ▶ if (ref $str) {warn "Bad param str: $str"}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str) 0 42 42
101  
102 42 $str=~ s/\s*//g;
103 42 $self->{'Str'}=$str;
104  
105 42 $self->NextToken;
106 42 my $e=$self->Expr;
107 42 $e;
108 }
109  
110 =head2 Priority({'^'=>50, '/'=>40, '*'=>30, '-'=>20, '+'=>10})
111  
112 This will set the priority of ALL the operands (there is currently no
113 way to change only one of them). The priority decides what should be
114 constructed if several operands is listed without delimiters. Eg if
115 a+b*c should be treated as (a+b)*c or a+(b*c). (Default is listed in
116 header).
117  
118 The priority is global for all parsers and all expretions, so
119 changing it here will change it for all parsers and parsed objects.
120 The idea is to use this method to initiate the system before using it.
121  
122 =cut
123  
124 0 ▶sub Priority {
Subroutine Priority: not called CC=1 SCAR=6.9
Pod documented
125 0 my ($p) = @_;
126 0 $Pri=$p;
127 }
128  
129 =head2 SetOppDB($db)
130  
131 Sets the OpperationDB to be used to $db. See L<Math::Expr::OpperationDB>
132 for more info.
133  
134 This is a global variable afecting all parsers and all parsed structures.
135  
136 =cut
137  
138 1 ▶sub SetOppDB {
Subroutine SetOppDB: called CC=1 SCAR=0.0
Pod documented
139 1 my ($db) = @_;
140  
141 1 $OppDB= $db;
142 1 $OppDB->InitDB;
143 }
144  
145 266 ▶sub NextToken {
Subroutine NextToken: called CC=7 SCAR=19.5
Pod undocumented
146 266 my $self = shift;
147  
148 266 ▶ if ($self->{'Str'} =~ s/^([a-zA-Z][a-zA-Z0-9]*)\(//) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->{'Str'} =~ s/^([a-zA-Z][a-zA-Z0-9]*)\(//) { } 12 254 266
elsif ($self->{'Str'} =~ s/^([a-zA-Z][a-zA-Z0-9]*(:[a-zA-Z][a-zA-Z0-9]*)?)//) { } 98 156 254
elsif ($self->{'Str'} =~ s/^(\d*\.\d+|\d+)//) { } 14 142 156
elsif ($self->{'Str'} =~ s/^([^a-zA-Z0-9\(\)\,\.\:]+)//) { } 70 72 142
elsif ($self->{'Str'} =~ s/^([\(\)\,])//) { } 30 42 72
149 12 $self->{'TType'}="Func";
150 }
151 elsif ($self->{'Str'} =~ s/^([a-zA-Z][a-zA-Z0-9]*(:[a-zA-Z][a-zA-Z0-9]*)?)//) {
152 98 $self->{'TType'}="Var";
153 }
154 elsif ($self->{'Str'} =~ s/^(\d*\.\d+|\d+)//) {
155 14 $self->{'TType'}="Num";
156 }
157 elsif ($self->{'Str'}=~ s/^([^a-zA-Z0-9\(\)\,\.\:]+)//) {
158 70 $self->{'TType'}="OpChr";
159 }
160 elsif ($self->{'Str'}=~ s/^([\(\)\,])//){
161 30 $self->{'TType'}="Chr";
162 }
163 else {
164 0 ▶ if ($self->{'Str'} ne "") {$self->Bad}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Str"} ne "") 0 42 42
165 42 return 0;
166 }
167 224 $self->{'Token'}=$1;
168 224 return 1;
169 }
170  
171 63 ▶sub Expr {
Subroutine Expr: called CC=8 SCAR=20.9
Pod documented
172 63 my $self = shift;
173 63 my $e;
174 my $n;
175  
176 63 ▶ if ($self->{'Token'} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Token'} eq '-') { } 0 63 63
177 0 $e= new Math::Expr::Opp('neg');
178 0 $self->NextToken;
179 0 $e->SetOpp(0,$self->Elem);
180 } else {
181 63 $e=$self->Elem;
182 }
183  
184 63 ▶ while ($self->{'TType'} eq 'OpChr'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"TType"} eq "OpChr") 70 63 133
185 70 $n= new Math::Expr::Opp($self->{'Token'});
186  
187 70 ▶ if ($e->isa('Math::Expr::Opp') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa('Math::Expr::Opp') and defined $$Math::Expr::Pri{$e->{'Val'}} and defined $$Math::Expr::Pri{$n->{'Val'}} and $$Math::Expr::Pri{$e->{'Val'}} < $$Math::Expr::Pri{$n->{'Val'}} and $e->Breakable) { } 18 52 70
Condition91%11/12
$e->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$e->{"Val"}} and defined $$Math::Expr::Pri{$n->{"Val"}} and $$Math::Expr::Pri{$e->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}} and $e->Breakable
!ll&&!rl&&r
46618
$e->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$e->{"Val"}} and defined $$Math::Expr::Pri{$n->{"Val"}} and $$Math::Expr::Pri{$e->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}}
!ll&&!rl&&r
40624
$e->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$e->{"Val"}} and defined $$Math::Expr::Pri{$n->{"Val"}}
!ll&&!rl&&r
40030
$e->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$e->{"Val"}}
!ll&&!rl&&r
37330
MC/DC80%4/5
$e->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$e->{"Val"}} and defined $$Math::Expr::Pri{$n->{"Val"}} and $$Math::Expr::Pri{$e->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}} and $e->Breakable
$e->isa("Math::Expr::Opp") defined $$Math::Expr::Pri{$e->{"Val"}} defined $$Math::Expr::Pri{$n->{"Val"}} $$Math::Expr::Pri{$e->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}} $e->Breakable
Truth table83%5/6
A and B and C and D and E
A= $e->isa("Math::Expr::Opp")B= defined $$Math::Expr::Pri{$e->{"Val"}}C= defined $$Math::Expr::Pri{$n->{"Val"}}D= $$Math::Expr::Pri{$e->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}}E= $e->Breakable
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
188 defined $Pri->{$e->{'Val'}} &&
189 defined $Pri->{$n->{'Val'}} &&
190 $Pri->{$e->{'Val'}} < $Pri->{$n->{'Val'}} &&
191 $e->Breakable
192 ) {
193 18 $n->SetOpp(0,$e->Opp(1));
194 18 $self->NextToken;
195 18 $n->SetOpp(1,$self->Elem);
196 18 $n->Breakable(1);
197 18 $n=$self->FixPri($n);
198 18 $e->SetOpp(1,$n);
199 } else {
200 52 $n->SetOpp(0,$e);
201 52 $self->NextToken;
202 52 $n->SetOpp(1,$self->Elem);
203 52 $n->Breakable(1);
204 52 $e=$n;
205 }
206 }
207 63 $e->Breakable(0);
208 63 return $e;
209 }
210  
211 44 ▶sub FixPri {
Subroutine FixPri: called CC=6 SCAR=18.0
Pod undocumented
212 44 my ($self, $n)=@_;
213 44 my $a=$n->Opp(0);
214 44 my $t;
215  
216 44 ▶ if ($a->isa('Math::Expr::Opp') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($a->isa('Math::Expr::Opp') and defined $$Math::Expr::Pri{$n->{'Val'}} and defined $$Math::Expr::Pri{$a->{'Val'}} and $$Math::Expr::Pri{$a->{'Val'}} < $$Math::Expr::Pri{$n->{'Val'}} and $a->Breakable) { } 26 18 44
Condition83%10/12
$a->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$n->{"Val"}} and defined $$Math::Expr::Pri{$a->{"Val"}} and $$Math::Expr::Pri{$a->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}} and $a->Breakable
!ll&&!rl&&r
17126
$a->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$n->{"Val"}} and defined $$Math::Expr::Pri{$a->{"Val"}} and $$Math::Expr::Pri{$a->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}}
!ll&&!rl&&r
17027
$a->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$n->{"Val"}} and defined $$Math::Expr::Pri{$a->{"Val"}}
!ll&&!rl&&r
16127
$a->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$n->{"Val"}}
!ll&&!rl&&r
16028
MC/DC60%3/5
$a->isa("Math::Expr::Opp") and defined $$Math::Expr::Pri{$n->{"Val"}} and defined $$Math::Expr::Pri{$a->{"Val"}} and $$Math::Expr::Pri{$a->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}} and $a->Breakable
$a->isa("Math::Expr::Opp") defined $$Math::Expr::Pri{$n->{"Val"}} defined $$Math::Expr::Pri{$a->{"Val"}} $$Math::Expr::Pri{$a->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}} $a->Breakable
Truth table66%4/6
A and B and C and D and E
A= $a->isa("Math::Expr::Opp")B= defined $$Math::Expr::Pri{$n->{"Val"}}C= defined $$Math::Expr::Pri{$a->{"Val"}}D= $$Math::Expr::Pri{$a->{"Val"}} < $$Math::Expr::Pri{$n->{"Val"}}E= $a->Breakable
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
217 defined $Pri->{$n->{'Val'}} &&
218 defined $Pri->{$a->{'Val'}} &&
219 $Pri->{$a->{'Val'}} < $Pri->{$n->{'Val'}} &&
220 $a->Breakable
221 ) {
222 26 $n->SetOpp(0,$a->Opp(1));
223 26 $n=$self->FixPri($n);
224 26 $a->SetOpp(1,$n);
225 26 $a;
226 } else {
227 18 $n;
228 }
229 }
230  
231 133 ▶sub Elem {
Subroutine Elem: called CC=9 SCAR=23.3
Pod undocumented
232 133 my $self=shift;
233  
234 133 ▶ if ($self->{'TType'} eq "Var") {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->{'TType'} eq 'Var') { } 98 35 133
elsif ($self->{'TType'} eq 'Num') { } 14 21 35
elsif ($self->{'TType'} eq 'Var') { } 0 21 21
elsif ($self->{'Token'} eq '(') { } 9 12 21
elsif ($self->{'TType'} eq 'Func') { } 12 0 12
235 98 my $n = new Math::Expr::Var($self->{'Token'});
236 98 $self->NextToken;
237 98 return $n;
238 }
239 elsif ($self->{'TType'} eq "Num") {
240 14 my $n = new Math::Expr::Num($self->{'Token'});
241 14 $self->NextToken;
242 14 return $n;
243 }
244 elsif ($self->{'TType'} eq "Var") {
245 0 my $n = new Math::Expr::Var($self->{'Token'});
246 0 $self->NextToken;
247 0 return $n;
248 }
249 elsif ($self->{'Token'} eq "(") {
250 9 $self->NextToken;
251 9 my $n= $self->Expr;
252 9 ▶ if ($self->{'Token'} ne ")") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Token"} ne ")") 0 9 9
253 0 $self->Bad;
254 }
255 9 $self->NextToken;
256 9 return $n;
257 }
258 elsif ($self->{'TType'} eq "Func") {
259 12 my $n=new Math::Expr::Opp($self->{'Token'});
260 12 my $o=0;
261 do {
262 12 $self->NextToken;
263 12 $n->SetOpp($o, $self->Expr);
264 12 $o++;
265 12 ▶ } while ($self->{'Token'} eq ",");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Token"} eq "," and do { do { $self->NextToken; $n->SetOpp($o, $self->Expr); ++$o } } 0 12 12
266 12 ▶ if ($self->{'Token'} ne ")") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Token"} ne ")") 0 12 12
267 0 $self->Bad;
268 }
269 12 $self->NextToken;
270 12 return $n
271 } else {
272 0 $self->Bad;
273 }
274 }
275  
276 0 ▶sub Bad {
Subroutine Bad: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my $self = shift;
278
279 0 warn "Bad str: " . $self->{'Str'} . "\n";
280 }
281  
282 =head1 BUGS
283  
284 The parses does not handle bad strings in a decent way. If you try
285 to parse a string that does not follow the specification above, all
286 strange things might happen...
287  
288 =head1 AUTHOR
289  
290 Hakan Ardo <hakan@debian.org>
291  
292 =head1 SEE ALSO
293  
294 L<Math::Expr::Opp>
295  
296 =cut
« Expr/VarSet.pm ↑ Summary Expr/Rule.pm »