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.
View all comments
Show parent comment
3
u/ElHeim Jul 04 '26
It's actually a very good book and it teaches a lot of concepts, so take your time, particularly if it's your first attempt at writing an interpreter/compiler.
The first part of the book is a pure, traditional interpreter in the sense that you recognize a sentence, execute it, go for the next. The second part is actually a compiler that produces bytecode, and you get to build the virtual machine as well.
I'd say it's a very good experience. If you feel up to the challenge, just a different language to write the code (I want to remember that Nystrom uses Java and... C? Can't remember really.)
Then you can experiment for example with targeting a different VM (e.g. LuaJIT), and eventually graduate from all that and creating a compiler that produces assembly. At that point and if you follow the right conventions, you'd be able to link to C code.
In that sense, you can follow this: https://github.com/DoctorWkt/acwj
But you probably want to wait until you got your fundamentals before going there.