COMS 4115 Programming Languages and Translators

Homework 1
Prof. Stephen A. Edwards
Due October 24, 2021 at 11:59 PM

All the problems ask you to use OCaml.  You may download the compiler
from ocaml.org.

Make sure your code compiles without warnings

Create a hw1.zip file containing your solutions (e.g., with the
Makefile) and upload it to Courseworks.

Do this assignment alone.  You may consult the instructor or a TA, but
not other students.

Cite any websites you use to help you answer these questions aside
from those for the class and ocaml.org.

1) Edit zip3.ml to create the zip3 function.  See zip3.ml for details.

2) Edit wordcount.mll to complete the word counter.
   See wordcount.mll for details

3) Edit ast.mli, calc.ml, parser.mly, and scanner.mll to add variables,
   assignment, conditionals, and sequencing to the four-function
   calculator example.

   For example,

   foo = 3; bar = baz = 6; foo * bar + baz

   should print 24.

   Use a String-to-integer hash table to track variable values, i.e.,
   
   module StringHash = Hashtbl.Make(String)
   let vals = StringHash.create 10

   Variable names should be sequences of one or more lowercase letters.

   "=" should denote variable assignment, with a variable name on
   the left and and expression on the right.  An assignment expression
   should return the value being assigned and associate right-to-left.

   ";" should denote sequencing of expressions and should be
   interpreted left-to-right.

   The expression

   "if" expr1 "then" expr2 "else" expr3
   
   should evaluate expr1 and, if non-zero
   evaluate expr2, otherwise evaluate expr3.

   ; should be at the lowest level of precedence, followed by if, =,
   + -, and * /, so

   baz = 2 ; foo = bar = 3 ; 3 * 2 + 4	

   should be interpreted as if it were parenthesized

   ((baz = 2) ; (foo = (bar = 3))) ; ((3 * 2) + 4)

   (Don't make your calculator accept parentheses)

   baz = 2 ; bar = if baz - 2 then 42 + 2 else 17 ; baz + bar

   should produce 19
   

The Makefile has recipes for compiling and running all three problems.