Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Generate a truth table from input string

by ikegami (Patriarch)
on May 13, 2008 at 16:43 UTC ( [id://686315]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Generate a truth table from input string
in thread Generate a truth table from input string

$str =~ s/'/!/g;
is not sufficient to convert a postfix operator to a prefix operator. "a'" produces the invalid code "1!".

Replies are listed 'Best First'.
Re^4: Generate a truth table from input string
by tachyon-II (Chaplain) on May 13, 2008 at 18:40 UTC
    $str =~ s/(\w+)'/!$1/g; $str =~ s/\(([^\)]+)\)'/!($1)/g;

      It you're going to allow (a+b)' — the OP might be happy with only supporting /\w+'/ — you must allow (a*(b+c))' too.

      Other than that, there are a few other problems. You don't handle xor, you assume the same precedence and associativity as Perl, and you don't generate a truth table. I presume that's intentional and that you're leaving those as an exercise to the reader, so I'm just documenting what's left to do.

        For your viewing pleasure.....

        while(<DATA>){ my ($re, $term, @vars, %vars, $loops); s/\*/ && /g; s/\+/ || /g; s/(\w+)\s*'/!$1/g; $re = qr{\((?:(?>[^()]+)|(??{$re}))*\)}; # :-) s/($re)\s*'/!$1/g; $term = $_; @vars = $_ =~ m/(\w+)/g; $vars{$_}++ for @vars; @vars = sort keys %vars; s/(\w+)/\$$1/g; printf "\n@vars = $term"; @vars = map {"\$$_"}@vars; $loops .= "for $_ (0..1) {\n" for @vars; $loops .= qq{printf "@vars = %d\n", eval;\n}; $loops .= "}" for @vars; do{ no strict 'vars'; eval $loops }; die "Can't process $term$_\n$@\n" if $@; } __DATA__ a*b*!(c) a'+b'+c' !(a*b) b^a (a^b)' (a+(a+b))'*c

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://686315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found