PL/0 - Examples

Examples

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; ! squ; x := x + 1 END END.

This program outputs the squares of numbers from 1 to 10. Most courses in compiler construction today have replaced the exclamation mark with the WriteLn procedure.

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.

Read more about this topic:  PL/0

Famous quotes containing the word examples:

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)