Girish Mahajan (Editor)

M4 (computer language)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Paradigm
  
macro

First appeared
  
1977

Designed by
  
Brian Kernighan, Dennis Ritchie.

m4 is a general-purpose macro processor included in all UNIX-like operating systems, and is a component of the POSIX standard.

Contents

The language was designed by Brian Kernighan and Dennis Ritchie for the original versions of UNIX. It is an extension of an earlier macro processor m3, written by Ritchie for the AP-3 minicomputer.

The macro preprocessor operates as a text-replacement tool. It is employed to re-use text templates, typically in computer programming applications, but also in text editing and text-processing applications. Most users require m4 as a dependency of GNU autoconf.

History

Macro processors became popular when programmers commonly used assembly language. In those early days of programming, programmers noted that much of their programs consisted of repeated text, and they invented simple means for reusing this text. Programmers soon discovered the advantages not only of reusing entire blocks of text, but also of substituting different values for similar parameters. This defined the usage range of macro processors.

Kernighan and Ritchie developed m4 in 1977, basing it on the ideas of Christopher Strachey. The distinguishing features of this style of macro preprocessing included:

  • free-form syntax (not line-based like a typical macro preprocessor designed for assembly-language processing)
  • the high degree of re-expansion (a macro's arguments get expanded twice: once during scanning and once at interpretation time)
  • The implementation of Rational Fortran used m4 as its macro engine from the beginning; and most Unix variants ship with it.

    As of 2016 many applications continue to use m4 as part of the GNU Project's autoconf. It also appears in the configuration process of sendmail (a widespread mail transfer agent) and for generating footprints in the gEDA toolsuite. The SELinux Reference Policy relies heavily on the m4 macro processor.

    m4 has many uses in code generation, but (as with any macro processor) problems can be hard to debug.

    Features

    m4 offers these facilities:

  • a free-form syntax, rather than line based syntax
  • a high degree of macro expansion (arguments get expanded during scan and again during interpretation)
  • text replacement
  • parameter substitution
  • file inclusion
  • string manipulation
  • conditional evaluation
  • arithmetic expressions
  • system interface
  • programmer diagnostics
  • programming language independent
  • human language independent
  • provides programming language capabilities
  • Unlike most earlier macro processors, m4 does not target any particular computer or human language; historically, however, its development originated for supporting the Ratfor dialect of Fortran. Unlike some other macro processors, m4 is Turing-complete as well as a practical programming language.

    Unquoted identifiers which match defined macros are replaced with their definitions. Placing identifiers in quotes suppresses expansion until possibly later, such as when a quoted string is expanded as part of macro replacement. Unlike most languages, strings in m4 are quoted using the backtick (`) as the starting delimiter, and apostrophe (') as the ending delimiter. The use of separate starting and ending delimiters allows for the arbitrary nesting of quotation marks in strings, allowing a fine degree of control of how and when macro expansion takes place in different parts of a string.

    Example

    The following fragment gives a simple example that could form part of a library for generating HTML code. It defines a commented macro to number sections automatically:

    Processing this code with m4 generates the following text:

    Implementations

    FreeBSD, NetBSD, and OpenBSD provide independent implementations of the m4 language. Furthermore, the Heirloom Project Development Tools includes a free version of the m4 language, derived from OpenSolaris.

    GNU m4 is an implementation of m4 for the GNU Project. GNU m4 is designed to avoid many kinds of arbitrary limits found in traditional m4 implementations, such as maximum line lengths, maximum size of a macro and number of macros. Removing such arbitrary limits is one of the stated goals of the GNU Project.

    The GNU Autoconf package makes extensive use of the features of GNU m4.

    GNU m4 is currently maintained by Gary V. Vaughan and Eric Blake. Released under the terms of the GNU General Public License, GNU m4 is free software.

    References

    M4 (computer language) Wikipedia