Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the eval() function for evaluating expressions of the simple boolex language (cf. Lab 4): def eval(n): if n == 't': return True if n
Consider the eval() function for evaluating expressions of the simple boolex language (cf. Lab 4): def eval(n): if n == 't': return True if n == 'f': return false if len(n) == 2: assert n[0] == 'not, "expected 'not', got " + n[0] return not eval(n[1]) # otherwise, n is a binop op = n[0] left = eval(n[1]) right = eval(n[2]) if op == 'and': return left and right if op == 'or': return left or right if op == 'xor': return left and not right or not left and right if op == 'eq': return left == right else: raise Exception("expected an boolean op, got " + op) (a) For each binary operation, can you tell if both operands are always evaluated? If so, is the evaluate left-to-right or right-to-left? (b) Modify the program, so that the operand evaluation order is reversed. (You don't need to reproduce the program; just describe where and what changes are to be made.) (c) Now we want to implement the "short-circuit" evaluation semantics (i.e. skipping the evaluation of the right operand if the binary operation's result can be decided with left operand's value). Modify the program to achieve this semantics. Consider the eval() function for evaluating expressions of the simple boolex language (cf. Lab 4): def eval(n): if n == 't': return True if n == 'f': return false if len(n) == 2: assert n[0] == 'not, "expected 'not', got " + n[0] return not eval(n[1]) # otherwise, n is a binop op = n[0] left = eval(n[1]) right = eval(n[2]) if op == 'and': return left and right if op == 'or': return left or right if op == 'xor': return left and not right or not left and right if op == 'eq': return left == right else: raise Exception("expected an boolean op, got " + op) (a) For each binary operation, can you tell if both operands are always evaluated? If so, is the evaluate left-to-right or right-to-left? (b) Modify the program, so that the operand evaluation order is reversed. (You don't need to reproduce the program; just describe where and what changes are to be made.) (c) Now we want to implement the "short-circuit" evaluation semantics (i.e. skipping the evaluation of the right operand if the binary operation's result can be decided with left operand's value). Modify the program to achieve this semantics
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started