Homework must be submitted via D2L. All submitted files (five for this assignment) must be submitted in the appropriate D2L directory in the drop box HW1. It is your responsibility to submit the homework in the proper format. This format includes the following. Each file you submit should have the following information on the first line as a comment (in the appropriate format for that kind of file). This format is required for this homework, and all homework throughout the quarter.
For example in a Haskell file
-- Homework 1 Tom Smith tom_smith@gmail.comor in a Python file
# Homework 1 Tom Smith tom_smith@gmail.comor in an *.e1 file
{- Homework 1 Tom Smith tom_smith@gmail.com -}
All programs mentioned can be downloaded from the this document
Consider the programs Hw1A.py and Hw1A.hs. Each defines a function that takes a string parameter "w" as input, and writes the string: "Hello w!" to standard output.
Each program can be run within the language's interactive read-eval-print loop, by calling "hi" with a string argument, or by executing the program from the command line, where the first command line argument is used as the input to "hi".
code> python3 Python 3.2.3 (default, Sep 25 2013, 18:22:43) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Hw1A >>> Hw1A.hi("Steve") Hello Steve!
code> python3 Hw1A.py Justin Hello Justin!
code>python HW1A.py Sam Hello Sam!
code> chmod +X Hw1A.py code> ./Hw1A.py Zane Hello Zane!
code> ghci Hw1A.hs GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Ok, modules loaded: Main. Prelude Main> hi "Bill" Hello Bill!
code> runhaskell Hw1a.hs Tommy Hello Tommy!
code> ghc --make Hw1a.hs Linking Hw1a ... code> ./Hw1a Mary Hello Mary!
code>ghc --make Hw1A.hs Linking Hw1A.exe ... code>Hw1A.exe Jane Hello Jane!
Your task is to alter each of these programs so that it reverses the characters in w before doing its output.
For example, if the input string is Andrew, the output of the altered program should be Hello werdnA!.
Programs should have a function "hi" with single string parameter "w" just as before. Your extended programs should be called sol1A.py and sol1A.hs Respectively. (5 points each).
Hint: Before you try to take the string apart by hand, look at the functions available on strings and lists in the standard libraries of the two languages. Submit (just) the files sol1A.py and sol1A.hs.
Consider a very simple language of expressions, which we'll call E1 Its abstract syntax is given by the following tree grammar:
Prog : Program -> Exp Add : Exp -> Exp Exp Sub : Exp -> Exp Exp Mul : Exp -> Exp Exp Div : Exp -> Exp Exp Int : Exp -> (int)
To actually read and write programs in the language, we'll use a LISP-style concrete syntax that maps directly to the abstract syntax, specified by the following grammar:
exp := int | '(' '+' exp exp ')' | '(' '-' exp exp ')' | '(' '*' exp exp ')' | '(' '/' exp exp ')' int := digit | digit intThis problem asks you to modify interpreters for E1, written in Python and Haskell, living in files called Hw1B.py and Hw1B.hs. respectively. These interpreters read a file containing an E1 program in the LISP-style concrete syntax described above and evaluate the expression, producing an integer result.
The interpreters are primarily designed to be executed from the shell command line, passing the filename to be interpreted as an command-line argument, e.g., python3 Hw1B.py foo.e1 or runhaskell Hw1B.hs foo.e1.
It is also possible to execute the interpreters from within the read-eval-print loop, as discussed in Part A of this assignment by starting the interpretor and then typing Interp.interp "foo.e1";; in Python, or interp "foo.e1" in Haskell.
For example, if foo.e1 contains the following text:
(/ {- Integer division rounds results down -} (+ 7 (- 0 2)) {- Here's how to make a negative number -} 3)
then both interpreters should return the answer 1.
Note that programs can be broken arbitrarily across multiple lines. Any text within comment braces ( {- ... -} ) will be treated as a comment.
Here are your tasks:
Notice that both Haskell and Python have a library function that implements remainder (% in Python) (rem in Haskell), but they behave differently when given negative numbers, but that should not stop you from implementing % as specified here.
Note: The Haskell version does not evaluate the program AST directly; instead, it compiles it into a sequence of stack machine instructions, and then executes the resulting stack machine program.
In adding support for the remainder operator, you must not change the instruction set or implementation of the stack machine Implementation.
Hint: Look at the code for an existing binary operator, e.g., +. In the Python version, you need to add a new class for % expressions. In the Haskell version, you will need to add a new constructor to the Exp datatype and corresponding clauses to functions that match over that type using case statements.
In each, you'll need to make two small additions to the lexer/parser.
Put your Python solution into a file sol1B.py and put your Haskell solution into a file sol1B.hs.
Submit these two files. (30 points each).
Some hints about altering the Haskell program