Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^5: Generate a truth table from input string

by ikegami (Pope)
on May 13, 2008 at 18:43 UTC ( #686343=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^6: Generate a truth table from input string
by tachyon-II (Chaplain) on May 14, 2008 at 00:30 UTC

    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

      I'm a total noob and want to understand this code line by line.Can anyone help?


        it's not at all a noob level code: it's something like a grammar execution using eval

        Nowadays grammars are better implemented using Marpa::R2 but is not my field.

        Anyway using print YAPE::Regex::Explain and Data::Dumper all will become much clearer (I hope..):

        use Data::Dumper; use YAPE::Regex::Explain; print "regex explanation:\n"; print "\t",YAPE::Regex::Explain->new(qr{s/\*/ && /})->explain; print "\t",YAPE::Regex::Explain->new(qr{s/\+/ || /})->explain; print "\t",YAPE::Regex::Explain->new(qr{s/(\w+)\s*'/!$1/g})->explain; print "\t",YAPE::Regex::Explain->new(qr{\((?:(?>[^()]+)|(??{$re}))*\)} +)->explain; print "processing DATA:\n"; while(<DATA>){ chomp; # added by me print "\n\t-->received [$_]\n"; my ($re, $term, @vars, %vars, $loops); s/\*/ && /g; print "\t-->after first regex [$_]\n"; s/\+/ || /g; print "\t-->after second regex [$_]\n"; s/(\w+)\s*'/!$1/g; print "\t-->after third regex [$_]\n"; $re = qr{\((?:(?>[^()]+)|(??{$re}))*\)}; # :-) s/($re)\s*'/!$1/g; print "\t-->after fourth regex [$_]\n"; $term = $_; print "\t-->\$term is [$term]\n"; @vars = $_ =~ m/(\w+)/g; print "\t\@vars is [@vars]\n"; $vars{$_}++ for @vars; print "\t-->\%vars is :\n"; print "\t\t", Dumper \%vars; @vars = sort keys %vars; print "\t-->\@vars is [@vars]\n"; s/(\w+)/\$$1/g; print "\t-->\$_ is [$_]\n"; printf "\n@vars = $term"; @vars = map {"\$$_"}@vars; print "\n\t-->\@vars is [@vars]\n"; $loops .= "for $_ (0..1) {\n" for @vars; print "\t-->\$loops is [$loops]\n"; $loops .= qq{printf "@vars = %d\n", eval;\n}; print "\t-->\$loops is [$loops]\n"; $loops .= "}" for @vars; print "\t-->\$loops is [$loops]\n\n\n"; 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

        which output:


        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Just yesterday I mentioned how you can run your code under the perl debugger to see what's happening on a line by line basis. I encourage you to give it a try: it's faster than instrumenting your code with print statements, and it'll help you learn perl faster.


        When your only tool is a hammer, all problems look like your thumb.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2021-06-20 22:10 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (95 votes). Check out past polls.