The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Macros and Domain Specific Languages
by Christopher Diggins
July 9, 2006
A macro pre-processor which works as a type-aware pattern matcher can be used to transform language X into another language Y. In other words you could create a domain specific language from another language.


Many macro languages, like the C pre-processor (CPP), involve dumb token replacement using a pseudo-function syntax, but this is extremely limited. The desired use of a pre-processor is often to modify the syntax and semantics of a language, but rarely is it type-aware. This severely limits is usefulness. The C++ template system on the other hand is also a powerful macro system, which is type-aware but can't work with tokens. The two systems clearly need to be married.

The next logical step in creating a better macro language would be to eliminate the function syntax, and use pattern matching syntax. A macro can be more effective if it matches patterns in the source code (with or without types) and rewrites them as desired.

For example consider if "add" was a function primitive in a post-fix language:

  42 13 add_int
  4.2 1.3 add_float
Now say that like most sane individuals, you prefer infix notation. Let's introduce a macro system which works using pattern transforms:
  transform { x:int + y:int } => { x y add_int }
  transform { x:float + y:float } => { x y add_float }
Now you can write two infix expression in your postfix language:
  42 + 13
  4.2 + 1.3
So this has essentially allowed you to redefine the syntax for your postfix language to create a new infix language, which may be more appropriate for the problem at hand.

Talk Back!

Have an opinion? Readers have already posted 13 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at

This weblog entry is Copyright © 2006 Christopher Diggins. All rights reserved.

Sponsored Links


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use