-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPNtool.pl
executable file
·498 lines (458 loc) · 9.08 KB
/
RPNtool.pl
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/perl
#############################################################################
#
# Script: RPNtool.pl
# Author: Chris Vitalos
# Description: Using this script you can :
# - Convert an Infix expression to Postfix
# - Convert a Postfix expression to Infix
# - Evaluate a Postfix expression
# - Evaluate an Infix expression
# Web: www.vitalos.us
# E-mail: cvitalos at yahoo dot com
# Created: 23 April 2020
#
# � 2020 Chris Vitalos. All rights reserved.
#
############################################################################
use strict;
use warnings;
use 5.10.0;
use Data::Dumper;
use Getopt::Std;
#
# sub isOperand
# returns a boolean if the input char is a math operand e.g. number
#
sub isOperand
{
my ($who)=@_;
if((!isOperator($who)) && ($who ne "(") && ($who ne ")") && !isFunction($who))
{
return 1;
}
else
{
return 0;
}
}
#
# sub isOperator
# returns a boolean if the input char is a recognized math operator
#
sub isOperator
{
my ($who)=@_;
if(($who eq "+") || ($who eq "-") || ($who eq "*") || ($who eq "/") || ($who eq "^"))
{
return 1;
}
else
{
return 0;
}
}
#
# sub isFunction
# returns a boolean if the input string is a recognized math function
#
sub isFunction
{
my ($who)=@_;
if(($who =~ /sin/i) || ($who =~ /cos/i) || ($who =~ /tan/i) || ($who =~ /sqrt/i) || ($who =~ /log/i) || ($who =~ /ln/i) || ($who =~ /exp/i))
{
return 1;
}
else
{
return 0;
}
}
#
# sub topStack
# essentially a stack peek() function
# returns the value at the top of the FILO stack
#
sub topStack
{
my (@arr)=@_;
my $arr_len=@arr;
return($arr[$arr_len-1]);
}
#
# sub isEmpty
# returns boolean indicating if input list is empty (or not)
#
sub isEmpty
{
my (@arr)=@_;
my $arr_len=@arr;
if(($arr_len)==0)
{
return 1;
}
else
{
return 0;
}
}
#
# sub prcd
# used to facilitate infix to RPN processing
# returns a priority assigned to an operator, or function
#
sub prcd
{
my ($who)=@_;
my $retVal;
if($who eq "~")
{
$retVal="50";
}
elsif($who eq "^")
{
$retVal="50";
}
elsif(($who eq "*") || ($who eq "/"))
{
$retVal="40";
}
elsif(($who eq "+") || ($who eq "-"))
{
$retVal="30";
}
elsif($who eq "(")
{
$retVal="20";
}
elsif(isFunction($who))
{
$retVal="20";
}
else
{
$retVal="10";
}
return($retVal);
}
#
# sub genArr
#
# this is the input string parser
# returns a list object of tokens
#
sub genArr
{
my ($who)=@_;
my @whoArr;
my $i;
my $temp;
my $pc=0;
my $l = length($who);
$who=~tr/ //d; #remove all whitespace from input string;
# say "Parsing $who";
for($i=0; $i<=$l; $i++)
{
$who=~m/\A\-{1}/;
if(defined($&))
{
if($i==0||($i>0&&!isOperand($whoArr[$i-1])))
{
$whoArr[$i]="~"
}
else
{
$whoArr[$i]="-";
}
$who=$';
# say "unary binary minus check: token=$whoArr[$i] remains=$who";
next;
}
$who=~m/\A([0-9]+|\.)+/;
if(defined($&))
{
$whoArr[$i]=$&;
$who=$';
if(defined($whoArr[$i-1]))
{
if($whoArr[$i-1] eq "~")
{
$temp=$whoArr[$i-1]; $whoArr[$i-1]=$whoArr[$i]; $whoArr[$i]=$temp;
}
}
# say "numeral check: token=$whoArr[$i] remains=$who";
next;
}
$who=~m/\A(\+|\*|\/|\^){1}/;
if(defined($&))
{
$whoArr[$i]=$&;
$who=$';
# say "operator check: token=$whoArr[$i] remains=$who";
next;
}
$who=~m/\A(\(|\)){1}/;
if(defined($&))
{
$whoArr[$i]=$&;
$who=$';
$pc++ if ($& eq "("); $pc-- if ($& eq ")");
# say "parenthesis check: token=$whoArr[$i] remains=$who";
next;
}
$who=~m/\A[A-Z|a-z]+\(/;
if(defined($&))
{
$whoArr[$i]=$&; $whoArr[$i]=~tr/\($//d;
$who=$'; $pc++;
die "Unrecognized function $&" if (!isFunction($&));
# say "function check: token=$whoArr[$i] remains=$who";
next;
}
}
die "Unbalanced parentheses in input string!" if ($pc != 0);
# say Dumper @whoArr;
# say "Done";
return(@whoArr);
}
#
# sub InfixToPostfix
#
# this performs the Shunting Yard Algorithm on input infix math string
# returns a list object representing a RPN (postfix) math expression
#
sub InfixToPostfix
{
my ($infixStr)=@_;
my @infixArr=genArr($infixStr);
my @postfixArr;
my @stackArr;
my $postfixPtr=0;
my $i;
# return (); # Uncomment to debug parser sub genArr
for($i=0; $i<scalar(@infixArr); $i++)
{
if(isOperand($infixArr[$i]))
{
$postfixArr[$postfixPtr]=$infixArr[$i];
$postfixPtr++;
}
if(isOperator($infixArr[$i]))
{
if($infixArr[$i] ne "^")
{
while((!isEmpty(@stackArr)) && (prcd($infixArr[$i])<=prcd(topStack(@stackArr))))
{
$postfixArr[$postfixPtr]=topStack(@stackArr);
pop(@stackArr);
$postfixPtr++;
}
}
else
{
while((!isEmpty(@stackArr)) && (prcd($infixArr[$i])<prcd(topStack(@stackArr))))
{
$postfixArr[$postfixPtr]=topStack(@stackArr);
pop(@stackArr);
$postfixPtr++;
}
}
push(@stackArr,$infixArr[$i]);
}
if($infixArr[$i] eq "(" || isFunction($infixArr[$i]))
{
push(@stackArr,$infixArr[$i]);
}
if($infixArr[$i] eq ")")
{
while(!isEmpty(@stackArr))
{
last unless (topStack(@stackArr) ne "(");
$postfixArr[$postfixPtr]=pop(@stackArr);
$postfixPtr++;
}
pop(@stackArr);
}
}
while(!isEmpty(@stackArr))
{
if(topStack(@stackArr) eq "(" )
{
pop(@stackArr)
}
else
{
my $temp=@postfixArr;
$postfixArr[$temp]=pop(@stackArr);
}
}
return(@postfixArr);
}
#
# the following are collection of RPN post fix to infix processing routines
# remains untested
#
sub PostfixToInfix
{
my ($postfixStr)=@_;
my @stackArr;
my @postfixArr=genArr($postfixStr);
my $i;
my $temp;
my $pushVal;
say Dumper $postfixStr;
for($i=0; $i<length($postfixStr); $i++)
{
if(isOperand($postfixArr[$i]))
{
push(@stackArr,$postfixArr[$i]);
}
else
{
$temp=topStack(@stackArr);
pop(@stackArr);
$pushVal=topStack(@stackArr).$postfixArr[$i].$temp;
pop(@stackArr);
push(@stackArr,$pushVal);
}
}
return((@stackArr));
}
sub PostfixEval
{
my ($postfixStr)=@_;
my @stackArr;
my @postfixArr=genArr($postfixStr);
my $i;
for($i=0; $i<length($postfixStr); $i++)
{
if(isOperand($postfixArr[$i]))
{
push(@stackArr,$postfixArr[$i]);
}
else
{
my $temp=topStack(@stackArr);
pop(@stackArr);
my $pushVal=PostfixSubEval(topStack(@stackArr),$temp,$postfixArr[$i]);
pop(@stackArr);
push(@stackArr,$pushVal);
}
}
return(topStack(@stackArr));
}
sub PostfixSubEval
{
my ($num1,$num2,$sym)=@_;
my $returnVal;
if($sym eq "+")
{
$returnVal=$num1+$num2;
}
if($sym eq "-")
{
$returnVal=$num1-$num2;
}
if($sym eq "*")
{
$returnVal=$num1*$num2;
}
if($sym eq "/")
{
$returnVal=$num1/$num2;
}
if($sym eq "^")
{
$returnVal=$num1**$num2;
}
return($returnVal);
}
sub joinArr
{
my (@who)=@_;
my $who_len=@who;
my $retVal;
my $i;
for($i=0; $i<$who_len; $i++)
{
$retVal.=$who[$i];
}
return $retVal;
}
sub evalInfix
{
my ($exp)=@_;
return PostfixEval(joinArr(InfixToPostfix($exp)));
}
#
# sub printInfixtoPostfix
#
# pretty prints a RPN math expression given an input infix math expression
#
sub printInfixtoPostfix
{
my (@result) = InfixToPostfix(@_);
say join(" ",@result);
}
#
# sub printInfixtoPostfix
#
sub printHelp
{
say "RPNtool.pl: math infix to RPN tool .. and back ..";
say "Invoke as : RPNtool.pl -t -r infix_expression";
say " -t (runs built in test cases)";
say " -r infix_expression (convert to RPN expression)";
}
#
# sub testCase
#
# executes a test case given the input testString (math expression)
# compares the observed result against passed expected result
# (hardcoded for Infix to Postfix RPN testing)
# prints pass/fail, testString, observed result
#
sub testCase
{
my ($testString, $expectedResult) = @_;
my @observedResult=InfixToPostfix($testString);
my $result = ($expectedResult eq join(" ",@observedResult)) ? "PASSED" : "FAILED";
say "$result : testString=($testString) expectedResult = ( $expectedResult ) observedResults = ( @observedResult )";
}
#
# sub runTestCases
#
# this is a collection of test cases
#
sub runTestCases
{
testCase( "1+2" , "1 2 +" );
testCase( "1 + 2" , "1 2 +" );
testCase( "1*(2^(3+4))" , "1 2 3 4 + ^ *" );
testCase( "1*2^(3+4)" , "1 2 3 4 + ^ *" );
testCase( "sin(1+2^4)" , "1 2 4 ^ + sin" );
testCase( "1+sin(2*4)" , "1 2 4 * sin +" );
testCase( "1*cos(6^9)" , "1 6 9 ^ cos *" );
testCase( "1*cos(6^9)+sin(3/4)" , "1 6 9 ^ cos * 3 4 / sin +" );
testCase( "1/log(cos(2*3))" , "1 2 3 * cos log /" );
testCase( "-1*sin(-10)" , "1 ~ 10 ~ sin *");
testCase( "5-1" , "5 1 -");
testCase( "-5.1*5.3" , "5.1 ~ 5.3 *");
testCase( "1.7-3.14" , "1.7 3.14 -");
testCase( "1.9^-2.73" , "1.9 2.73 ~ ^");
testCase( "cos(-14)" , "14 ~ cos");
testCase( "5*-1*cos(-30)" , "5 1 ~ * 30 ~ cos *");
}
#
# sub main
#
sub main
{
my %options=();
getopts("hr:t", \%options);
runTestCases if (defined($options{t}));
printInfixtoPostfix($options{r}) if (defined($options{r}));
printHelp if (defined($options{h}));
exit 0;
}
main();