Laboratory exercises under Windows (Imperative Programming)

From Spivey's Corner
Jump to: navigation, search

All these notes were prepared using Windows XP running in VirtualBox – Mike

Lab 1

The unix programs uniq and awk that are used in the last part of the lab are not provided as part of Windows, but there is a program called sort that is rather more restricted than the unix version. You can complete the lab exercise under Windows by using the pipeline,

crack2 n private | sort | tally

where n is the key length deduced in an earlier part of the lab, and tally is an Oberon program whose source code follows.



  line, prev: ARRAY 80 OF CHAR;
  count: INTEGER;

  IF count > 1 THEN 
    Out.Int(count, 7); 
    Out.Char(' '); 
END Print;

  count := 0;
    IF ~In.Done THEN Print; EXIT END;

    IF (count = 0) OR (line # prev) THEN
      prev := line;
      count := 0

    count := count+1
END Tally.

Lab 2

To get the tester script to work under Windows, you'll need to install an implementation of TCL, the language in which the test script is written. There's a free implementation that can be downloaded from After using the installer, you may need to reboot your machine so that the appropriate directory appears on your path.

Now the command to run the tester script is something like:

tclsh85 tester 1

where tclsh85 is the name of the TCL interpreter you just installed.

Here's a transcript of a Command Window session:

C:\Documents and Settings\mike\ip1\lab2>dir
 Volume in drive C has no label.
 Volume Serial Number is 8C98-6309

 Directory of C:\Documents and Settings\mike\ip1\lab2

24/02/2010  12:29    <DIR>          .
24/02/2010  12:29    <DIR>          ..
17/12/2008  00:32               674 grep.hs
17/12/2008  00:32             3,505 MGrep.m
17/12/2008  00:32             2,801 tester
               3 File(s)          6,980 bytes
               2 Dir(s)  44,180,574,208 bytes free

C:\Documents and Settings\mike\ip1\lab2>obc -o mgrep MGrep.m

C:\Documents and Settings\mike\ip1\lab2>tclsh85 tester 1
test 'x' '' -- OK (status 1)
test 'x' 'x' -- OK (status 0)
test 'x' 'aaaa' -- OK (status 1)
test 'x' 'xaaa' -- OK (status 0)
test 'x' 'aaxa' -- failed
*** wrong exit status 1 (match failed)
*** expected status 0 (match succeeded)
test 'x' 'aaax' -- failed
*** wrong exit status 1 (match failed)
*** expected status 0 (match succeeded)

Actually, the supplied version of mgrep matches lines that begin with an x, regardless of the pattern supplied on the command line. This is normal for the beginning of the lab exercise.

Lab 3

Windows lacks the sed, cat, sort and join programs that make up the nuts and bolts of the Poor Man's Diff script. Nevertheless, you can prepare the UpSeq program under Windows, even if testing its use as the brains of diff requires you to move it to a unix system.

Actually, it's possible to get the pmdiff script working under Windows by rewriting it as a batch file. First, you need to install some GNU utilities:

and make sure the directory C:\Program Files\GnuWin32\bin appears on your path. Then use the following batch file pmdiff.bat:

echo **EOF** | cat -n %1 - | sed "s/^ *//" | sort -k 2 >tmpa
echo **EOF** | cat -n %2 - | sed "s/^ *//" | sort -k 2 >tmpb
join -t "	" -j 2 -o 1.1,2.1 tmpa tmpb | gsort -k 1,1n -k 2,2nr >tmpc
upseq <tmpc | diffout %1 %2

(The character between " " on the third line must be a TAB.)

This script won't deal properly with files that contain TAB characters, but is good enough to complete the lab exercise. But then again, if you're going to install some bits of unix, you may as well go the whole hog!