Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

[ Based on the code in Operator Associativity and Eliminating Left-Recursion in Parse::RecDescent. ]

First you need a parser. I used the same precedence and associativity as Perl uses.

make_parse.pl (Run once to create Parser.pm)

use strict; use warnings; use Parse::RecDescent qw( ); my $grammar = <<'__END_OF_GRAMMAR__'; { use strict; use warnings; } parse : <rulevar: local %vars> parse : expr /^\Z/ { [ $item[1], [ sort keys %vars ] ] } # Just an alias expr : or # vvv lowest precedence # or : or '+' and # | or '^' and # | and or : and or_[ $item[1] ] or_ : '+' <commit> and or_[ [ 'or', $arg[0], $item[3] ] ] | '^' <commit> and or_[ [ 'or', $arg[0], $item[3] ] ] | { $arg[0] } # and : and '*' not1 # | not1 and : not1 and_[ $item[1] ] and_ : '*' <commit> not1 and_[ [ 'and', $arg[0], $item[3] ] ] | { $arg[0] } # not : '!' not # | not "'" # | term not1 : '!' <commit> not1 { [ 'not', $item[3] ] } | not2 not2 : term not2_[ $item[1] ] not2_ : "'" <commit> not2_[ [ 'not', $arg[0] ] ] | { $arg[0] } # ^^^ highest precedence term : '(' <commit> expr ')' { $item[3] } | /\w+/ { $vars{$item[1]} = 0; [ 'var', $item[1] ] } __END_OF_GRAMMAR__ Parse::RecDescent->Precompile($grammar, 'Parser') or die("Bad grammar\n");

Then you can generate the truth tables from the parse tree.

table_truth.pl

use strict; use warnings; use Algorithm::Loops qw( NestedLoops ); use Data::Dumper qw( Dumper ); use Parser qw( ); { our %symtab; my %eval = ( var => sub { $symtab{$_[1]} }, not => sub { ( ~ eval_node($_[1]) ) & 1 }, and => sub { eval_node($_[1]) & eval_node($_[2]) }, or => sub { eval_node($_[1]) | eval_node($_[2]) }, xor => sub { eval_node($_[1]) ^ eval_node($_[2]) }, ); sub eval_node { my ($node) = @_; $eval{$node->[0]}->(@$node) } sub eval_expr { my ($tree, $vars, $vals) = @_; local %symtab; @symtab{ @$vars } = @$vals; eval_node($tree) } } my $parser = Parser->new(); foreach my $expr ( "a*b*!(c)", "a'+b'+c'", "!(A*B)", ) { my $results = $parser->parse($expr) or die("Bad expression $expr\n"); my ($tree, $vars) = @$results; print(join("\t", @$vars), "\t$expr\n"); my $i = NestedLoops([ ([0,1])x@$vars ]); while (my @vals = $i->()) { my $result = eval_expr($tree, $vars, \@vals); print(join("\t", @vals), "\t$result\n"); } print("\n"); }

Output

>make_parser.pl >truth_table.pl a b c a*b*!(c) 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 1 1 1 1 0 a b c a'+b'+c' 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0 A B !(A*B) 0 0 1 0 1 1 1 0 1 1 1 0

table_truth.pl could actually produce Perl code which you could eval instead of "executing" the tree itself, but executing the tree is quite simple.

Update: I had the precedence reversed.


In reply to Re: Generate a truth table from input string by ikegami
in thread Generate a truth table from input string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2021-06-23 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (123 votes). Check out past polls.

    Notices?