m4 is a macro processing language. It was the original macro engine of Rational Fortran.
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. One of the most widespread uses is as part of the GNU autoconf.
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 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.
A simple example (using the GNU extension patsubst to construct the definition of bye from that of hi):
define(`hi', `Hello, $1')
define(`bye', patsubst(patsubst(defn(`hi'),
`,', ````,' ' ' '),
Hello, Good-``by'e')`, have a good time')
hi(Tom)!
bye(Tom)!
After being processed with m4:
m4 inputfile >outputfile
the outputfile will contain some blank lines followed by
Hello, Tom!
Good-bye, Tom, have a good time!
Free software implementations
There is a GNU version of m4.
External links
References
René Seindal. GNU M4 Manual. GNU Press. 2004. [1]
Last updated: 09-25-2005 16:17:30