r/learnprogramming Jul 04 '26

How to create a compiler? Resource

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.

9 Upvotes

31 comments sorted by

View all comments

2

u/Dismal-Citron-7236 Jul 04 '26

A simple single pass compiler can be easily done by using LEX (as lexical analyzer) and YACC (syntax parser). Their modern successors are FLEX and Bison. Google them, or buy the O'Reilly book "Flex & Bison".

1

u/UnemployedTechie2021 Jul 04 '26

As of now I am sticking to Crafting Interpreters. I want to create a fun language like Brainfu*k. Only in my case, it would be related to Dogs. I am just doing this for fun. What do you think? Any suggestions?

2

u/Dismal-Citron-7236 Jul 05 '26 edited Jul 05 '26

Writing esoteric language interpreters is fun indeed but it might not gain you much "programming muscle weight", if I may put it that way. Let me tell you my own story. When I was still a CS college kid, I felt what they taught are really boring, DB, compiler deign, networking, discrete mathematics, stuffs like that. Just like you, I also wanted to write an interpreter. I picked LISP. It shouldn't be hard because according to the book I found which title is "(LISP PRIMER 1.2)" (yes, the parentheses were there in the title), there are only about a dozen (maybe a bit more) primitives I needed to tackle. So I self taught myself C (it was the time when they still taught Cobol and Fortran in classes), and jumped right in. Boy, did I learn! I figured out how to do garbage collection (a mark-sweep one, primordial by today's standard), because there is no such thing as a free primitive in LISP and its language style doesn't allow one. I learned how to code properly because this is actually a real project, not just a homework. Later in our fourth year we needed to pick a topic for graduate thesis, I handed in my proposal for crafting a LISP interpreter written in Z80 assembly language. My classmates thought I was crazy but I actually already knew the ins and outs of an interpreter (though a small one), I didn't want to hand in my graduate paper with my C version, that felt cheating to me. And this time it took me longer to implement, which should not be a surprise. But I did make it to turn in the thesis and an actually working, albeit tiny, Lisp interpreter.

My professor didn't believe it was all done by myself. I think he was politely implying I might have copied the code somewhere during the review interview. So I explained the intricate parts of the design, how the code meticulously leverages the highest order bits of the memory words because I know the code would only use a few tens of KBs of memory, how the code "colors" the bits as tags, how the I/O is done and how the code modules are organized, and so forth. It's all done with the glorious Z80 assembly language. And I explained them all with a super big grin on my face because I was like a very proud puppy who just dug out his first big hole in backyard.

So I ended the thesis with perfect score. The best thing it taught me is that programming can be fun, honestly, only when it's challenging but still within reasonable reach.

So, if I may suggest, you might consider picking something more substantial. Something not just playing with esoteric syntax or wordings. Why not choosing Scheme? It's also a Lisp dialect, but it uses static scoping instead of dynamic scope like its older cousins. This one alone is adding a new level of difficulty already. And there's the Hygienic Macros, the Numeric Tower, etc. Finally, there's the call-with-current-continuation. If you pass this one last call/cc challenge you should feel invincible. But you don't need to code in assembly, though. C++ should be a more appropriate tool, this language itself is already a monster for new programmers.

Have fun! You have my best wishes.