Programming Languages: Optional Homework Assignment 2

G22.2110






Notes: For those who did the first optional homework assignment, the best score between both assignments will be used to complement the mid-term grade.



This homework can be implemented indifferently in Java or C++.

Consider a simple language for arithmetic expressions, defined as:

  Exp ::= Int | Exp + Exp | Exp - Exp | Exp * Exp | Exp / Exp

Problem 1  

Represent each kind of arithmetic expression using classes and objects. For example, the expression (3+(5+6)) would be created by writing (in Java):


  new AddExp(new IntExp(3), new AddExp(new IntExp(5), new IntExp(6)))




Problem 2   Write an evaluator that takes such an expression, and computes its value (of type int). Particular care should be taken with division by 0.





This document was translated from LATEX by HEVEA.