#!/usr/bin/perl use strict; # https://rosettacode.org/wiki/Arithmetic_evaluation use warnings; sub node { bless [ splice @_, 1 ], shift } sub error { die s/\G.*//sr =~ tr/\t/ /cr, "^ $_[0] !\n" } sub want { /\G$_[1]/gc ? shift : error pop } sub expr { /\G\h+/gc; my $tree = /\G\d+/gc ? node NUMBER => $& : /\G\(/gc ? want expr(0), qr/\)/, 'Missing Right Paren' : error 'Operand Expected'; $tree = /\G\h+/gc ? $tree : $_[0] <= 0 && /\G\+/gc ? node ADD => $tree, expr(1) : $_[0] <= 0 && /\G\-/gc ? node SUBTRACT => $tree, expr(1) : $_[0] <= 1 && /\G\*/gc ? node MULTIPLY => $tree, expr(2) : $_[0] <= 1 && /\G\//gc ? node DIVIDE => $tree, expr(2) : return $tree while 1; } sub ADD::value { $_[0][0]->value + $_[0][1]->value } sub SUBTRACT::value { $_[0][0]->value - $_[0][1]->value } sub MULTIPLY::value { $_[0][0]->value * $_[0][1]->value } sub DIVIDE::value { $_[0][0]->value / $_[0][1]->value } sub NUMBER::value { $_[0][0] } sub NUMBER::show { "$_[0][0]\n" } sub UNIVERSAL::show { ref($_[0]) . "\n" . join('', map $_->show, @{$_[0]}) =~ s/^/ /gmr } while( ) { eval { print; my $tree = want expr(0), "\n", 'Incomplete Parse'; print $tree->show, "value of tree = ", $tree->value, "\n\n"; } or print "$@\n"; } __DATA__ (1+3)*7 42 + ( 33 + ( 7 * 8 ) 123 456 foobar 7 / foobar 7 foobar 10 - 3 - 1 10 - (3 - 1) 2 * 3 + 4 * 5 2 + 3 * 4 + 5 ((((( 7 / 4 ))))) ((((( 7 / 4 )))))))) 2 + (3 + 4 no_names_allowed ) 7 ) 1 + 3 )