PL/0

Appeared in:
1975
Paradigm:
Typing discipline:
Versions and implementations (Collapse all | Expand all):
Programming language

PL/0 is a small educational language used as an example of compiler development.

The language was presented by Niklaus Wirth in his book “Algorithms + Data Structures = Programs” (1975). His later book “Compilerbau” (“Compiler Construction”, 1976) provided a full source code of PL/0 compiler written in Pascal.

Originally the capabilities of the language were intentionally limited:

  • the only data type are integer numbers. Still all constants and variables used have to be declared explicitly, not deduced at compile-time.
  • the only operators provided are arithmetical and comparison ones.
  • there is one built-in function odd which checks whether the integer argument is odd.
  • there are no input/output routines; instead the compiler prints the new value of each variable whenever it gets changed.
  • the flow control structures are represented by if-then and while-do constructs, as well as user-defined procedures (which can’t accept any parameters).

The syntax of PL/0 described in extended Backus-Naur form follows:

program = block "." .

block = [ "const" ident "=" number {"," ident "=" number} ";"]
        [ "var" ident {"," ident} ";"]
        { "procedure" ident ";" block ";" } statement .

statement = [ ident ":=" expression | "call" ident |
            "begin" statement {";" statement } "end" |
            "if" condition "then" statement |
            "while" condition "do" statement ].

condition = "odd" expression |
            expression ("="|"#"|"<"|">") expression .

expression = [ "+"|"-"] term { ("+"|"-") term}.

term = factor {("*"|"/") factor}.

factor = ident | number | "(" expression ")".

The following editions of “Compiler Construction” enhanced PL/0 to illustrate more complicated principles (for example, they added input-output routines), until finally it was replaced by Oberon-0.

PL/0 is so simple to implement and has so many extension ways (adding new data types, flow control structures etc.) that it’s still a rather popular choice for compiler-studying courses.

Elements of syntax:

Case-sensitivity yes
Variable assignment <varname> := <value>
Variable declaration var <varname>
Block begin ... end
Physical (shallow) equality =
Physical (shallow) inequality #
Comparison < >
Function definition procedure <name>; <body>;
Function call call <name>
Sequence ;
If - then if <condition> then <trueBlock>
Loop forever while 1 = 1 do <loopBody>
While condition do while <condition> do <loopBody>

Examples:

Factorial:

Example for versions Wirth's PL/0 (1976)

The language makes it impossible to print any characters, so the program just outputs the pairs number-factorial without any separators. Running this program results in the following output:

    0 var n, f;  
    1 begin  
    2    n := 0;  
    4    f := 1;  
    6    while n # 16 do  
   10    begin  
   10       n := n + 1;  
   14       f := f * n;  
   18    end;  
   19 end.  
    0  jmp  1    1  
    1  int  1    5  
    2  lit  1    0  
    3  sto  1    3  
    4  lit  1    1  
    5  sto  1    4  
    6  lod  1    3  
    7  lit  1   16  
    8  opr  1    9  
    9  jpc  1   19  
   10  lod  1    3  
   11  lit  1    1  
   12  opr  1    2  
   13  sto  1    3  
   14  lod  1    4  
   15  lod  1    3  
   16  opr  1    4  
   17  sto  1    4  
   18  jmp  1    6  
   19  opr  1    0  
 start pl/0
0
1
1
1
2
2
3
6
4
24
5
120
6
720
7
5040
8
40320
9
362880
10
3628800
11
39916800
12
479001600
13
1932053504
14
1278945280
15
2004310016
16
2004189184
 end pl/0
var n, f;
begin
   n := 0;
   f := 1;
   while n # 16 do
   begin
      n := n + 1;
      f := f * n;
   end;
end.