[ 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.