in reply to Re: prefix notation in an array
in thread prefix notation in an array

I am sorry, the code that actually solves the outlined problem is the following. You can enter prefix boolean expressions in brackets ( "[and, a, [==, b, [!, c]]]" ).
After you entered the first valid expression, you may instead add new expressions as "</code>or, d" which will be applied to the previous expression. Entering "<code>!" negates the current expression.

use strict; use warnings; use Data::Dumper; use Parse::RecDescent; my $grammar = <<'GRAMMAR'; boolean_expr: '[' /and|or|==/ ',' boolean_expr ',' boolean_expr ']' { [ $item[2], $item[4], $item[6] ] } | '[' '!' ',' boolean_expr ']' { [ '!', $item[4] ] } | /and|or|==/ ',' boolean_expr { [ $item[1], $item[3] ] } | '!' | terminal { $item[1] } | <error> terminal: /\w+/ { $item[1] } GRAMMAR my $parser = Parse::RecDescent->new($grammar); my $old; while (<STDIN>) { chomp; my $parsed = $parser->boolean_expr($_); next unless defined $parsed; if (ref $parsed eq 'ARRAY' and @$parsed == 2 and $parsed->[0] =~ /and|or|==/ ) { warn("Cannot add clause if no " . "starting term was defined."), next if not defined $old; $old = [$parsed->[0], $old, $parsed->[1]]; } elsif ($parsed eq '!') { warn("Cannot add clause if no " . "starting term was defined."), next if not defined $old; $old = ['!', $old]; } else { $old = $parsed; } print Dumper $old; }
Update: Fixed line breaks for display.

Steffen