Computer Science 301 - 2001


Solutions - Tutorial for week 15 - T-diagrams

(a) Consider the following general form of T diagram. Although it uses the letters A through I in the various arms of the T's, one could draw the diagram with fewer letters. How?

      ----------------------------          ----------------------------
      |                          |          |                          |
      |   A    ---------->  B    |          |   C   ----------->    D  |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |                          |          |
              |     E    |    F   -------->     G   |     H    |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |     I    |
                                 |          |
                                 ------------
Because of the rule that the "input" is semantically the same as the "output" it follows that we can draw this
      ----------------------------          ----------------------------
      |                          |          |                          |
      |   A    ---------->  B    |          |   A   ----------->    B  |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |                          |          |
              |     F    |    F   -------->     G   |     G    |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |     I    |
                                 |          |
                                 ------------


(b) A pretty printer is a program that will take the source code of a program written in some language and essentially simply reproduce it, but making it look "pretty", for example adjusting all the indentation to be consistent, placing each statement on a new line, adding blank lines between functions and so on.

Suppose you have available a C++ compiler CC.EXE for the PC, and that you have just been introduced to the language PGTC (Pat's Gift to Civilization). Use T diagrams to show in broad detail how you would (a) develop a pretty printer PRETTY.EXE for PGTC that would run (walk) on a Windows NT system in the Braae Laboratory, and (b) use this pretty printer on that NT system to beautify a messy program MESSY.PGTC written in PGTC.

We write the pretty-printer in C++ and compile it:

      ----------------------------          ----------------------------
      |         PRETTY.CPP       |          |        PRETTY.EXE        |
      |  PGTC  ---------->  PGTC |          |  PGTC ----------->  PGTC |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |          BCC.EXE         |          |
              |   C++    |   C++  -------->    8086 |    8086  |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------

Running the pretty-printer is depicted by

      ----------------------------          ----------------------------
      |        MESSY.PGTC        |          |        BETTER.PGTC       |
      |  DATA  ---------->  JUNK |          |  DATA ----------->  JUNK |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |       PRETTY.EXE         |          |
              |   PGTC   |  PGTC  -------->    PGTC |    8086  |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------

We can try to depict this all in one diagram as follows:

                         -----------------------            -----------------------
                         |     MESSY.PGTC      |            |    BETTER.PGTC      |
                         | DATA ------->  JUNK |            | DATA  ------>  JUNK |
                         |                     |            |                     |
                         ---------     ---------            ---------     ---------
  ----------------------------   |     |----------------------------|     |
  |         PRETTY.CPP       |   |PGTC ||        PRETTY.EXE        ||PGTC |
  |  PGTC  ---------->  PGTC |   |     ||  PGTC ----------->  PGTC ||     |
  |                          |   -------|                          |-------
  ---------          ----------------------------          ---------
          |          |         BCC.EXE          |          |
          |   C++    |   C++  -------->    8086 |    8086  |
          |          |                          |          |
          --------------------          --------------------
                             |          |       |          |
                             |   8086   |       |   8086   |
                             |          |       |          |
                             ------------       ------------
                             |          |
                             |   8086   |
                             |          |
                             ------------
Note that we are not using or writing a PGTC compiler here!

(c) Suppose that you were required to develop a C compiler for the newly invented Betta Mousetrap computer. Assume that you have available both the C source code and the corresponding executable version of a C compiler that will run on the Sadleigh Outdeight machine that you have somewhere in the spare room. Using T-diagrams, outline how you would develop the new compiler so that when you get a Betta Mousetrap for Christmas you will be able to plug your compiler in and have it working immediately.

This is an example of the classic "use a cross-compiler to do a half bootstrap" situation.

                         -----------------------            -----------------------
      These two are ---> |        CBM.C        |            |       CBM.BM        |
      the same   |       |   C  -------->  BM  |            |  C  -------->   BM  |
                 |       |                     |            |                     |
                 V       ---------     ---------            ---------     ---------
  ----------------------------   |     |----------------------------|     |
  |           CBM.C          |   |  C  ||          CBM.SO          || BM  |
  |   C    ---------->   BM  |   |     ||   C   ----------->   BM  ||     |
  |                          |   -------|                          |-------
  ---------          ----------------------------          ---------
          |          |          CSO.SO          |          |
          |    C     |   C    -------->     SO  |    SO    |
          |          |                          |          |
          --------------------          --------------------
                 ^           |          |       |          |
                 |           |    SO    |       |    SO    |
     This is produced by     |          |       |          |
     modifying the back      ------------       ------------
     end of the C -> SO      |          |
     compiler source         |    SO    |
                             |          |
                             ------------


(d) In the practical sessions you should have used the Extacy Modula-2 to C translator. This was developed in Russia by a team who used the JPI Modula-2 compiler available for the PC. But the demonstration system we downloaded from the Internet came with the file XC.EXE and a few other modules written in Modula-2 (but not the source of the XC executable itself). Draw T-diagrams showing the process the Russians must have used to produce this system, and go on to draw T-diagrams showing how you managed to take the program SIEVE.MOD and run it on the PC using the MicroSoft C++ system as your time-waster of choice.

The XC program was written in Modula-2 and compiled:

      ----------------------------          ----------------------------
      |           XC.MOD         |          |           XC.EXE         |
      |   M-2  ---------->   C   |          |  M-2  ----------->   C   |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |          JPI.EXE         |          |
              |    M-2   |   M-2  -------->    8086 |    8086  |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------

Using XC.EXE can be depicted as follows:

  ----------------------------          ----------------------------          ----------------------------
  |         SIEVE.MOD        |          |         SIEVE.C          |          |         SIEVE.EXE        |
  |   N    ----------> Primes|          |   N   -----------> Primes|          |   N   -----------> Primes|
  |                          |          |                          |          |                          |
  ---------          ----------------------------          ----------------------------          ---------
          |          |          XC.EXE          |          |          CL.EXE          |          |
          |    M-2   |   M-2  -------->     C   |     C    |    C   -------->   8086  |   8086   |
          |          |                          |          |                          |          |
          --------------------          ----------------------------          --------------------
                             |          |                          |          |       |          |
                             |   8086   |                          |   8086   |       |   8086   |
                             |          |                          |          |       |          |
                             ------------                          ------------       ------------
                             |          |                          |          |
                             |   8086   |                          |   8086   |
                             |          |                          |          |
                             ------------                          ------------