#!/usr/bin/perl # # Parse and evaluate boolean expressions # # 16 April 1998 M-J. Dominus (mjd-perl-py@plover.com) # require 'arith.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($_); $skip = 0; my $rc = yyparse(); next if $skip; if ($rc == 0) { my $val = $values[0]; print "The value of `$_' is $val.\n"; } else { print "`$_' caused a syntax error.\n"; } } sub error { warn "Runtime error `$_[0]' at line $. of input. Skipping.\n"; $skip = 1; }