Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Experimenting with various lisps lately (clojure especially) i have wondered if there are any s expression based representations of (subsets) of c, so you could use lisp/closure to write macros and then convert the s-expression c tree to pure c.

I am not asking for a to-c-compilers of lisp/scheme/clojure but more of using lisps to transform a c syntax tree.

Little background to why i am asking this question: i find myself to really enjoy certain clojure macros like the threading macros -> doto etc. And i feel that they would be great in a non FP environment as well.

share|improve this question
2  
    
You might also want to have a look at Nu language. Yes, it isn't for C but it is really cool! programming.nu –  Chiron Jun 29 '13 at 8:00
4  
Take a look at this C-on-top-of-Lisp compiler with macros: bit.ly/cux2i5 (sorry it's a zip file, apparently there is no project page or even a github repository). It parses C into S-expressions-based AST which is then type-checked and translated into an LLVM AST. There are two types of macros, first type is executed at the parsing stage and second type is what you're looking for, S-expressions based, executed during typing pass. See the examples in tests/ directory. P.S., it's a literate program, so here's a PDF: bit.ly/12wSMks –  SK-logic Jun 29 '13 at 9:42
add comment

1 Answer

You can get this from any AST representation of a C program. Just walk the tree and dump it as an S-expression. All you need now is a good C parser that will build an AST.

I understand you can bend GNU to do that. I don't know how.

There are tools that can export XML versions of the AST, which are trivially equivalent to S-expressions. Our DMS Software Reengineering Toolkit it one of these. It can even retain some of the preprocessor directives. Not a dump of C, but a dump of Java doing exactly the same thing can be found here.

What you will find a bit harder is to regenerate valid source text from the tree. If you can't do that, doing transformations on the tree seems pointless.

You wanted to do this to apply transformations to the C code. Yes, you can do with with tree operations in lisp relatively easily. But you have code those transformations in lisp. DMS provides the ability to carry out transformations, using the surface syntax of the language (e.g., transfomations written using C syntax for patterns). That makes them generally easier to write, because the surface syntax doesn't have to know the microscopic detail of the AST. (DMS also has built-in prettyprinting of the trees).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.