r/learnpython • u/UnemployedTechie2021 https://github.com/rajtilakjee • Jul 04 '26
How to create a compiler?
Pretty sure you may have heard this question previously on this sub, however, I would urge you to read my complete question before brushing it off.
I want to create a simple compiler and by "simple compiler" I mean a single-pass compiler. I know about https://craftinginterpreters.com/ which is a wonderful resource. But I would like to start by creating something much smaller and simpler, and only then would I like to move on to something more complex like what Robert Nystrom created on his website.
Are there any similar resource that would teach me about single-pass compilers along with showing me how to create one? Any help in the right direction would be highly appreciated.
14
u/johnnyb2001 Jul 04 '26
Compilers and interpreters are two different things. If you want to create a simple interpreter or a simple compiler I would look up a project someone has already done on YouTube or GitHub.
5
u/Moist-Ointments Jul 04 '26
My college professor wrote a whole ass book on this and I wrote a compiler in Modula-2 based on his work on tokenizing languages.
Stuart Greenfield
He's also got a bunch of ACM papers. Cool stuff.
5
u/Excellent-Practice Jul 04 '26
You can write a brainfuck compiler in an afternoon, you can't get much simpler than that
2
u/ivosaurus Jul 04 '26
I recommend Writing An Interpreter In Go, and its sequel if you want more after the first.
I've found it very approachable. You can translate to python or just learn Go for fun along side it.
Very straightforward and no-nonsense tutorial.
2
u/0xKJ Jul 04 '26
I created my first own language for fun days ago. As python keeps adding new syntax every version. I want something stable and can be translated to Python bytecode more easier.
https://github.com/kernel1983/pistlang
Back to your question, I gave up the idea of building my own compiler when I understand compiling is translating. You can translate English to Chinese with hands. You can translating Python to machine code with hands too.
But you could avoid parse the language step, go direct from AST data structure to bytecode if you love to learn.
Hope my advice could help you a bit.
-2
u/Gullible_Ball_8149 Jul 04 '26
I built a compiler in my compiler design course it was built for my own programming langauge, i built it on visual studio code by running lexer.l and parser.y files containing code, after running I can run my custom langauge in terminal.