Click here to Skip to main content
13,705,765 members
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 6 Apr 2005

A Macro Preprocessor in C#

, 6 Apr 2005
Rate this:
Please Sign up or sign in to vote.
This library supplies the same macro substitution facilities as the C/C++ preprocessor.


The C preprocessor is a useful tool that allows programmers to work around the limitations of C. It was so abused to do clever and un-maintainable things that C++ programmers now avoid it, and in fact, it has been left out of more modern languages like C#. If you really wanted to, you could put a separate preprocessor step in (using a standalone preprocessor like CPP) but you would probably not be thanked by those who have to read the code later.

The class I'll present here does the lexical substitution part of the C preprocessor. You can add macros which behave like C macros and which can do useful things like 'stringizing' and token-pasting. Then calling the Substitute() method will return your string after all macros have been expanded. It is not intended to replace standalone C preprocessors (that has been done repeatedly) but it is sometimes convenient to have the ability to make complex macro substitutions. I wrote this class as part of an interpreter project, and it makes typing in an interactive session much easier. It's also a cool way to explore C macro substitution interactively, and shows how .NET regular expressions can make complex text substitutions easier. The full implementation is only 130 lines of C#, including comments!


System.Text.RegularExpressions.Regexp is a marvelous class and everyone should be familiar with it. It is very useful when making intelligent, possibly context-sensitive replacements in text. The idea is to find the match, and then depending on the matched value, replace the match with some other text. For instance, the formal parameters of a macro are the dummy variables which were used when defining it; the preprocessor will then find the actual parameters and have to substitute these into the macro text.

Here is an example from MacroSubstitutor where we have to replace the formal parameters of a macro with the actual parameters. The code has been slightly simplified (ignoring '#') to show the match/replace loop more clearly.

public class MacroEntry {
        public string   Subst;
        public string[] Parms;

    static Regex iden = new Regex(@"[a-zA-Z_]\w*");            
    public string ReplaceParms(MacroEntry me, string[] actual_parms) {
        Match m;
        int istart = 0;
        string subst = me.Subst;
        while ((m = iden.Match(subst,istart)) != Match.Empty) {
            int idx = Array.IndexOf(me.Parms,m.Value);
            int len = m.Length;
            if (idx != -1) {
                string actual = actual_parms[idx];
                subst = iden.Replace(subst,actual,1,istart);                
                len = actual.Length;
            istart = m.Index + len;
        return subst;

Please note the overloaded forms of Regex.Match and Regex.Replace that are used here. They allow you to specify the starting position where the matching and replacing begin and how many replacements to actually make (by default, it does a global replace, which is not good for context-sensitive replacements). Obviously, it could be faster, but it's fast enough for what I needed it to do, and (most importantly) it's concise and readable code.

The tricky method is MacroSubstitutor.Substitute, because we have to be careful extracting the arguments passed to the macro. At first, this seems easy; grab the string up to ')' and split using ','. But consider FOR(i,f(x,g(y))); it is necessary to carefully count the bracket level and only pull in arguments at level one. Not everything can be done with regular expressions, and sometimes a loop is more obvious.

int idx = 0, isi = i;
while (parenDepth > 0) {
    if (parenDepth == 1 && (str[i] == ',' || str[i] == ')')) {
        actuals[idx] = str.Substring(isi,i - isi);
        isi = i+1;  // past ',' or ')'
    if (str[i] == '(') parenDepth++;  else
    if (str[i] == ')') parenDepth--;

The interesting point is that the results of substitutions are themselves examined for any new macros to be substituted. We start our next match at the newly substituted text. It works like this:

FOR(i,N)                        // original string
for(int i = 0; i < (N); i++)    // 'FOR' is substituted
for(int i = 0; i < (20); i++)   // 'N' is substituted

Using the code

The class is straightforward to use:

static void Test() {
    MacroSubstitutor ms = new MacroSubstitutor();
    ms.AddMacro("FOR","for(int i = 0; i < (n); i++)",new string[]{"i"});

There's an even easier method, where you let the class handle the macro definitions. These look just like C preprocessor #defines, except that I prefer #def because it's easier to type in a hurry. Here is the complete source for a simple interactive preprocessor.

class MacroTest {
    static string Read() {
        Console.Write(">> ");
        return Console.In.ReadLine();
   static void Main() {
       MacroSubstitutor ms = new MacroSubstitutor();
       string line;
       while ((line = Read()) != null)

Here is an example of an interactive session:

>> #def N 20
>> #def write Console.WriteLine
>> #def FOR(i,n) for(int i = 0; i < (n); i++)
>> FOR(k,N) write(k);
for(int k = 0; k < (20); k++) Console.WriteLine(k);
>> #def quote(x) #x
>> #def cat(x,y) x##y
>> quote(cat(dog,mouse))

Occasionally, you will need to customize the lookup. The example here is where we want to replace DATE and USER with their values at the time of substitution. MacroSubstitutor provides a CustomReplacement method which can be overridden; it will be called if the macro replacement for a symbol is a special value MacroEntry.Custom:

class MyMacroSubstitutor : MacroSubstitutor {
    public override string CustomReplacement(string s) {
        switch(s) {
        case "DATE": return DateTime.Now.ToString();
        case "USER": return Environment.GetEnvironmentVariable("USERNAME");
        return "";

static MacroSubstitutor ms = new MyMacroSubstitutor();


Alternatively, we could have used a delegate here, but a good old-fashioned virtual method does the job nicely. When C# 2.0 becomes widely available, it will be a pleasure to be able to write code using anonymous delegate functions:

ms.AddMacro("DATE",delegate(string s) {
    return DateTime.Now.ToString();


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Steve Donovan
Web Developer
South Africa South Africa
Steve is the author of 'C++ By Example'
(Que 2002) and developer of the UnderC interactive C++ interpreter.

You may also be interested in...

Comments and Discussions

GeneralDoes the job Pin
Guirec5-Aug-15 23:40
memberGuirec5-Aug-15 23:40 
GeneralMy vote of 1 Pin
hardik antala2-Aug-11 22:58
memberhardik antala2-Aug-11 22:58 
GeneralRe: My vote of 1 Pin
Thomas Levesque8-Mar-13 10:36
memberThomas Levesque8-Mar-13 10:36 
GeneralProblem in code Pin
interarticle5-Mar-10 17:52
memberinterarticle5-Mar-10 17:52 
GeneralDoesn't Work Pin
Christopher Stratmann14-Nov-07 6:15
memberChristopher Stratmann14-Nov-07 6:15 
GeneralRe: Doesn't Work Pin
klinkenbecker11-Aug-18 3:49
memberklinkenbecker11-Aug-18 3:49 
GeneralThanks Pin
Greg Roberts11-Apr-05 20:12
memberGreg Roberts11-Apr-05 20:12 
GeneralRe: Thanks Pin
Steve Donovan12-Apr-05 21:19
memberSteve Donovan12-Apr-05 21:19 
GeneralRe: Thanks Pin
necropower22-Feb-06 10:19
membernecropower22-Feb-06 10:19 
GeneralRe: Thanks Pin
Greg Roberts17-Apr-05 1:04
memberGreg Roberts17-Apr-05 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180920.1 | Last Updated 7 Apr 2005
Article Copyright 2005 by Steve Donovan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid