########################################################## # # Example of how to implement equalities in the Parser # DOCUMENT(); # This should be the first executable line in the problem. loadMacros( "PGbasicmacros.pl", "PGanswermacros.pl", "Parser.pl", "parserTables.pl", ); TEXT(beginproblem()); ########################################################## # # Define our own operator for equality # package Equality; our @ISA = qw(Parser::BOP); # subclass of Binary OPerator # # Check that the operand types are numbers. # sub _check { my $self = shift; my $name = $self->{bop}; $self->Error("Only one equality is allowed in an equation") if ($self->{lop}->class eq 'Equality' || $self->{rop}->class eq 'Equality') ; $self->Error("Operands of '$name' must be Numbers") unless $self->checkNumbers(); $self->{type} = Value::Type('Equality',1); # Make it not a number, to get errors with other operations. } # # Determine if the two sides are equal # sub _eval {return ($_[1] == $_[2])? 1: 0} package main; # # Add the operator into the current context # $prec = Context()->operators->get(',')->{precedence} + .25; Context()->operators->add( '=' => { class => 'Equality', precedence => $prec, # just above comma associativity => 'left', # computed left to right type => 'bin', # binary operator string => '=', # output string for it perl => '==', # perl string } ); ########################################################### # # The problem text # BEGIN_TEXT $BEGIN_ONE_COLUMN In this problem, we have added a new operator to the Parser: ${BTT} a = b${ETT}, for equality. $PAR \{ParserTable( 'Formula("x + y = 0")', 'Formula("x + y = 0")->{tree}->class', 'Formula("x + y = 0")->{tree}{lop}', 'Formula("x + y = 0")->{tree}{rop}', 'Formula("x + y = 0")->eval(x=>2,y=>3)', 'Formula("x + y = 0")->eval(x=>2,y=>-2)', 'Formula("x + y = 0 = z")', 'Formula("(x + y = 0) + 5")', 'Formula("x + y = 0, 3x-y = 4")', # you CAN get a list of equalities )\} $END_ONE_COLUMN END_TEXT ########################################################### ENDDOCUMENT(); # This should be the last executable line in the problem.