algol w

2 posts

ALGOL W - Language features

Let's look at ALGOL W in more detail, focusing on the differences compared to ALGOL 60.


ALGOL W is free format and case insensitive, though the convention was to have keywords in lower case and identifiers Like_This. Identifiers can be up to 255 characters long.

Keywords are reserved words so no stropping is required. A standard EBCDIC character set is used so there is no need for a different reference and hardware representation.

As well as the comment syntax from ALGOL 60, comments can be started with % and then terminated with either % or ;.

The final statement in a program or separately compiled unit (ie the last end) should end with a period.

% This is an ALGOL W program %
    integer Population_Count;

New data types

As well as the existing types from ALGOL 60 such as real and integer, ALGOL W provides long real, complex, bit and proper string types:

    complex C;
    bits B;
    string(50) S;
    c := 1 + 2I;
    b := #0000010F;
    s := "Hello, world";

Bit values are padded to 32 bits. String variables have to be declared with their length, such as 50 above; if the length is not provided the default is 16.

New operators

div does integer division and rem yields the remainder after division. As well as the logical operators like and, there are also shl and shr for bitwise left and right shifts.

    write(#1 shl 2);
    write(42 rem 20);

The above will print #00000004 and 2.

Records and references

Records group together simple variables like a struct in C. References can point to records but not fundamental types. A reference can be initialised, at which point memory for the record is allocated. After initialisation, fields can be accessed by field(variable).

    record Person(integer Age; real Height; string(40) Name);
    reference(Person) P, Q;
    P := Person(30,, "N Wirth");
    Q := Person;
    Age(Q) := 42;

Note for the initialisation of P we leave Height uninitialised, and although memory for Q is allocated it is not initialised at all. This example will print "N Wirth" and 42.

References can be used to point at records but not fundamental types. null can be used to refer to an non-existent record. So a simple linked list could be defined as below, which will print 20.

    record List(integer Data; reference(List) Next);
    reference(List) L;
    L := List(10, null);
    Next(L) := List(20, null);

A reference can point to more than one type of record; which one can be determined at run time with the is operator.

    record A(real Aa);
    record B(integer Bb);
    reference(A, B) R, S;
    R := A(3.0);
    S := B(4);
    S := R;
    if S is A then write("Got an A") else write("Got a B");

The above will print "Got an A". I think there is no garbage collector, so the memory allocated for the B is leaked.

while and case

As well as the three types of for statements from ALGOL 60, ALGOL W has a while statement. The below will print 256.0

    real X;
    X := 2.0;
    while X < 100 do
        X := X * X;

case allows switching by integer value; the below will print "Two". Note there is no default case, so if J < 1 or > 4 there will be a runtime error.

    integer J;
    string(10) Word;
    J := 2;
    Word := case J of ("One", "Two", "Three", "Four");


The syntax for procedures differs from ALGOL 60 as types and calling conventions are specified in the function header:

    real procedure Fn(real X; integer value Y);
        X * Y
    write(Fn(3.0, 2));

The default calling convention, as used in the above, is by name, but *ALGOLW will give you a warning suggesting you use another convention instead.

Putting value after the type means pass by value. There is a new calling convention, result, which is for pass-out variables. value results allows both pass-in and pass-out without the side effects of pass by name.

    procedure P1(integer value A; integer result B; real value result C);
        A := 10;
        B := A;
        C := C + A

    integer A, B;
    real C;
    A := 1; B := 2; C := 3.0;

    P1(A, B, C);
    write(A, B, C);

The above will display 1, 10, 13.

Input/Output and library

There's an extensive input/output library that has been customised for MTS.

Basic input/output allows reading and writing each from a single channel. The below program will expect input formatted like this:

1001 "Niklaus" "Wirth"  
Inventor of ALGOL W  
    integer ID;
    string(20) First, Last;
    string(80) Description;

    readon(First, Last);

More complex formatting and carriage control can be specified. There is also support for reading/writing to more than one channel, and indexed access.

The standard library contains mathematical functions and access to operating system information like time and date.

Further information

MTS volume 16 describes the language and the MTS extensions in detail.

Another introduction to the language can be found at everything2.

ALGOL W - Introduction

As discussed in the previous post, there were discussions on how to extend ALGOL after the experience of implementing ALGOL 60 on different systems. One such proposal was formulated by Niklaus Wirth and C A R Hoare. It did not get approved by the ALGOL committee, who chose what eventually became ALGOL 68 instead, so Wirth developed his proposal at Stanford and released it as ALGOL W.

ALGOL W overview

ALGOL W builds upon ALGOL 60 rather than making sweeping changes: its enhancements include

  • A simpler representation - no stropping of keywords
  • String, bits, complex and record types
  • References to records are allowed
  • The while keyword
  • A larger standard library, including input/output


ALGOL W was originally implemented at Stanford in a high level assembly language called PL360, which I will look at later; this was augmented with S/360 assembler support routines at the University of Newcastle, UM and elsewhere to build the MTS compiler *ALGOLW.

It was the most popular of the ALGOL dialects on MTS, used for teaching programming fundamentals to students at several sites, and is well integrated with the operating system. The compiler features both a 'compile, load and go' mode suitable for learning the language and a traditional output to object deck option.


No special installation instructions to get ALGOL W running - just do the standard D6.0 setup as described in this guide and then sign on as a regular user such as ST01.

Compiling using *ALGOLW

For compile, load and go mode just run *ALGOLW and type the desired program in directly. Finish it with $endfile and the system will compile it, print any errors and if OK will execute it once before returning you to the MTS prompt.

For object mode, run *ALGOLW with the command line option par=deck. It will read source from scards (by default *source* ie standard input) and will write object files to spunch (by default the temporary file -awload). The object file can then be $run directly without referencing any library.

Other *ALGOLW compilation parameters are listed in MTS Volume 16.

Hello world - compile, load and go

Here's a simple program to print "Hello, world!" five times using the compile, load and go model. After I run *algolw I can type a compiler control statement at the = prompt but instead I enter begin, signalling that source code is being entered. *algolw prompts for the remainder of the program with :.

# $run *algolw
= begin

 Compilation begins ...
:    integer I;
:    for I := 1 until 5 do
:        write("Hello, world!");

 Options (UN022) :- main, debug

 (MAIN)    0.012 seconds to compile,  size 416 bytes

 Execution begins ...

 Hello, world!
 Hello, world!
 Hello, world!
 Hello, world!
 Hello, world!

 0.001 seconds in execution

Hello world - object file

Here I use the two step process of compiling to an object file and then running it. Assume the source code is in file hello.alw.

# $list hello.alw

     1     begin
     2         % Hello World program for ALGOL W %
     3         integer I;
     4         for I := 1 until 5 do
     5         begin
     6              write("Hello, world!");
     7         end
     8     end.

# $run *algolw scards=hello.alw par=deck

 Options (UN022) :- main, debug
 Object program:  "-AWLOAD"

 (MAIN)    0.007 seconds to compile,  size 416 bytes

# $run -awload

 Hello, world!
 Hello, world!
 Hello, world!
 Hello, world!
 Hello, world!

 0.001 seconds in execution

Further information

MTS volume 16 contains a very readable description of the language, the compiler options and how it integrates with MTS.

Wirth's Turing award lecture, From programming language design to computer construction, puts ALGOL W in context with Wirth's other work on Pascal and Modula 2.

The original ALGOL W reference manual from the Stanford implementation can be found here.