One-pass compiler

From Wikipedia, the free encyclopedia

A One-pass Compiler is a type of software compiler that passes through the source code of each compilation unit only once. In a sense, a one-pass compiler can't 'look back' at code it previously processed. Another term sometimes used is narrow compiler, which emphasizes the limited scope a one-pass compiler is obliged to use. This is in contrast to a multi-pass compiler which traverses the source code and/or the abstract syntax tree several times, building one or more intermediate representations that can be arbitrarily refined.

While one-pass compilers may be faster than multi-pass compilers, they are unable to generate as efficient programs, due to the limited scope available. In addition, some languages cannot be compiled in a single pass, as a result of their design.

In contrast some constructs in programming languages have been designed specifically to be compiled with one-pass compilers. An example of such a construct is the forward declaration in Pascal. Normally Pascal requires that procedures be fully defined before use. This helps a one-pass compiler with its type checking: calling a procedure that hasn't been defined is a clear error. However, this requirement makes mutually recursive procedures impossible to implement:

   function odd(n : integer) : boolean
   begin
      if n = 0 then
          odd := true
      else if n < 0 then
          odd := not even(n + 1) { Compiler error: 'even' is not defined }
      else 
          odd := not even(n - 1)
   end;
   
   function even(n : integer) : boolean
   begin
      if n = 0 then
          even := true
      else if n < 0 then
          even := not odd(n + 1)
      else 
          even := not 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.... }