首页 > 其他分享 >GNU m4为啥叫m4

GNU m4为啥叫m4

时间:2022-12-21 19:44:06浏览次数:58  
标签:macros macro GNU m4 POSIX https 为啥 processor

Originally, the Kernighan and Plauger macro-processor, and then m3, formed the engine for the Rational FORTRAN preprocessor, that is, the Ratfor equivalent of cpp. Later, m4 was used as a front-end for Ratfor, C and Cobol. [Ratfor:Rational FORTRAN; cpp: C preprocessor]

René Seindal released his implementation of m4, GNU m4, in 1990, with the aim of removing the artificial limitations in many of the traditional m4 implementations, such as maximum line length, macro size, or number of macros.

https://www.gnu.org/software/m4/manual/ | https://mbreen.com/m4.html

m4 is a macro processor, in the sense that it copies its input to the output, expanding macros as it goes. Macros are either builtin or user-defined, and can take any number of arguments. Besides just doing macro expansion, m4 has builtin functions for including named files, running shell commands, doing integer arithmetic, manipulating text in various ways, performing recursion, etc.… m4 can be used either as a front-end to a compiler, or as a macro processor in its own right [on its own, without depending on others].

At its most basic, m4 can be used for simple embedded text replacement. If it receives the input

define(AUTHOR, William Shakespeare)
A Midsummer Night's Dream
by AUTHOR

then it outputs

A Midsummer Night's Dream
by William Shakespeare

While similar in principle to the better-known C preprocessor, m4 is a far more powerful, general-purpose tool. Examples:

  • ifdef(`a',b) outputs b if a is defined; ifdef(`a',b,c) outputs c if a is not defined.
  • the eval macro allows access to integer arithmetic
  • Strings. substr(`hello', 1, 3); index(`hello',`llo')
  • Here is a reimplementation of the len builtin (replacing it) as a recursive macro: define(`len',`ifelse($1,,0,`eval(1+len(substr($1,1)))')')

Recursive template explanation C++ | Parameter pack(since C++11) | C++ variadic template recursive example

The m4 macro processor is widely available on all UNIXes, and has been standardized by POSIX. GNU Autoconf requires GNU m4 for generating configure scripts.

POSIX (Portable Operating System Interface) is a set of standard operating system interfaces based on the Unix operating system. The most recent POSIX specifications -- IEEE Std 1003.1-2017 -- defines a standard interface and environment that can be used by an operating system (OS) to provide access to POSIX-compliant applications. pthreads or POSIX threads are an implementation of the thread API for C/C++.

Macro languages were invented early in the history of computing. In the 1950s Alan Perlis suggested that the macro language be independent of the language being processed. Techniques such as conditional and recursive macros, and using macros to define other macros, were described by Doug McIlroy of Bell Labs in “Macro Instruction Extensions of Compiler Languages”, Communications of the ACM 3, 4 (1960), 214–20, https://dl.acm.org/doi/10.1145/367177.367223.

The Microsoft Macro Assembler (MASM) is an x86 architecture assembler for MS-DOS and Microsoft Windows.

An important precursor of m4 was GPM; see C. Strachey, “A general purpose macrogenerator”, Computer Journal 8, 3 (1965), 225–41, https://academic.oup.com/comjnl/article/8/3/225/336044. GPM is also succinctly described in David Gries’s book Compiler Construction for Digital Computers, Wiley (1971). Strachey was a brilliant programmer: GPM fit into 250 machine instructions!

The Brian Kernighan and P.J. Plauger book Software Tools, Addison-Wesley (1976), describes and implements a Unix macro-processor language, which inspired Dennis Ritchie to write m3, a macro processor for the AP-3 minicomputer.

Kernighan and Ritchie then joined forces to develop the original m4, described in “The M4 Macro Processor”, Bell Laboratories (1977), https://wolfram.schneider.org/bsd/7thEdManVol2/m4/m4.pdf. It had only 21 builtin macros.

标签:macros,macro,GNU,m4,POSIX,https,为啥,processor
From: https://www.cnblogs.com/funwithwords/p/16997000.html

相关文章