🌐
GitHub
github.com › Hyper5phere › simple-c-compiler
GitHub - Hyper5phere/simple-c-compiler: Simple C Compiler written in Python
Simple C Compiler written in Python. Contribute to Hyper5phere/simple-c-compiler development by creating an account on GitHub.
Starred by 66 users
Forked by 19 users
Languages   Python 97.1% | C 2.9%
🌐
GitHub
github.com › ShivamSarodia › ShivyC
GitHub - ShivamSarodia/ShivyC: C compiler created in Python.
C compiler created in Python. Contribute to ShivamSarodia/ShivyC development by creating an account on GitHub.
Starred by 1.1K users
Forked by 83 users
Languages   Python 81.2% | C 18.2%
🌐
GitHub
github.com › cython › cython
GitHub - cython/cython: The most widely used Python to C compiler
The most widely used Python to C compiler. Contribute to cython/cython development by creating an account on GitHub.
Starred by 10.5K users
Forked by 1.6K users
Languages   Python 51.9% | Cython 38.3% | C 9.4% | C++ 0.3% | Shell 0.1% | Starlark 0.0%
🌐
GitHub
github.com › ShichenLiu › C-compiler
GitHub - ShichenLiu/C-compiler: C to LLVM / Python compiler
C to LLVM / Python compiler. Contribute to ShichenLiu/C-compiler development by creating an account on GitHub.
Starred by 10 users
Forked by 3 users
Languages   C++ 67.1% | Yacc 18.8% | C 7.5% | Lex 5.5% | Makefile 1.1%
🌐
GitHub
github.com › ShivamSarodia › ShivC
GitHub - ShivamSarodia/ShivC: A small C compiler written in Python for learning purposes
A small C compiler written in Python for learning purposes - ShivamSarodia/ShivC
Starred by 41 users
Forked by 39 users
Languages   Python 92.8% | C 6.9% | Shell 0.3%
🌐
GitHub
github.com › python › cpython › blob › main › Python › compile.c
cpython/Python/compile.c at main · python/cpython
* This file compiles an abstract syntax tree (AST) into Python bytecode. * * The primary entry point is _PyAST_Compile(), which returns a · * PyCodeObject. The compiler makes several passes to build the code · * object: * 1. Checks for future statements.
Author   python
🌐
GitHub
github.com › mypyc › mypyc
GitHub - mypyc/mypyc: Compile type annotated Python to fast C extensions
Mypyc compiles Python modules to C extensions. It uses standard Python type hints to generate fast code.
Starred by 1.9K users
Forked by 47 users
🌐
GitHub
github.com › Jamesbarford › python-c-compiler
GitHub - Jamesbarford/python-c-compiler: A toy c compiler written in python
A toy c compiler written in python. Contribute to Jamesbarford/python-c-compiler development by creating an account on GitHub.
Author   Jamesbarford
🌐
GitHub
github.com › jaredp › PythonCompiler
GitHub - jaredp/PythonCompiler: A Python to C compiler research project
A Python to C compiler research project. Contribute to jaredp/PythonCompiler development by creating an account on GitHub.
Author   jaredp
Find elsewhere
🌐
GitHub
github.com › Nuitka › Nuitka
GitHub - Nuitka/Nuitka: Nuitka is a Python compiler written in Python. It's fully compatible with Python 2.6, 2.7, 3.4-3.13. You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
To ensure smooth operation of Nuitka, make sure to follow system requirements, that include the following components: ... You need a C compiler with support for C11 or alternatively a C++ compiler for C++03 [1].
Starred by 14.2K users
Forked by 748 users
Languages   Python 64.3% | C 34.0% | Jinja 1.3% | C++ 0.4% | Dockerfile 0.0% | Batchfile 0.0%
🌐
GitHub
github.com › eatonphil › pyc
GitHub - eatonphil/pyc: A Python to C compiler
A simple Python to C compiler written in Python making use of libpython.
Starred by 39 users
Forked by 6 users
Languages   Python 97.8% | C 2.2%
🌐
GitHub
github.com › topics › online-compiler
online-compiler · GitHub Topics · GitHub
Compile and run C, C++, Java, Nodejs, Python, Ruby, Go and PHP code online, directly from Github, Gitlab, Bitbucket, and GoByExample.
🌐
Reddit
reddit.com › r/python › c compiler written in python
r/Python on Reddit: C Compiler Written in Python
September 27, 2019 -

Fifty days ago, upon discovering that there were 50 days left in the decade, I decided to finally get working on a project I have meant to work on for several months. I am happy to say that I have completed the first leg of something which can compile most of the C Programing Language: https://github.com/CarterTS/CCompiler.
However, there are several caveats here, this is not a complete C compiler, it is missing support for floats, unions, enumerations, bit fields, along with several other smaller inconsistencies. Furthermore, the compiler does not output machine code in ELF files for linking. Instead, it outputs assembly. By default, it is configured to output assembly for an emulator I built specifically for this project to act as a test case (https://github.com/CarterTS/DemoProcessor).
This is where I would like to reach out to you for assistance, this compiler is far from perfect, and the most productive method for testing it I have found is by throwing code at it, seeing what breaks and fixing those bugs. I would greatly appreciate feedback on the code, the project in general, and especially the functionality. I would be very grateful for issues filed on either of these repositories.
Since the holiday season is coming to a close and with the return to school approaching in a few days, work on this particular project will slow somewhat, however, I am going to attempt to get weekly or bi-monthly updates together, along with something of a dev blog.
I would love to answer any questions you all may have, and of course, feedback is greatly appreciated. I hope you all have a wonderful new year and your code may be bug-free!

Top answer
1 of 3
7
I've written a couple of toy compilers myself. I also wrote a C Compiler written in Python, targeting x86 (actually a subset of C). In retrospect I should have targeted llvm. I noticed you hand-rolled your own parser. In my experience that was the most fragile part of the code and felt like a menial task to implement. I recommend using parser generators (i.e. flex/bison or ANTLR). For python bindings for flex/bison see: http://freenet.mcnabhosting.com/python/pybison/ ).
2 of 3
2
Not that I have looked at your project but if you want to test it some more, consider property based testing. Of course, it is challenging to come up with properties for a compiler but here are a few techniques that come to mind. ( A talk by Zac Hatfield-Dodds about property based testing with Hypothesis , and the Hypothesis Property based testing library ) Oracle Properties Consider a property like "If this program compiles with CCompiler, it should also compile with gcc" (that is if it allows a program to compile that gcc wouldn't it's probably a bug). (It may be hard for the testing framework to find a suficient number of random strings that are compile-able for this to be effective). Likewise you can test that "last implementation" has exactly the same behavior as "current" if you are refactoring and "code has exact same behavior" as current if you are adding optimizations (note that this won't hold true if you are using UB to add optimizations aggressively). Metamorphic Properties It is hard to come up with properties but we can think of changes to a compiling / runable program that we can predict the resulting change for, examples include: If Program A compiles then renaming any symbol x to a symbol y should * Not change the output of the executable * Change x in the asm to y but nothing else. Adding any whitespace to any existing whitespace should not change the asm (with the exception of one line comments where //hello world means something different than //hello\nworld. Generalized example take a program that you write by hand and eliminate all parts that are irrelevant to the output and ensure the output changes as expected. (I'll write to indicate a place to plug in the text for variable x (I would use {x} like in an f-string but I would then need to escape brackets in c code.) int fib(int n) { if (n <= 1) { return 1; } else { return fib(n-1) + fib(n - 2); } } could become for any alphanumeric (without leading number) strings x and y where x != y int (int ) { if ( <= 1) { return 1; } else { return (-1) + ( - 2); } } the only change to the output should be every instance of the symbol fib should now become whatever your random x was. If your compiler doesn't do this, it's almost certainly bugged. Reverse Oracle Write a "compiler" that translates your AST to a C program (just simple recursive text substitution should suffice). This can help you test Oracle Properties more easily (and you can do random text substitution to the resulting code after the "AST->C" transformation to get a really good distribution. The AST you get after parsing the "AST->C" output should be the same (or equivalent) to what you started with. Anyways, I am just listing these because I would have liked a list like this the last time I tried writing property tests for a compiler. and in my experience Property tests can be very good at finding either things you misunderstood about your problem "oh, not alphanumeric strings but alphanumeric strings with a leading alpha character" or bugs "oops my compiler crashes if a c file has no functions and only has constants". Therefore, you can use this for some help. Also, if you are looking for simple C-Programs (You'll have to check copyright issues yourself) you can check the benchmark games , game websites like codewars and Project Euler and C tutorials to features you have implemented and help websites like Stack Overflow. Unfortunately, I haven't checked each websites copyright policies on submitted code so the linked websites might not allow you to add these samples to your project as test cases.
🌐
Medium
medium.com › @pasi_pyrro › how-to-write-your-own-c-compiler-from-scratch-with-python-90ab84ffe071
How to write your own C compiler from scratch with Python!
October 4, 2020 - Furthermore, references for the interested readers are provided for reading all about the mindboggling details. Many pictures and examples are given as a serious attempt to make the task of the reader at least somewhat bearable. The full source code of the compiler implementation discussed in this blog post can be found from github.
🌐
GitHub
github.com › jiggum › c-compiler
GitHub - jiggum/c-compiler: Simple C compiler & interpreter written in Python
Simple C compiler & interpreter written in Python. Contribute to jiggum/c-compiler development by creating an account on GitHub.
Author   jiggum
🌐
Cython
cython.org
Cython: C-Extensions for Python
Making Cython a great programming ... with the Python ecosystem and the changing requirements of its diverse user bases, takes a lot of time and dedication. To support the maintenance and future development of the Cython language and compiler, YOU can sponsor the work of Stefan Behnel via: ... Note that PayPal takes 5 - 15% fees for small non-EUR payments, which is money that you pay without helping us. Consider signing up for a GitHub Sponsors ...
🌐
GitHub
github.com › SKantar › CInterpreter
GitHub - SKantar/CInterpreter: Small C interpreter written in Python from scratch. Project was written as a part of course Compiler Construction at The Faculty Of Computer Science.
CInterpreter is a very small C interpreter written in Python from scratch. Project was written as a part of course Compiler Construction at The Faculty Of Computer Science.
Starred by 52 users
Forked by 8 users
Languages   Python 95.3% | C 4.7%
🌐
GitHub
github.com › sidpro-hash › Online-Compiler
GitHub - sidpro-hash/Online-Compiler: Online Compiler for C,C++,JAVA,Python,JavaScript.
Online Compiler for C,C++,JAVA,Python,JavaScript. Contribute to sidpro-hash/Online-Compiler development by creating an account on GitHub.
Starred by 11 users
Forked by 8 users
Languages   PHP 30.0% | JavaScript 24.1% | Hack 19.3% | HTML 10.2% | CSS 9.7% | Dockerfile 6.4% | Shell 0.3%
🌐
GitHub
github.com › topics › python-compiler
python-compiler · GitHub Topics · GitHub
Nuitka is a Python compiler written in Python. It's fully compatible with Python 2.6, 2.7, 3.4-3.13. You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
🌐
GitHub
github.com › topics › c-compiler
c-compiler · GitHub Topics · GitHub
Repo dedicated to my Computer Logic subject on Insper. Goal was to develop a simple C Compiler with basic functions. ... Python script will be translated python code to pyd by Microsoft C/C++ compiler x64 (Visual Studio Community 2015).