Clemson University -- CPSC 231 -- Fall 2009 macro processor simple idea of textual substitution define(name,definition) place definition in symbol table and replace name with definition wherever it appears quoted string protects from replacement example define and replace sequence given by program /home/mark/231/sim/mp (must be on Sun SPARC system to run it) m4 macro processor a programming language with a quite different feel than C, Java, etc.: * textual substitution (replacement) rather than numeric data processing * function evaluation rather than variety of control structures * recursion rather than iteration (you can build iteration out of recursion) define(name,definition) -- macro definition - instructs m4 to replace each occurrence of "name" by its definition (textual substitution, a.k.a. expansion). You can define a macro once and use it several times. Leading unquoted blanks, tabs, and newlines are ignored. $1,$2,... inside "definition" -- macro parameters (based on position in call) for parameterized expansion ($0 = macro name, $* = all arguments $1 and above) name(arg1,arg2) -- macro call with two parameters (arguments) include(file) -- read in text of "file" `text' -- delay expansion of text but strip quotes (note open quote and close quote difference) changequote(^,!) -- change quote characters to other characters eval(expression) -- evaluate an arithmetic expression incr(arg) / decr(arg) -- returns the value incremented or decremented [ shortcuts for eval(arg+1), eval(arg-1) ] ifdef(arg1,arg2,arg3) -- if the first parameter is defined, return the second, otherwise return the third ifelse(arg1,arg2,arg3,arg4) -- if the first parameter is the same string as the second, return the third parameter, otherwise return the fourth (with a provision for nested ifelse evaluation using parameters 4,5,6, and 7) index(arg1,arg2) -- returns position within the first parameter where the second parameter begins (0-origin), or -1 if the second parameter is not a substring within the first parameter len(arg) -- returns the number of characters in the first parameter (i.e., the string length) substr(arg1,arg2,arg3) -- substring(string,start,length) where start is 0-origin position translit(arg1,arg2,arg3) -- transliterate first string using match characters of second parameter with substitute characters of third parameter divert(2) -- send output to second stream ... undivert -- print all streams dnl -- delete rest of line, including newline debugmode(V) -- turns on tracing and debugging output ---------- typical use in HLL of "magic number" substitution ---------- define(N,10) for(i=0;i