(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 Parva2ToCSharp 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 ────────> │ │ ────────> │ │ │ │ │ │ │ │ └──────────┴───────┐ ┌───────┴──────────┴───────┐ ┌───────┴─────────┘ │ │ │ │ │ │ │ │ ├──────────┤ ├─────────┤ │ │ │ │ │ │ │ │ └──────────┘ └─────────┘
(d) As mentioned in class, compiler writers have to be familiar with various data structures and the classes available for manipulating these. In the practical just completed you were expected to learn some of the uses of the IntSet class for handling simple sets, as well as the use of some of the IO library devised to support this course as simply as possible. Compilers often have to make use of searchable tables, and those tables are often interrogated by looking up identifiers. We shall shortly see this in action, but just to remind you of material you should (hopefully) have seen in CSC 201. here is a simple application of the Dictionary class available in C# which makes (hidden) use of a hash table to achieve quick lookups. Study the cade, and if you don't understand, come to ask about it, or do some look up on web pages for further details.
The idea here is that a table (a "dictionary") will be primed with simple pairs of course codes and the venues for the lectures associated with those codes; a table that can then be interrogated. Typical input to this program might read as below (for the moment ignore the possibility of bad errors):
CSC_102 Zoo_Major CSC_201 Geology_C11 CSC_301 Geology_C11 CHE_102 Barratt_2 stop end of table CSC_301 reports Geology_C11 BOT_102 reports not found stop end of session using Library; using System.Collections.Generic; class Venues { // Simple application of the generic Dictionary class in C# // Pat Terry, Rhodes University, 2017 class Entry { public string course; public string venue; public Entry(string course, string venue) { this.course = course; this.venue = venue; } // constructor public override string ToString() { return course + " is held in " + venue; } // ToString } // Entry static Dictionary<string, Entry> venueTable = new Dictionary<string, Entry>(); public static void Main (string[] args) { do { // Store the timetable string course = IO.ReadWord(); if (course.Equals("#######")) break; string venue = IO.ReadWord(); venueTable.Add(course, new Entry(course, venue)); } while (true); do { // Now lookup some venues string query = IO.ReadWord(); if (query.Equals("stop")) break; if (venueTable.ContainsKey(query)) IO.WriteLine(query + " is held in " + venueTable[query].venue); else IO.WriteLine(query + " not found - stop to exit"); } while (true); } // Main } // Venues