#!/usr/bin/perl
#
# Parse and evaluate boolean expressions
#
# 16 April 1998 M-J. Dominus (mjd-perl-py@plover.com)
#

require 'bool.pl';

# This auxiliary subroutine is invoked by the parser whenever it needs
# to compute the value of an atom.  It should return the correct
# value.  The way it presently does that is by consulting a hash,
# %atom_value, but you could do it any way at all.


# $yydebug = 1;

$yydebug = (shift() eq '-d');

# Main program
while (<>) {
  chomp;
  set_input($_);
  my $rc = yyparse();
  if ($rc == 0) {
    my $struct = $values[0];
    print "`$_' => ", to_string($struct), "\n";
  } else {
    print "`$_' caused a syntax error.\n";
  }
}

sub to_string {
  my $expr = shift;
  if (ref $expr) {
    my ($a1, $o, $a2) = @$expr;
    '(' . to_string($a1) . $o . to_string($a2) . ')';
  } else {
    $expr;
  }
}

sub true {
  $_[0] =~ /^[Tt1]$/;
}

sub false {
  $_[0] =~ /^[Ff0]$/;
}

sub constant {
  $_[0] =~ /^[Tt1Ff0]$/;
}
