General-purpose macro processor

As macro processors computer programs are called, replace the strings within a text by other strings. A well-known macro processor, for example, part of the typesetting program TeX. Also, the C preprocessor of the C language is a macro processor.

A string with their replacement text is called macro. Macros are usually parameterized. A special macro define a rule is pre-defined and enables the generation of further macros. Other predefined macros often allow string handling, conditional text replacement, writing and reading help files and the like.

The m4 macro processor as an example

The Unix operating system comes standard with the m4 macro processor. This allows the definition of close- define your own macros.

Define ( ` H ',` Hans ') define ( ` U ',` and ') define ( ` I ', ` rings ') dnl      H U I, I U H. H loves I. results

  • Hans and Inge, Inge and Hans. Hans loves Inge.

The characters ` ( on a German keyboard to the right of the question mark ) and ' (right next to the Ä ) ensure that macros are not replaced when they are within a part of the ` start with ' ends.

For many more built-in macros such as eval, ifelse can be expressions and conditions to implement. Repeats and grinding can be achieved by recursion.

With a macro processor can be, for example, automatic meter implement:

Define ( ' numerator ', ` define ( ` $ 1 ', incr ($ 1) ) $ Chapter 1 ') dnl define ( ` kapitelnr ', 0) dnl Counter ( ` kapitelnr ') Counter ( ` kapitelnr ') Counter ( ` kapitelnr ') Counter ( ` kapitelnr ') If this sample processed by m4, we get the following output:

Chapter 1 Chapter 2 Chapter 3 Chapter 4 This allows, for example, chapter and section numbers automatically count up in a text:

Divert (-1 ) dnl    define ( ' numerator ', ` define ( ` $ 1 ', incr ($ 1) ) $ 1')    define ( ` kapitelnr ', 0)    define ( ` abschnittnr ', 0)    define ( ` chapter ',`

counter ( ` kapitelnr '). $ 1 < / h1 > define ( ` abschnittnr ', 0) ')    define ( ` section ',`

kapitelnr.Zaehler ( ` abschnittnr ' ) $ 1 < / h2 > ' ) divert ( 0 ) dnl Chapter (Introduction) This text is about ... Section ( story) History following to say ... Section ( Recent Developments ) However, in recent times, resulting ... Chapter (Definitions) Section ( numerical values ​​) ... Section (constants) ... Section (variables) ... The output of m4 is then

1 Introduction < / h1 >    This text is about ...   

1.1 History < / h2 >    History following to say ...   

1.2 Recent Developments < / h2 >    However, in recent times, resulting ...   

2 Definitions < / h1 >   

2.1 Numerical values ​​< / h2 >    ......   

constants 2.2 < / h2 >    ......   

2.3 Variables < / h2 >    ...... The macro processor as a preprocessor

Macro processors represent a form of preprocessors (pre- processors) dar. This change an input text before the actual processing program gets this to face.

On Unix macro processor on the command line as a separate processes can be called, passing the processed text is done via a pipe:

$ M4 diplomarbeit.txt | tbl | eqn | troff - mt- Tps | kprinter

Here the file is diplomarbeit.txt first by the macro processor m4 processed, then becoming tbl and converted by the formalities set processor eqn (both macro processors), then groff from the text set - up (macro ) processor in the PostScript language from the table processor. kprinter can then output the result to a PostScript - capable printer.

The C preprocessor

The C programming language contains a simple macro processor, the C preprocessor. This can be used for the following tasks:

  • Definition of symbolic constants
  • Conditional Compilation
  • Extension of the language through simple language constructs
  • Simplification of paperwork

The possibilities of the C preprocessor are relatively limited. It gives the language, however, an additional flexibility that is rarely achieved by other languages ​​. However, the results in complex program systems also difficulties with the care and maintenance of uniform definitions, so below -level programming languages ​​were partly deliberately avoided this approach.

In the following program example

# define FIELD SIZE 100      int array [FIELD SIZE];    main () {       int i;       Field = 0; Field = 1;       for (i = 2; i < FIELD SIZE, i)          Field [i ] = array [i- 1] array [i -2];    } FIELD SIZE is simply replaced by 100:

Int field;    main () {       int i;       Field = 0; Field = 1;       for (i = 2; i < 100; i )          Field [i ] = array [i- 1] array [i -2];    } Only by creating a program text that the actual C compiler can flawlessly translate.

The following program determines whether it has been compiled on Unix. Otherwise, waiting for an input:

# include    main () {       printf ( "The program is running" );    # ifdef __ UNIX__       printf (" Unix \ n. ");    # else       printf ("by an unknown operating system. \ n ");       printf (" Please press a key " );       getchar ();    # endif    } A Unix compiler would translate here the following text:

Main () {       printf ( "The program is running" );       printf (" Unix \ n. ");    } A compiler of an unknown operating system, however, would translate the following program:

Main () {       printf ( "The program is running" );       printf ("by an unknown operating system. \ n ");       printf (" Please press a key " );       getchar ();    } The C macro processor is much easier than the m4 processor. He does not allow recursive calls, grinding or evaluation of expressions.

The macro processor of the typesetting program TeX can be used for custom extensions. The macro package LaTeX by Leslie Lamport is a widespread extension dar. instead define new macros are defined by newcommand.

Part of a CD cover shows the following example:

541440
de