#!/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. sub atomvalue { my %atom_value = ('a' => 1, 'b' => 0, 'c' => 1); my $atom = shift; if (!exists $atom_value{$atom}) { warn "Unrecognized atom `$atom'; assuming false value.\n"; } $atom_value{$atom}; } # $yydebug = 1; $yydebug = (shift() eq '-d'); # Main program while (<>) { chomp; set_input($_); my $rc = yyparse(); if ($rc == 0) { my $truth = $values[0] ? 'true' : 'false'; print "The value of `$_' is $truth.\n"; } else { print "`$_' caused a syntax error.\n"; } }