In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers.
A common example from computer programming is the processing performed on source code before the next step of compilation. Preprocessors are typical examples of Domain-Specific Programming Languages.
Lexical pre-processors are the lowest-level of preprocessors, insofar as they only require lexical analysis. Indeed, these pre-processors work by simple substitution of tokenized character sequences for other tokenized character sequences, according to user-defined rules. They typically perform macro substitution, inclusion of other files (by opposition to higher-order features such as inclusion of modules/packages/units/components), conditional compilation and/or conditional inclusion.
While this use of a preprocessor for code reuse is simple, it is also slow and very inefficient and requires the additional use of conditional compilation to avoid multiple inclusions of a given header file.
Since the 1970s, faster, safer and more efficient alternatives to reuse by file inclusion have been known and used by the programming language community, and implemented in most programming languages: Java has packages, Pascal has units, Modula, OCaml, Haskell or Python have modules, just as D, designed as a replacement of C and C++, has imports.
For instance, #define max(a,b) a>b?a:b defines macro max. This macro may be called as any C function. Therefore, after preprocessing, z = max(x,y); becomes z = x>y?x:y;
While this use of macros is very important for C, for instance to define type-safe generic data-types or debugging tools, it is also slow, rather inefficient, and may lead to a number of pitfalls.
For instance, if f and g are two functions, calling z = max(f(), g()); will not evaluate once f() and once g() and place the highest value in z as one may believe. Rather, one of the functions will be evaluated twice. If that function has side effects, this is usually not the expected behavior.
More modern languages typically do not use this form of meta-programming by macro-expansion of character strings, rather relying on (either automatic or manual) inlining of functions/methods and templates/parametric polymorphism/generic functions/classes/data structures.
Lisp languages, on the other hands, are designed around the use of macros of a similar -- yet much more powerful -- style.
#ifdef x ... #else ... #endif or #if x ... #else ... #endif
Once again, most modern programming languages discard this feature, rather relying on traditional if...then...else... flow control operators, leaving to the compiler the task of removing useless code from the executable.
Other lexical preprocessors include
Syntactic preprocessors are typically used to customize the syntax of a language, extend a language by adding new primitives or turn a generic programming language into a Domain-Specific Programming Languages.
Similarly, a number of programs written in OCaml customize the syntax of the language by the addition of new operators.
Similarly, statically-checked, type-safe regular expressions or code generation may be added to the syntax and semantics of OCaml through macros, as well as micro-threads/coroutines/fibers, monads or transparent XML manipulation.
The MetaOCaml preprocessor/language provides similar features for external Domain-Specific Programming Languages. This preprocessor takes the description of the semantics of a language (i.e. an interpreter) and, by combining compile-time interpretation and code generation, turns that definition into a compiler to the OCaml programming language -- and from that language, either to bytecode or to native code
code generation | Programming language implementation
Präprozessor | Préprocesseur | Preprocesorius | プリプロセッサ | Preprocessore | Preprocesor | Препроцессор
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Preprocessor".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world