Is it possible to compile c code using python?

Sure, why not? Of course, you'd need GCC installed (or llvm) so you have something to compile with. You can just use os.system, or any of the other ways for calling an external program.

Of course, you're probably better off looking at something like SCons, which already exists to solve this problem.

Plus, to answer the question actually asked, there's nothing that would prevent you from writing a compiler/assembler/linker in python, they're just programs like anything else. Performance probably wouldn't be very good though.


Step 1. Get PLY. Python Lex and Yacc. http://www.dabeaz.com/ply/

Step 2. Find a Yacc/Lex configuration for C. http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

Step 3. Tweak PLY to use the C language rules you found.

Step 4. Run. You're "compiling" C code -- checking the syntax.


If I understood you clearly, you just want to run compiler with some arguments from python?

In this case, you can just to use os.system. http://docs.python.org/library/os.html#os.system

Or better way is module "subprocess". http://docs.python.org/library/subprocess.html#module-subprocess