The M4 Macro Processor Brian W. Kernighan Dennis M. Ritchie Bell

The M4 Macro Processor Brian W. Kernighan Dennis M. Ritchie Bell

The M4 Macro Processor Brian W. Kernighan Dennis M. Ritchie Bell Laboratories Murray Hill, New Jersey 07974 ABSTRACT M4 is a macro processor available on UNIX† and GCOS. Its primary use has been as a front end for Ratfor for those cases where parameterless macros are not ad- equately powerful. It has also been used for languages as disparate as C and Cobol. M4 is particularly suited for functional languages like Fortran, PL/I and C since macros are specified in a functional notation. M4 provides features seldom found even in much larger macro processors, including •arguments •condition testing •arithmetic capabilities •string and substring functions •file manipulation This paper is a user’s manual for M4. July 1, 1977 †UNIX is a Trademark of Bell Laboratories. -- -- The M4 Macro Processor Brian W. Kernighan Dennis M. Ritchie Bell Laboratories Murray Hill, New Jersey 07974 Introduction Usage A macro processor is a useful way to enhance a On UNIX, use programming language, to make it more palatable m4 [files] or more readable, or to tailor it to a particular application. The #define statement in C and the Each argument file is processed in order; if there analogous define in Ratfor are examples of the are no arguments, or if an argument is `−’, the basic facility provided by any macro processor — standard input is read at that point. The processed replacement of text by other text. text is written on the standard output, which may The M4 macro processor is an extension of a be captured for subsequent processing with macro processor called M3 which was written by m4 [files] >outputfile D. M. Ritchie for the AP-3 minicomputer; M3 was in turn based on a macro processor imple- On GCOS, usage is identical, but the program is mented for [1]. Readers unfamiliar with the basic called ./m4. ideas of macro processing may wish to read some of the discussion there. Defining Macros M4 is a suitable front end for Ratfor and C, and The primary built-in function of M4 is define, has also been used successfully with Cobol. which is used to define new macros. The input Besides the straightforward replacement of one define(name, stuff) string of text by another, it provides macros with arguments, conditional macro expansion, arith- causes the string name to be defined as stuff. All metic, file manipulation, and some specialized subsequent occurrences of name will be replaced string processing functions. by stuff. name must be alphanumeric and must The basic operation of M4 is to copy its input to begin with a letter (the underscore counts as a its output. As the input is read, however, each letter). stuff is any text that contains balanced alphanumeric ‘‘token’’ (that is, string of letters parentheses; it may stretch over multiple lines. and digits) is checked. If it is the name of a Thus, as a typical example, macro, then the name of the macro is replaced by define(N, 100) its defining text, and the resulting string is pushed ... back onto the input to be rescanned. Macros may if (i > N) be called with arguments, in which case the argu- ments are collected and substituted into the right defines N to be 100, and uses this ``symbolic con- places in the defining text before it is rescanned. stant’’ in a later if statement. M4 provides a collection of about twenty built-in The left parenthesis must immediately follow the macros which perform various useful operations; word define, to signal that define has arguments. in addition, the user can define new macros. If a macro or built-in name is not followed imme- Built-ins and user-defined macros work exactly diately by `(’, it is assumed to have no arguments. the same way, except that some of the built-in This is the situation for N above; it is actually a macros have side effects on the state of the pro- macro with no arguments, and thus when it is cess. used there need be no (...) following it. You should also notice that a macro name is only recognized as such if it appears surrounded by -- -- -2- non-alphanumerics. For example, in macros. If you want the word define to appear in the output, you have to quote it in the input, as in define(N, 100) ... `define´ = 1; if (NNN > 100) As another instance of the same thing, which is a the variable NNN is absolutely unrelated to the bit more surprising, consider redefining N: defined macro N, even though it contains a lot of define(N, 100) N’s. ... Things may be defined in terms of other things. define(N, 200) For example, Perhaps regrettably, the N in the second definition define(N, 100) is evaluated as soon as it’s seen; that is, it is define(M, N) replaced by 100, so it’s as if you had written defines both M and N to be 100. define(100, 200) What happens if N is redefined? Or, to say it another way, is M defined as N or as 100? In M4, This statement is ignored by M4, since you can the latter is true — M is 100, so even if N subse- only define things that look like names, but it quently changes, M does not. obviously doesn’t hav ethe effect you wanted. To This behavior arises because M4 expands macro really redefine N, you must delay the evaluation names into their defining text as soon as it possi- by quoting: bly can. Here, that means that when the string N define(N, 100) is seen as the arguments of define are being col- ... lected, it is immediately replaced by 100; it’s just define(`N´, 200) as if you had said In M4, it is often wise to quote the first argument define(M, 100) of a macro. in the first place. If ` and ´ are not convenient for some reason, the If this isn’t what you really want, there are two quote characters can be changed with the built-in ways out of it. The first, which is specific to this changequote: situation, is to interchange the order of the defini- changequote([, ]) tions: makes the new quote characters the left and right define(M, N) brackets. You can restore the original characters define(N, 100) with just Now M is defined to be the string N, so when you changequote ask for M later, you’ll always get the value of N at that time (because the M will be replaced by N There are two additional built-ins related to which will be replaced by 100). define. undefine removes the definition of some macro or built-in: Quoting undefine(`N´) The more general solution is to delay the expan- sion of the arguments of define by quoting them. removes the definition of N. (Why are the quotes Any text surrounded by the single quotes ` and ´ absolutely necessary?) Built-ins can be removed is not expanded immediately, but has the quotes with undefine,asin stripped off. If you say undefine(`define´) define(N, 100) but once you remove one, you can never get it define(M, `N´) back. the quotes around the N are stripped off as the The built-in ifdef provides a way to determine if argument is being collected, but they hav eserved a macro is currently defined. In particular, M4 their purpose, and M is defined as the string N, has pre-defined the names unix and gcos on the not 100. The general rule is that M4 always strips corresponding systems, so you can tell which one off one level of single quotes whenever it evalu- you’re using: ates something. This is true even outside of ifdef(`unix´, `define(wordsize,16)´ ) −− −− -3- ifdef(`gcos´, `define(wordsize,36)´ ) define(a, (b,c)) makes a definition appropriate for the particular there are only two arguments; the second is liter- machine. Don’t forget the quotes! ally (b,c). And of course a bare comma or paren- ifdef actually permits three arguments; if the thesis can be inserted by quoting it. name is undefined, the value of ifdef is then the third argument, as in Arithmetic Built-ins M4 provides two built-in functions for doing ifdef(`unix´, on UNIX, not on UNIX) arithmetic on integers (only). The simplest is incr, which increments its numeric argument by Arguments 1. Thus to handle the common programming situ- So far we have discussed the simplest form of ation where you want a variable to be defined as macro processing — replacing one string by ``one more than N’’, write another (fixed) string. User-defined macros may define(N, 100) also have arguments, so different invocations can define(N1, `incr(N)´) have different results. Within the replacement text for a macro (the second argument of its Then N1 is defined as one more than the current define) any occurrence of $n will be replaced by value of N. the nth argument when the macro is actually used. The more general mechanism for arithmetic is a Thus, the macro bump, defined as built-in called ev al, which is capable of arbitrary arithmetic on integers. It provides the operators define(bump, $1 = $1 + 1) (in decreasing order of precedence) generates code to increment its argument by 1: unary + and − bump(x) ∗∗ or ˆ (exponentiation) ∗ / % (modulus) is + − x=x+1 == != < <= > >= ! (not) A macro can have as many arguments as you & or && (logical and) want, but only the first nine are accessible, or (logical or) through $1 to $9. (The macro name itself is $0, although that is less commonly used.) Arguments Parentheses may be used to group operations that are not supplied are replaced by null strings, where needed.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us