M5(ID:1696/m::001)


A. Dain, U Cincinnati, 1992. Macro processor, a generalization of M4. For Unix and DOS.


Related languages
M4 => M5   Evolution of

References:
  • A. Dain Samples "User's Guide to the M5 Macro Language" view details Abstract: M5 is a powerful, easy to use, general purpose macro language. M5's
    syntax allows concise, formatted, and easy to read specifications of
    macros while still giving the user control over the appearance of the
    resulting text. M5 macros can have named parameters, can have an
    unbounded number of parameters, and can manipulate parameters as a single unit. M5 provides separate macro name spaces called `pools' that simplify the creation and management of context-dependent macros and dynamic macros (macros defined by macros based on the input). Several examples demonstrate m5's power and flexibility, including a random string generator that uses BNF grammars to specify the form of the strings; an implementation of a Turing machine; and a demonstration of how m5 can process LaTeX input and programming language source in the same file to simplify code maintenance and documentation.
    Extract: Introduction
    Introduction

    Macro processors are an extremely useful means of transforming text, and are a standard part of many language systems. For example, the C language system has a macro preprocessor as a de facto part of the language standard. However, the general UNIX user does not have access to a powerful general purpose macro processor. The macro processors distributed with UNIX cpp and m4 do not qualify on either point.

    The phrase `powerful, general purpose macro processor' should mean that the processor implements a robust set of functions for a proof that m5 is Turing equivalent. I assert that cpp is not Turing equivalent, and have not bothered to check m4, though I suspect it is. The macro language should implement scopes, much as modern programming languages allow name overloading through scopes. Debugging should be easy. The macro language should also give the programmer great flexibility in dealing with macros with variable numbers of parameters.

    Recursive macros should be easy to write. Perhaps most importantly, the user'should find it easy to read macro definitions the day after they are written.

    M5 implements a large set of pre-defined macros that simplify the manipulation of text. It has a powerful scoping mechanism that allows the user to define `pools' of macros (think of them as a file system subdirectory) and a macro name search stack of such pools. For instance, in a programming language source file, a user defined macro might expand one way if it occurs in a global environment, and another in local environments. M5 can be used to track nesting of function declarations, begin/end blocks, or nesting of C's curly braces. Pools of macros can be defined for the various contexts, and the appropriate pool used to control macro expansion in the desired context. Pools are an associative data structure mapping names to definitions and so are useful for collecting and manipulating many different kinds of data. The example in appendix implementation demonstrates the usefulness of pools: in the implementation of the m5 processor, the names of parameters to macros can be referenced both in LaTeX documentation and in C code using the same name, but each expands into something useful for that particular context; the LaTeX version of the macros sets the parameter names in a particular font, while the C version expands to the code to access the runtime data structure containing the value of the indicated parameter.

    M5 debugging allows the user to track easily each step of a macro's expansion, and control how much debug output is desired.

    M5 macros can handle large numbers of named or anonymous parameters, allowing recursive macros to be defined easily and naturally. M5 has many control constructs to simplify the user's task, including Case,  Match, and several flavors of conditional tests.

    M5 can be configured to scan various kinds of source files; it understands C, C++, and Ada commenting conventions. The user can disable macro definitions in contexts where their expansion would be a nuisance.

    Finally, m5 allows the user precise control over the text that appears as the result of macro expansion without making the definition of the macro difficult to read. Macro definitions can be textually formatted to reflect logical structure without sacrificing the readability of the expanded text.