One-pass Compiler - Pascal Example

Pascal Example

An example of such a construct is the forward declaration in Pascal. Pascal requires that procedures be declared or fully defined before use. This helps a one-pass compiler with its type checking: calling a procedure that hasn't been declared anywhere is a clear error. Forward declarations help mutually recursive procedures call each other directly, despite the declare-before-use rule:

function odd(n : integer) : boolean; begin if n = 0 then odd := false else if n < 0 then odd := even(n + 1) { Compiler error: 'even' is not defined } else odd := even(n - 1) end; function even(n : integer) : boolean; begin if n = 0 then even := true else if n < 0 then even := odd(n + 1) else even := odd(n - 1) end;

By adding a forward declaration for the function even before the function odd, the one-pass compiler is told that there will be a definition of even later on in the program.

function even(n : integer) : boolean; forward; function odd(n : integer) : boolean; { Et cetera }

When the actual declaration of the body of the function is made, either the parameters are omitted or must be absolutely identical to the original forward declaration, or an error will be flagged.

Read more about this topic:  One-pass Compiler

Famous quotes containing the word pascal:

    Let man consider what he is in comparison with all existence; let him regard himself as lost in this remote corner of nature; and from the little cell in which he finds himself lodged, I mean the universe, let him estimate at their true value the earth, kingdoms, cities, and himself. What is a man in the infinite?
    —Blaise Pascal (1623–1662)

    Does he who loves someone on account of beauty really love that person? No, for smallpox, which will kill beauty without killing the person, will cause him to love the person no more. And if one loves me for my judgment, for my memory, he does not love me, for I can lose these qualities without losing myself. Where, then, is this myself, if it be neither in the body nor in the soul?
    —Blaise Pascal (1623–1662)