Prog Lang Prin    Home Work 

Section 3

week 1

1.1   Write a recursive version of "strcpy" in NUT.
1.2   Does Python language use a compiler?  (or an interpreter).  How it is executed?

week 2

2.1   Try the scanner (test-tok.txt), compile it and run it.  See the output.  Modify the output so that each token is printed on a separated line.  (use the function (nl) instead of (space))
2.2   Write a grammar of Nut language in the form of "rail-road-diagram".  There is a grammar in the Nut language manual (ch2.pdf) but you should try to write it by yourself.

week 3

3.1   Try compiling the nut-in-nut compiler (this one nut2.txt).  Use it to compile some example such as ex-ch2.txt  .  Observe what happen on the screen, try to describe it.  You can redirect the output from the screen (which is the object code) to a file, let's name it "sq.obj".  This object can be executed using "nsim32 sq.obj".  Try it and see the result.  Is it what you expect?
If you forget how to do nut-compiling see the section
Session on how to use the tools .
3.2   Assume we have a function "average", if it is a user-define function like this (file "avg.txt"):
    (def average (a b) ()
      (/ (+ a b) 2))
    (def main () ()
      (average 5 3))

The output at the screen (compile "avg.txt"):
E:\prabhas\bag\teaching\proglang\2010\nut\compiler> nut32 < avg.txt
average
(fun.2.2 (/ (+ get.1 get.2 )lit.2 ))
main
(fun.0.0 (call.18 lit.5 lit.3 ))
What change you have to make to the compiler (at the pseudo code level) to make "average" to be the built-in operator?  You need to read the compiler to understand this.  Read:
  parseNL    (parse Name List)
  parseName
  parseEL    (parse Exp list)
  parseExp
  parse
Don't worry about parseEnum, parseLet, we won't touch it. If you read the actual code, here is my coding style, to do a "switch case" I did a "cascade if":
  if case1  do1
  if case2  do2
  if ...
  ; else
  doDefault

You will find this assignment a bit confusing because it really change at the code-generator level which we will learn next week. However, read the compiler and write some report of your understanding.  If "average" is really a built-in operator the "main" function in the above example will be compiled into this (notice the difference):
main
(fun.0.0 (average lit.5 lit.3 ))

End