Question: Extend the WAE PLY implementation in include the following tpyes of expressions: Single With Expressions: These expressions allow the possibility of evaluating a WAE expression

Extend the WAE PLY implementation in include the following tpyes of expressions:

  1. Single With Expressions: These expressions allow the possibility of evaluating a WAE expression that contains variables, given values for these variables. Some valid "single with expressions" are shown below:
    {with {x 3} {+ x x}} should evaluate to 6 {with {x 3} {with {y 4} {+ x y}}} should evaluate to 7 {with {x {with {x 3} {+ x x}}} {* x x }} should evaluate to 36 
  2. Multi With Expressions: These generalize the single with expressions by allowing multiple variables to be assigned values in the same with expression. Some examples are:
    {with {{x 3} {y 4} {z 5}} {+ x {* y z}}} should evaluate to 23 {with {{x 3} {y 4} {z 5}} {+ {with {{x 10} {z 20}} x} {* {+ x y} z}}} should evaluate to 45 {with {{x 3} {y 4} {z 5} {y 3}} {+ x {* y z}}} # note this is syntactically valid # but should not be evaluated # (semantic error should be printed) 
    Submit the following files: WAE.py, WAELexer.py, WAEParser.py, and README

Here's the WAE.py file

from WAEParser import parser def eval_expression(tree): if tree[0] == 'num': return tree[1] elif tree[0] == 'id': return 'ERROR' elif tree[0] == '+' or tree[0] == '-' or tree[0] == '*' or tree[0] == '/': v1 = eval_expression(tree[1]) if v1 == 'ERROR': return 'ERROR' v2 = eval_expression(tree[2]) if v2 == 'ERROR': return 'ERROR' if tree[0] == '+': return v1+v2 elif tree[0] == '-': return v1-v2 elif tree[0] == '*': return v1*v2 elif v2 != 0: return v1/v2 else: return 'ERROR' else: #if clause v1 = eval_expression(tree[1]) if v1 == 'ERROR': return 'ERROR' if v1 != 0: return eval_expression(tree[2]) else: return eval_expression(tree[3]) def read_input(): result = '' while True: data = input('WAE: ').strip() if ';' in data: i = data.index(';') result += data[0:i+1] break else: result += data + ' ' return result def main(): while True: data = read_input() if data == 'exit;': break try: tree = parser.parse(data) except Exception as inst: print(inst.args[0]) continue #print(tree) try: answer = eval_expression(tree) if answer == 'ERROR': print(' EVALUATION ERROR ') else: print(' The value is '+str(answer)+' ') except: pass main()

Here's the instructions: Hopefully it makes sense. If you need the WAELexer and WAEParser file, I can provide you.

Here is a sample run of the program for a single-with expression:

macbook-pro:wae-singlewith ------$ python3 WAE.py

WAE: {with {x {with {x 3} {+ x x}}} {* x x }};

['with', ['x', ['with', ['x', ['num', 3.0]], ['+', ['id', 'x'], ['id', 'x']]]], ['*', ['id', 'x'], ['id', 'x']]]

The value is 36.0

The main program should have the following two functions:

eval_expression(tree); this is already present in the file I have given

This function takes the tree (a Python list, representing the WAE) returned by the parser as input and returns the values of the WAE.

substitute_var(var, val, tree)

This function replaces all occurrences of var by val within the scope in the WAE represented by tree and returns the modified tree

The strategy to evaluate a single-with expression { with {x e1} e2 } is as follows:

1. evaluate expression e1 to get its value, val

2. substitute x by val in e2 to get e3

3. evaluate expression e3 and return its value

The strategy to substitute is as follows

1. base case num; return num

2. base case ID; if var same as ID return val otherwise return ID

3. recursive cases: apply function to nested WAEs and reconstruct tree and return it

if the nested WAE is a single-with-expression and the single-with variable is same as var then scope rules dictate that no further substitution is needed, otherwise need to substitute in nested WAEs

Both these functions are mutually recursive!! Make sure base cases are covered and that the recursion moves towards termination on each subsequent call.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!