(a) Develop stack machine code equivalents of the following simple programs:
(1) void main () { // print numbers 0 through 10 for (int i = 0; i <= 10; i++) write(i, "\n") } (2) void main() { // factorials int n, f; read(n); write(n, "! ="); f = 1; while (n > 0) { f = f * n; n = n - 1; } write(f); } (3) void main () { // simple array handling int i = 1; int[] list = new int[11]; list[0] = 5; while (i <= 10) { list[i] = 2 - list[i-1]; i++; } }
(b) Consider the following general form of T diagram. Although it uses the letters A through I in the various arms of the Ts, one could draw the diagram with fewer letters. How and why?
┌──────────────────────────┐ ┌──────────────────────────┐ │ │ │ │ │ A ──────────> B │ │ C ───────────> D │ │ │ │ │ └───────┐ ┌───────┴──────────┴───────┐ ┌───────┘ │ │ │ │ │ E │ F ────────> G │ H │ │ │ │ │ └──────────┴───────┐ ┌───────┴──────────┘ │ │ │ I │ │ │ └──────────┘
(c) In the practical sessions you (should) have used the Parva2 translator. This was developed at Rhodes by using a compiler generator program called CoCo to produce its C# source code from a so-called "Attribute Grammar" written in a specification language called Cocol, which you will meet in a few weeks' time. This C# code was then compiled with the C# compiler available for the PC. Draw T-diagrams that describe this development.
┌──────────────────────────┐ ┌──────────────────────────┐ ┌─────────────────────────┐ │ │ │ │ │ │ │ ──────────> │ │ ──────────> │ │ ────────> │ │ │ │ │ │ │ └───────┐ ┌───────┴──────────┴───────┐ ┌───────┴─────────┴───────┐ ┌───────┘ │ │ Coco.exe │ │ │ │ │ Cocol │ Cocol ────────> │ │ ────────> │ │ │ │ │ │ │ │ └──────────┴───────┐ ┌───────┴──────────┴───────┐ ┌───────┴─────────┘ │ │ │ │ │ │ │ │ ├──────────┤ ├─────────┤ │ │ │ │ │ │ │ │ └──────────┘ └─────────┘