I Wrote a Compiler

ingve | 91 points

>The original authors of yacc were Mike Lesk and Eric Schmidt - yes that Eric Schmidt.

Incorrect, they were authors of lex. yacc was authored by Stephen Johnson.

Surprising to me is all the authors are still around, even though the tools are over 50 years old!. Shows how young computer science field is.

npalli | 15 hours ago

I have played around with the Crafting Interpreters book. Maybe the author oversimplifies a bit, but it seems with yacc and lex that nothing is really stopping anyone from making their own language and compiler. It always seemed like black magic.

But the same could be said about books, nothing is stopping you from writing a book except good ideas, story, and structure.

firesteelrain | 3 hours ago

This is very similar to the project I have in Chapter 2 of my new book Computer Science from Scratch [0]. It's also Tiny BASIC without INPUT. I called it NanoBASIC. But it's an interpreter not a compiler. This tutorial is a nice starting point. The chapter is much more comprehensive, so if you want to get into the weeds, I can recommend my own chapter (of course). But it's in Python, not Go. The code is on GitHub[1]. But this tutorial is great too.

0: https://nostarch.com/computer-science-from-scratch

1: https://github.com/davecom/ComputerScienceFromScratch

WoodenChair | 11 hours ago

> It’s possible to write the lexer and parser entirely by hand

I write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome.

One advantage of doing them by hand is better, more targeted error messages are easier to fold in.

WalterBright | 9 hours ago

TinyBASIC is fun and beautifully simple. I wrote a 3-part tutorial for making a TinyBASIC-to-C compiler using Python a few years ago.

Let’s make a Teeny Tiny compiler https://austinhenley.com/blog/teenytinycompiler1.html

azhenley | 14 hours ago

This is great! I feel like there's been a resurgence of interest in language design and compilers of late. I have no business having an interest in this kind of thing, but even I have been inspired to try and make the changes to javascript that I think would improve it: https://chicory-lang.github.io/

jcuenod | 2 hours ago

Since 1990, I have developed software with C and later C++. Now that I am working on a C compiler, I am learning new things about the language. So, writing a compiler (or an interpretter) can really help to get a deep understanding of a programming language.

fjfaase | 10 hours ago

>The original authors of yacc were Mike Lesk and Eric Schmidt - yes that Eric Schmidt.

I don't know if it's worth mentioning, but the author of the post is David Singleton, the former CTO of Stripe. I almost hadn't noticed until I saw the domain.

bastawhiz | 15 hours ago

I like these kind of fun projects!

I wrote a very small but complete compiler and VM for a very simple language: boolean expressions. I use it as a "what to expect" type of introduction during the first session of my compiler course.

The whole code is here, it is less than 150 lines of OCaml code (plus a few lines of C for the VM) and uses standard parsing tools: https://gist.github.com/p4bl0-/9f4e950e6c06fbba7e168097d89b0...

p4bl0 | 5 hours ago

BASIC is a good candidate for an easy language to implement. I'd also propose Pascal, Lisp (because S-expressions make parsing easier), and FORTH.

throw10920 | 2 hours ago

Notably, this is written by David Singleton, who at the time was the CTO of Stripe.

likium | 4 hours ago

How does one debug a grammar? This has always eluded me.

Where is the special tooling to help spot errors?

djoldman | 4 hours ago

> Yes, this is what I do for fun.

Don't we all? ;-)

musicale | 11 hours ago

nice to see that the red dragon book is still a thing. I've heard people have stopped using the wizard book.

kristopolous | 7 hours ago

Love reading these. Keep these blog posts coming!

matthewmueller | 13 hours ago
[deleted]
| 9 hours ago

Wait! What are == and != doing in a BASIC language? Heresy! :)

teo_zero | 10 hours ago

I thought a compiler, with no adjective or caveat, should turn a HLL into machine language. Isn't what this describes—turning BASIC into Go—more accurately described as a "pseudocompiler" or "Go compiler" or somesuch? I know Emacs is always said to have a "bytecode compiler" that processes Elisp code, not a "compiler" per se. Am I mistaken?

TMWNN | 15 hours ago

I wrote a full fledged interpreter by hand for a vintage BASIC language. I called it Basice (I'm Icemanind, so Basice is a play on BASIC and Iceman). It's a little outdated now though. I wrote it using C# and it doesn't use any external libraries, like yacc or flex. It's all by hand. You can see it at https://github.com/icemanind/Basice

icemanind | 13 hours ago

[dead]

DustinEgg | 3 days ago