M4 (computer language)

From Free net encyclopedia

Revision as of 07:13, 11 March 2006; view current revision
←Older revision | Newer revision→

Template:Lowercase

m4 is a macro processing language designed by Brian Kernighan and Dennis Ritchie.

Contents

Use

A macro processor is a text-replacement tool. Its chief use is to re-use text templates, typically in programming applications, but also in text editing, text processing applications.

History

Macro processors were prevalent when assembly language programming was the common tool of programmers. In these early days of programming, the programmers noted that much of their program was repeated text. Simple means to re-use this text were invented. Programmers soon discovered it easy to not only reuse entire blocks of text, but, on occasion, substitute different values for similar parameters. This defined the usage range of macro processors. M4 was developed in 1977. It was the original macro engine used to implement Rational Fortran, and is shipped with most Unix variants. One of the most widespread present-day uses is as part of the GNU Project's autoconf.

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, it is not targeted at any particular computer or human language; historically, however, it was developed 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 is a simple example that could be part of an HTML generation library. It defines a macro to number sections automatically:

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

H2(First Section)
H2(Second Section)
H2(Conclusion)

After being processed with m4, the following text will be generated:

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

Free software implementations

There is a GNU version of m4. FreeBSD, NetBSD, and OpenBSD also provide independent implementations of the m4 language.

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. GNU Press. 2004. [1]

External links

ko:M4 (프로그래밍 언어) pl:M4 (informatyka) ru:M4