PL/0

From Free net encyclopedia

There are at least two programming languages known as PL/0. One is a subset of IBM's general purpose programming language PL/I.

The other PL/0 is a simplified version of the general-purpose programming language Pascal, intended as an educational programming language. It serves as an example of how to construct a compiler. It was originally introduced in the book, Algorithms + Data Structures = Programs, by Niklaus Wirth in 1975. It features quite limited language constructs: there are no real numbers, very few basic arithmetic operations and no control-flow constructs other than "if" and "while" blocks. While these limitations makes writing real applications in this language impractical, it helps the compiler remain compact and simple.

The following is the syntax rules of the model language defined in EBNF:

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 ")".

It is rather easy for students to write a recursive descent parser for such a small and simple syntax. Therefore, the PL/0 compiler is still widely used in courses on compiler construction throughout the world. Due to the lack of features in the original specification, students usually spend most of their time with extending the language and their compiler. They usually start with introducing REPEAT .. UNTIL and continue with more advanced features like parameter passing to procedures or data structures like arrays, strings or floating point numbers.

The following example is taken from such an extended language called PL/0E.

VAR x, squ;

PROCEDURE square;
BEGIN
   squ := x * x
END;

BEGIN
   x := 1;
   WHILE x <= 10 DO
   BEGIN
      CALL square;
      CALL WriteLn (squ);
      x := x + 1;
   END
END.

This program outputs the squares of numbers from 1 to 10. The WriteLn procedure is not part of the original specification of PL/0, but most courses in compiler construction today assume it is part of the language.

In December 1976 (while at Palo Alto), Wirth wrote a small booklet about compiler construction, containing the full source code of the PL/0 compiler. The following example was taken from the second edition of Wirth's book Compilerbau, which appeared in 1986 in Germany.

CONST
  m =  7,
  n = 85;

VAR
  x, y, z, q, r;

PROCEDURE multiply;
VAR a, b;

BEGIN
  a := x;
  b := y;
  z := 0;
  WHILE b > 0 DO BEGIN
    IF ODD b THEN z := z + a;
    a := 2 * a;
    b := b / 2;
  END
END;

PROCEDURE divide;
VAR w;
BEGIN
  r := x;
  q := 0;
  w := y;
  WHILE w <= r DO w := 2 * w;
  WHILE w > y DO BEGIN
    q := 2 * q;
    w := w / 2;
    IF w <= r THEN BEGIN
      r := r - w;
      q := q + 1
    END
  END
END;

PROCEDURE gcd;
VAR f, g;
BEGIN
  f := x;
  g := y;
  WHILE f # g DO BEGIN
    IF f < g THEN g := g - f;
    IF g < f THEN f := f - g;
  END;
  z := f
END;

BEGIN
  x := m;
  y := n;
  CALL multiply;
  x := 25;
  y :=  3;
  CALL divide;
  x := 84;
  y := 36;
  CALL gcd;
END.

External links

References