CIS 425 Homework II

Grammars in ML; due 10/12

Here is a grammar for the abstract syntax of some simple arithmetic expressions, specified as a collection of ML datatypes:
(* binary operations *)
datatype BOp = Plus | Minus | Multiply | Divide 

(* unary operations: 
  Inverse is the function that divides 1 by its argument, Inverse 2 = 0.5
  Ln is the natural logarithm on your calculator
*)
datatype UOp = Inverse | Square | SquareRoot | Sin | Cos | Tan | Ln 

(* expressions: 
  Pi is the constant 3.1415926...
*)
datatype Expression =
    Pi
  | Number of real
  | Binary of Expression * BOp * Expression
  | Unary of UOp * Expression
Using the grammar, the abstract parse tree for the expression 3+4 is represented in ML as Binary(Number(3.0), Plus, Number(4.0)) :


Visited times since September 21, 1999 (or the last crash).

sabry@cs.uoregon.edu