Lisp-like macros for C?

Discussion in 'C Programming' started by jsykari, Sep 30, 2009.

  1. jsykari

    jsykari Guest

    Has anyone ever implemented lisp-like macros for C?

    Or any similar pursuits?

    (I'm aware of MetaC, but that's not quite it.)

    jsykari, Sep 30, 2009
    1. Advertisements

  2. jsykari

    ld Guest

    Maybe the cpp library chaos-pp could be of interest for you?


    ld, Sep 30, 2009
    1. Advertisements

  3. what on earth would they look like? Isn't the point of lisp-like
    macros based on the programs-look-like-data idea?
    Nick Keighley, Sep 30, 2009
  4. jsykari

    Eric Sosman Guest

    I was working on some, but my parenthesis key broke
    from overuse.
    Perhaps if you'd describe what you mean by "it" and
    by "lisp-like," people would be able to help. As it is,
    I've no idea what you're looking for, or why.
    Eric Sosman, Sep 30, 2009
  5. I think this is one of those things where if you have to have it
    explained to you, you won't be of any help. I.e./OTOH, if you understand
    what the OP is talking about, then you are likely to be able to help him
    (and if not, you should just be quiet and move on).

    (Like you, I have no idea what he is talking about either)

    P.S. To the OP: And if you do explain it here (which you shouldn't have
    to [see above]), all you'll get for your trouble is abuse.
    Kenny McCormack, Sep 30, 2009
  6. jsykari

    Phil Carmody Guest

    I believe some were bandied around this newsgroup in about 1993-ish.
    They wouldn't fool a real lisp user, but let one create reasonably
    lisp-like chunks of code. As a one-time scheme hacker, they at least
    raised a chortle here for being a good attempt. I couldn't recreate
    them if I tried, alas, which is a shame.

    Phil Carmody, Sep 30, 2009
  7. jsykari

    jsykari Guest

    Let me elaborate (the risk of abuse notwithstanding):

    Lisp macros (the ones in Common Lisp, not the hygienic variety
    provided by Scheme) are
    an instance of compile-time metaprogramming, which lets the programmer
    do arbitrary
    AST-level transformations to the program code before or during

    The difference between standard C preprocessor and Lisp macros is that
    Lisp allows you to
    use Lisp itself to perform the transformations, therefore giving much
    more power to the

    I'm looking for a macro language/preprocessor for C that gives me the
    power of C to preprocess C programs. In practice, this would mean that
    the compiler/preprocessor would have to include a C interpreter and
    facilities for generating and analyzing C constructs.
    Programs-look-like-data certainly helps when implementing them, but is
    not a necessity.

    Examples of metaprogramming/macro systems that have "real syntax"
    include MetaLua (, Template Haskell (http://, Converge (
    1.1/ctmp/) and Nemerle (
    Thanks for the pointer. I've taken a look at Chaos and Boost
    libraries but they don't quite cut it. While Cpp can be made to
    impressive feats, it is still basically a glorified search-and-replace

    jsykari, Oct 1, 2009
  8. jsykari

    jsykari Guest

    I found a paper 1993 from Weise et al., "Programmable Syntax Macros"
    Perhaps this is the same thing you remember:

    Anyway, this was pretty much what I was looking for.

    jsykari, Oct 1, 2009
  9. I've never seen this done in C (which isn't proof of nonexistence).

    (IME) the prevailing/popular practice in C when you want more than the
    standard preprocessor gives is *separate* tools that generate C source
    -- either general-purpose like awk and perl (both somewhat C-like but
    not totally) or special-purpose like IDL (RPC) compilers, embedded-SQL
    mappers, autoconf, etc. C-pp does have #line and #file directives to
    help such separate tools be more transparent.

    perl does have eval; (standard/common) awk doesn't, but it can invoke
    itself as a child process which gives the same power more clumsily.
    None of the tools I know has parsing that would handle full C as
    input; they do sometimes use C *style* for more limited input.

    These kinds of techniques handle a LOT of useful cases. But if you
    need (or want) complete generality -- any bounded manipulation of any
    representable program -- I can't help you.

    FWIW, PL/I (I believe standardly) has a preprocessor with syntax
    slightly tweaked (mostly adding some percent signs) and (quite!)
    subset semantics (but still useful). And, inevitably, suffered from
    the unresolvable debates between 'heavy preprocessing can much better
    express the problem domain' and 'heavy preprocessing makes it too
    difficult to understand what the actual code is'.
    David Thompson, Oct 12, 2009
  10. True, AFAIK, the PL/1 PP is Turing complete, but you need PL/1 itself.
    For an arcane, but free and widely deployed PP. look up M4 used in
    and sendmail.


    PS.: or write your own - it's not that difficult, we did this to
    handle different DBMS
    (those are tricky, as some intercept before the C PP, and so don't
    know macro expansions)
    wolfgang.riedel, Oct 14, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.