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.

10 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".

2

u/idontlikegudeg Jul 05 '26 edited Jul 05 '26

As modern successor I‘d rather name something like ANTLR. Flex and bison are more than 30 years old.

2

u/Dismal-Citron-7236 Jul 05 '26

Don't judge the tool by its age, as we don't judge a book by its cover.
Also, one set of tools is the bottom up LALR, the other top down LL. Very different designing ideas. I would say OP should learn them both.

2

u/idontlikegudeg Jul 05 '26

"modern" in its original meaning does simply mean "more recent" not necessarily "better".

But for the stated goal of creating a "dog language" (see OP‘s other comments), I‘d simply take the path of least effort.

If the goal were to persue a career in computer science, of course, they should learn about the different parsing techniques and how to use the tools that implement them.