Re: C preprocessor vs Macro processor

ok@cs.rmit.edu.au (Richard A. O'Keefe)
13 Nov 1997 23:45:18 -0500

          From comp.compilers

Related articles
C preprocessor vs Macro processor peter.govoni@analog.com (Peter Govoni) (1997-11-02)
Re: C preprocessor vs Macro processor gjb@cs.washington.edu (Greg Badros) (1997-11-07)
Re: C preprocessor vs Macro processor tgm@netcom.com (1997-11-11)
Re: C preprocessor vs Macro processor ok@cs.rmit.edu.au (1997-11-13)
| List of all articles for this month |

From: ok@cs.rmit.edu.au (Richard A. O'Keefe)
Newsgroups: comp.compilers
Date: 13 Nov 1997 23:45:18 -0500
Organization: Comp Sci, RMIT University, Melbourne, Australia.
References: 97-11-021 97-11-033
Keywords: macros

I'm going to release a new version of pdm4 next week, so I thought I'd
comment.


Greg Badros <gjb@cs.washington.edu> writes:


>Also, M4 has many many more builtin functions (including regular
>expression matching, and much more) and permits recursive macros; Ansi
>C's cpp disallows recursion in macros.


*GNU* M4 has regular expression stuff, but V7 M4, BSD M4, System V M4,
and PD M4 do *not*. It's worth noting that according to Sun's 'Shade'
toolkit, GNU m4 executes more than 3 times as many instructions as pd
m4.


The main thing about m4 is that it is a complete programming language,
while cpp isn't, and that cpp is very closely tied to C lexical
structure, while m4 isn't.


Next weeks' pdm4 release will include case-insensitive macro names as
a run-time option, making it even more useful with Fortran, Pascal,
and Ada than it already is.


>> Does the C preprocessor hold any advantage over M4 ?


>It's easier to learn, is not stuck with complicated quoting rules, and
>more constrained in its capabilities; reasoning about source code
>preprocessed by cpp is hard enough -- reasoning about source code
>processed by m4 would be nearly impossible.


On the other hand, it must be said that cpp has some weirdness (like
pp-numbers) that m4 is free of. m4 has very simple rules that allow
very complex programs, cpp has very complex rules that allow only very
simple "programs". For what it's worth, pdm4 and SVr4 m4 are about
the same size as the cpp distributed with lcc, which is the smallest
cpp I've ssen.


Here's an example of using m4 with C.
Suppose we want to have a two-dimensional array where the
[i][j] element is "fred(i,j)".


define(NROWS, 4)
define(NCOLS, 5)
include(forloop.m4) dnl comes with GNU M4 and pdm4.
char *demo[NROWS][NCOLS] = {
        forloop(i, 0, NROWS-1,
`{forloop(j, 0, NCOLS-1,
`"fred(i,j)",')},')
};


--
John Æneas Byron O'Keefe; 1921/02/04-1997/09/27; TLG,TLTA,BBTNOTL.
Richard A. O'Keefe; RMIT Comp.Sci; http://www.cs.rmit.edu.au/%7Eok
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.