Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!van-bc!eslvcr!hasvcr!ted From: ted@hasvcr.UUCP (Ted Powell) Newsgroups: comp.lang.fortran Subject: Re: Using cpp for macro substitution. Summary: Avoid special characters cheaply by post-processing with tr Keywords: cpp, tr Message-ID: <194@hasvcr.UUCP> Date: 10 Jul 90 17:47:15 GMT References: <1990Jul6.162153.17644@helios.physics.utoronto.ca> Reply-To: ted@hasvcr.wimsey.bc.ca (Ted Powell) Distribution: na Organization: H.A. Simons Ltd., Vancouver, BC Lines: 48 In article <1990Jul6.162153.17644@helios.physics.utoronto.ca> quinlan@physics.utoronto.ca (Gerald Quinlan) writes: >I have a question about using the C preprocessor to perform macro >substitutions in a Fortran program. ... [example omitted] >Note how cpp has combined all the statements of the macro into one long line. >I suppose this isn't a problem in C, where the statements end with ";" and >more than one statement per line is allowed. But in Fortran this doesn't >work; the compiler won't accept adtest.f. Does anyone know how to get >around this? I've tried various tricks to get the macro inserted properly, >but can't seem to find anything that works. Your problem is that you want to get characters into the macro expansion that cpp considers special. An unescaped newline terminates the macro definition, rather than being included in it, and consecutive spaces are reduced to a single space. Pick a couple of characters that do not otherwise occur in your source files, for example percent to represent newline and underscore to represent space. Then (assuming you are in a Unix environment; you'll have to use an intermediate file otherwise) pipe the output of cpp through: tr '_%' ' \012' ^note space This translates underscores into spaces and percent signs into newlines (\012 is interpreted by tr as a newline character). Note that some versions of the tr program require their arguments in a slightly different form--check your manual if necessary. Your macros now look something like: #define qqsv(a,b,c) line one\ %______line two\ %______line three\ %______last line ^column 1 The remainder of your program is of course unchanged, since actual spaces and newlines are unaffected by their trip through tr. All this takes much less time to do than to describe, of course. M4 is a nice language. I used a similar language called UMIST heavily for many years as a Fortran preprocessor, and as an MVTAB (multivariate contingency tabulator) preprocessor. But in your circumstances I think M4 is overkill. -- ted@hasvcr.wimsey.bc.ca ...!ubc-cs!van-bc!hasvcr!ted (Ted Powell)