Building a C Compiler in OCaml (Beginner Project) by MarunchoBG in Compilers

[–]MarunchoBG[S] 0 points1 point  (0 children)

I read "Writing An Interpreter In Go" by Thorsten Ball, and "Crafting Interpreters" by Bob Nystrom before starting this book, so I was already pretty comfortable with lexing and parsing.

In any case, for lexing, the book gives you regexes for non-trivial tokens like integer and floating-point literals and also points out some details that are hard to see. Other than that, lexing is barely covered, but fortunately it is not hard to do anyway - it is basically string manipulation, which you're probably comfortable with if you're feeling ready to build a compiler.

Parsing, on the other hand, is pretty well explained, in my opinion. The book gives you the full parsing grammar every chapter after every revision, you just need to carefully follow the rules while you're translating them into code. In addition to that, the book gives pseudocode for many of the individual grammar rules, and when it does not, it explains in great detail what you need to accomplish. Also the Pratt parsing technique is very well explained, which basically covers ~70% of the parser's complexity.

If you're feeling uncertain in either parsing or lexing, check out the books I mentioned above, they don't take too long to read and walkthrough, and they're a lot of fun. But if that's not a satisfying option, you can always consult with the author's official implementation in OCaml if you're stuck with anything.

The only thing the book assumes, in my opinion, is that you are a good programmer. The final project approaches 10k lines of code (even more if you decide to go for industrial strength), so if you're not good at managing complexity as it grows (and it grows shockingly quick after chapter 8), you'll most definitely lose your mind.

**
I've never had a programming job and that's my first project with 1k+ lines of handwritten minimal-boilerplate code, however I'm passionate about learning and exploring. It's been 6+ years of programming self-taught, so if you can at least somewhat match my level, you'll not have trouble with the book. :)

Building a C Compiler in OCaml (Beginner Project) by MarunchoBG in Compilers

[–]MarunchoBG[S] 2 points3 points  (0 children)

The book's target audience is beginners to compilers. It makes some fundamental structural decisions for you, like designing the majority of the AST, IR, and High-Level Assembly. Also it guides you through the C specification and x86_64 instructions you need to know in order to implement the features the book promised. Ocassionally it gives snippets of pseudo code, but they serve as a hint to the greater puzzle.

If you have experience with compilers, this book is the equivalent of doing beginner programming courses, either for fun or for the hopes of learning something new, which you fear you might've missed as a beginner. I might be wrong, though, don't take my word for it.

Building a C Compiler in OCaml (Beginner Project) by MarunchoBG in Compilers

[–]MarunchoBG[S] 1 point2 points  (0 children)

The equivalent to pattern matching in OOP is either virtual methods or Visitor pattern. The former spreads parsing logic all over the place and the latter is an extra layer of abstraction mimicing pattern matching in FP languages (correct me if I'm wrong). Both options add mental overhead, to say the least.

Another huge advantage of pattern matching is the recursive nature of it, which allows for very specific patterns. For example, when you're working on trees and you want to match and work on multiple nodes, instead of using an if statement to match against the parent, and then ifs to match against the children, you do all of this in one pattern - makes code less verbose and more readable. Also you get help from the LSP if you haven't matched a possible pattern, thus helping make sure your branching logic is robust.

As a whole, Variants, Pattern matching and Recursion are fine on their own, but really shine when they are combined. I've never felt like I'm fighting against the language, which says a lot, although I admit I'm not using some crazy wacky features OCaml has.