Jump to content

M4 (computer language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
copyediting
References: name link
Line 68: Line 68:
* Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. [http://wolfram.schneider.org/bsd/7thEdManVol2/m4/m4.pdf pdf]
* Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. [http://wolfram.schneider.org/bsd/7thEdManVol2/m4/m4.pdf pdf]
* Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. [http://www.cs.stir.ac.uk/~kjt/research/pdf/expl-m4.pdf pdf]
* Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. [http://www.cs.stir.ac.uk/~kjt/research/pdf/expl-m4.pdf pdf]
* René Seindal. GNU M4 Manual. GNU Press. 2004. [http://www.gnu.org/software/m4/manual/]
* René Seindal. [GNU M4 Manual http://www.gnu.org/software/m4/manual/]. GNU Press. 2004.


==External links==
==External links==

Revision as of 00:58, 5 April 2009


m4 is a general purpose macro processor designed by Brian Kernighan and Dennis Ritchie. The name "m4" stands for "macro": the letter m plus 4 other characters.

Use

All UNIXes make the m4 macro processor available, and POSIX has standardized it. The popularity of GNU autoconf (which requires GNU m4 for generating "configure" scripts) has encouraged many to install it, even if these people will not themselves program in m4.

A macro processor (or a preprocessor) operates as a text-replacement tool. End-users often employ it to re-use text templates, typically in programming applications, but also in text editing and in text-processing applications.

History

Macro processors became popular when programmers commonly used assembly language. In those early days of programming, the programmers noted that much of their program 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 (e.g., a macro's arguments get expanded twice, once during scanning and once when at interpolation time)

The implementation of Rational Fortran used m4 as its macro engine from the beginning; and most Unix variants ship with it.

As of 2009 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.

In general, m4 has many uses in code generation, owing to its Turing completeness, but can be hard to debug.

Features

m4 offers these facilities:

  • text replacement
  • parameter substitution
  • file inclusion
  • string manipulation
  • conditional evaluation
  • arithmetic expressions
  • system interface
  • programmer diagnostics

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.

Example

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

define(`H2_COUNT', 0)dnl
define(`H2', `define(`H2_COUNT', incr(H2_COUNT))'dnl
`<h2>H2_COUNT. $1</h2>')dnl
dnl
H2(First Section)
H2(Second Section)
H2(Conclusion)

Processing this code with m4 should generate the following text:

<h2>1. First Section</h2>
<h2>2. Second Section</h2>
<h2>3. Conclusion</h2>

Note the frequent use of dnl, which deletes up to and including the newline, thus preventing many blank lines appearing in the output.

Free software implementations

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

See also

References

  • Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. pdf
  • Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. pdf
  • René Seindal. [GNU M4 Manual http://www.gnu.org/software/m4/manual/]. GNU Press. 2004.