Week 12: Exercises

1. Postfix Parser

As suggested in the lecture notes, write a function that can parse an arithmetic expression in postfix notation and return an expression tree.

2. Expressions with Variables

Extend the infix expression parser from the lecture so that an expression can contain variables, where every variable name is a lowercase letter. For example, here is one possible expression:

((2 + x) * (y – 7))

Also extend the eval() function so that it can evaluate an expression with variables, given a dictionary that maps each variable name to its value.

3. Expression Simplification

Extending the previous exercise, write a function simplify() that takes an expression tree and simplifies it by making the following replacements:

For example, simplifying the expression '((x * 1) – (y * 0))' will yield the expression 'x'.