#!/usr/bin/perl use warnings; use strict; for my $given ( "(A & B)'", "((A & B)' | (A & C & (A & B & D)'))", "((A | B)' & (C | (D & (E & F)))')'", "((A & B)' | (A & C & (A & B & D | (A & (B' & D)')'))" ) { my @left; my $calc = $given; my $last; for my $pos (0 .. length($given) - 1) { my $current = substr $given, $pos, 1; push @left, $pos if '(' eq $current; $last = pop @left if ')' eq $current; if ("'" eq $current) { substr $calc, $pos, 1, q(); if (')' eq substr $calc, $pos - 1, 1) { # Negated parentheses. substr $calc, $last, 0, '!'; } else { # Negated literal. substr $calc, $pos - 1, 0, '!'; } } } print "Given: $given\n"; print "Calculated: $calc\n"; }