7 ms·
As I believe that there are a lot of compiler people around at HN, I would like to ask: What would be a good starting point to learn about writing compilers? I
by pcr910303 7y ago
As I believe that there are a lot of compiler people around at HN, I would like to ask:
What would be a good starting point to learn about writing compilers? I have written a small tree-walk language (and is following the byte-code based language) following Crafting Interpreters[0], but I would like some more learning resources.
I would ideally like some online book (I prefer readable resources) that are similar style with Crafting Interpreters, but more focusing on compilers & optimizing.
I have heard about the Dragon book, but it was too big and pricey :-(, I'm not sure if I can go through the entire book.
[0]: http://craftinginterpreters.com http://craftinginterpreters.com
- brudgers 7y agoThe classic is Crenshaw's Let's Build a Compiler. Yes, it's in Pascal. You probably don't use Pascal. https://compilers.iecc.com/crenshaw/ https://compilers.iecc.com/crenshaw/
- coliveira 7y agoPascal is the ideal language to learn compilers, because the language is so easy to parse. Nonetheless, it is still a real language, much closer to C than, let's say, Lisp.
- brudgers 7y agoI wasn’t trying to say Pascal is problematic beyond toolchain logistics. Pascal is no longer mainstream and using it probably means using a text editor or an unfamiliar IDE. Compared to more familiar languages, you’re pretty much on your own to figure out tools unless you already use Vim or Emacs. Which why people use Vim and Emacs. Now you have three problems...without regular expressions.
- pjmlp 7y agoFreePascal, Delphi and Oxygen, already three possibilities. And there are a couple of other variants still around. Lazarus, Visual Studio Code, Delphi IDE, Oxygen IDE, Emacs, Vim, Eclipse as IDEs.
- brudgers 7y agoExactly. 3 x 8 logical combinations. And 3x operating systems. Without Notepad, Sublime, and gedit. It’s not insurmountable but it ain’t nothing. And it isn’t writing a compiler either. Everyone doesn’t walk in my shoes.
- pjmlp 7y agoSo how that is any different of multiple combinations of C compilers and respective IDEs/Editors?
- brudgers 7y agoLet's Build a Compiler doesn't use C?
- pjmlp 7y agoSure, and I still fail to see why going through it and using Pascal is supposed to be a problem.
- brudgers 7y agoThe toolchain probably isn't on your computer. And the internet provides 72 options (or 99 if you consider Notepad, Subline Text, and gedit).
- coliveira 7y agoWhy is the text editor even an issue? Anyone able to learn how to write a compiler is fully capable of deciding which editor or IDE to use.
- brudgers 7y agoThe book isn't for people who are able to write compilers. When I discovered Hacker News, I'd never heard of Python. When I joined, I used Visual Studio Express, or rather Expresses because each was language locked. Obviously, I ran Windows. It was still four years until I bit the Emacs bullet. Yeah, installing and running Pascal wouldn't be a problem for me now. But I remember a time when it would have been. I remember what that was like. Half of all programmers are below average. I'm certainly one of them.
- coliveira 7y agoI don't understand why you're comparing Pascal to vim and Emacs. You don't even need to use Pascal to create a compiler in Pascal, do you understand that? I once created a simple Pascal compiler using C, and it was a simple project, much easier than parsing any other language. You can use any language you want, even Python would work.
- q3k 7y ago> I have heard about the Dragon book, but it was too big and pricey. Don't feel bad about acquiring US textbooks through alternative means like libgen. Their exorbitant prices are an effect of the student loan cartel.
- commandlinefan 7y agoI've been picking up older (but still perfectly relevant) textbooks from half-price books for years now. I have a hardcover copy of the dragon book that I bought for $5.99.
- mehrdadn 7y agoIf you're interested in the Dragon Book, the older edition is dirt-cheap ($3.08 as of right now): https://www.barnesandnoble.com/p/compilers-alfred-v-aho/1100358241/2662242152923 https://www.barnesandnoble.com/p/compilers-alfred-v-aho/1100...
- pcr910303 7y ago> If you're interested in the Dragon Book I'm def. interested, but I'm not sure if: * it's something that I can read for a week or two, for someone to use play with compilers & things (doesn't look like it) * and if it'll pay off, as I live in a non-US country which means cheap editions are still pricey due to shipping.
- mehrdadn 7y agoAnother option is to just find undergrad course materials for a compilers course and just go through it. Lecture slides can be great; sometimes they have other reading materials too. That should get you up to speed with the fundamentals fairly quickly, and most likely that's all you're looking for. If after that you're decently good with those and want more, you can look at grad-level materials, though in my personal opinion the materials for those can be quite a bit more dry (and painful) if you try to self-study, at least unless you find a particularly good source.
- 4ad 7y agoI have written the Go arm64 and sparc64 compilers, as well as several other proprietary production compilers for several languages. The Dragon Book is the best, for sure, and in my opinion essential material. Many people find its style too dry. It's basically a reference, not a tutorial. If you want something more hands on, I recommend this book about LCC[1]. It covers LCC, which is a C compiler. It's not a generic book about writing compilers, but I believe to be very useful anyway because it's very hands on. The current version of LCC is here[2]: Not sure if you can regenerate the book out of the current code or not (LCC is written in literate programming style, and the book used to be just the literate part). [1] A Retargetable C Compiler: Design and Implementation (Addison-Wesley, 1995, ISBN 0-8053-1670-1) [2] https://github.com/drh/lcc https://github.com/drh/lcc
- return_0e 7y agoYes, I find that The Dragon Book to be a fundamental reference in my university days of studying compilers. Even so, many sections of it are still relevant to this day. Also, I think I might have seen your name before whilst looking at the Solaris port of Go years back. Mind you, I'm looking at bringing Golang to Haiku (0) with similar changes like Solaris and Windows. (0) https://github.com/golang-haiku/go/tree/golang-1.11-haiku https://github.com/golang-haiku/go/tree/golang-1.11-haiku
- 4ad 7y agoCool, I am very interested in seeing Go work on all platforms!
- e19293001 7y agoBest book in my opinion: Compiler Construction Using Java, JavaCC, and Yacc, IEEE/Wiley, 2012 Please look at my past comments[0][1]. After that, read the Dragon Book as others had recommend. It would be smooth read after reading the first one. [0] - https://news.ycombinator.com/item?id=18996703 https://news.ycombinator.com/item?id=18996703 [1] - https://news.ycombinator.com/item?id=10184364 https://news.ycombinator.com/item?id=10184364
- rvz 7y ago> What would be a good starting point to learn about writing compilers? You also might find the LLVM/Clang kaleidoscope based tutorials interesting to get started having an insight into writing an LLVM-based compiler. Also Adrian Sampson has a great article in introducing LLVM for new-comers [1]. [0] https://llvm.org/docs/tutorial https://llvm.org/docs/tutorial [1] https://www.cs.cornell.edu/~asampson/blog/llvm.html https://www.cs.cornell.edu/~asampson/blog/llvm.html
- pcr910303 7y ago> You also might find the LLVM/Clang kaleidoscope based tutorials interesting to get started having an insight into writing an LLVM-based compiler. Hmm, I've always assumed that the tutorials were for the people who already 'know' compilers enough, it's great that it's more accessible than I thought. Definitely will going to look through them. Great resources :-)
- vrotaru 7y agoIf Haskell is your thing, this may be of interest https://lukelau.me/kaleidoscope/ https://lukelau.me/kaleidoscope/
- mhh__ 7y agoEngineering a compiler is great. The dragon book is pretty dated in certain areas e.g. SSA is mentioned once AFAIK and never used.
- userbinator 7y agoCrenshaw's tutorial is an excellently lucid read: https://compilers.iecc.com/crenshaw/ https://compilers.iecc.com/crenshaw/
- protomikron 7y agoMy humble opinion: do not get the Dragon book - it puts too much focus on parser theory and implementation which is in my opinion not the hard part of compiler construction. Most modern languages use a handwritten recursive descent parser anyway and the interesting part starts after you have the AST (code generation and register allocation).
- cbsmith 7y ago> which is in my opinion not the hard part of compiler construction It depends on whether you are compiling C++ or not. ;-)
- dfox 7y agoParsing C++ is not that hard if you use hand rolled recursive descent parser. On the other hand hacking some parser generator framework to be able to parse C++ is hard and IMO mostly pointless busywork.
- chrisseaton 7y ago> hacking some parser generator framework ... is hard and IMO mostly pointless busywork Bingo. We spent decades formalising and automating something... that was never that hard in the first place.
- dfox 7y agoIn context of parsing the situation when all the state machine/language theory has it's place is when you have some reason to care about compiler passes and generate output while reading input. That is, when you are not constructing some intermediate in-memory representation of whole compilation unit, presumably because you are running on system with (very) limited memory. For programming language compilers this ceased to be interesting limitation in 80's.
- nemoniac 7y agoNot that hard? It's literally undecidable! https://news.ycombinator.com/item?id=21154565 https://news.ycombinator.com/item?id=21154565
- pjmlp 7y agoI would advise the tiger book instead, pick your favourite flavour of it, ML, Java or C. There are some typos there, but it does provide a better experience developing a language and all compiler phases. https://www.cs.princeton.edu/~appel/modern/ https://www.cs.princeton.edu/~appel/modern/
- ernst_klim 7y agoI concur, an incredible book. Simple, neophyte-friendly, yet deep. Unlike Dragon Book, which should have been called Parsing Book, this is actually focused on compiling and languages.
- lixtra 7y agoI found great joy in "Compiler Construction" by N. Wirth. [1] Okay, that was more than 20 years ago. [1] https://news.ycombinator.com/item?id=10764672 https://news.ycombinator.com/item?id=10764672
- z29LiTp5qUC30n 7y agoThe best things about compilers is that they have gentle ramps for learning. If you start simple enough, https://github.com/oriansj/M2-Planet https://github.com/oriansj/M2-Planet the rest just becomes just add one more thing... It even works if you start from zero: https://github.com/oriansj/mescc-tools-seed https://github.com/oriansj/mescc-tools-seed
- devnonymous 7y agoIf you'd like a gentle introduction to something like gcc itself before diving in to writing your own, then you may want to read through: https://siddhesh.in/posts/gcc-under-the-hood.html https://siddhesh.in/posts/gcc-under-the-hood.html which gives some introduction into how a modern compiler like gcc is designed/structured. For context, the author, siddhesh is a current glibc maintainer.
- jcranmer 7y agoWhat are you interested in learning? Compilers is quite a broad field, consider: * Lexing/parsing * Classical scalar optimizations * Loop (vector) optimizations * Code generation trifecta of scheduling, register allocation, and peepholes * Dataflow and static analysis * Interprocedural, link-time, and whole-program optimization * JITs (to the extent that it differs from the above) * Garbage collection * A variety of advanced topics, such as decompilation, dynamic binary analysis/rewriting, superoptimization, formal verification, etc. There's no resource that's going to cover everything well, and some of them (particularly the last topic) are going to best covered by reading the research papers that come out at the premier compiler conferences such as PLDI. The Dragon book, for example, mostly covers parsing, classical scalar optimizations, and code generation, giving you very little to go on for interprocedural optimization, for example. Allen & Kennedy's "Optimizing Compilers for Modern Architectures" is going to be a better resource for loop optimization. If you want to get these textbooks for a cheaper price, try finding a nearby university, looking up when they offer a compiler course, and seeing if the bookstore has a used copy before the semester starts. I was lucky in that the compiler class had been cancelled immediately before the semester started, so the entire pallet of books was available, offering me my choice of highest quality used book.
- ufo 7y agoCrafting Interpreters is a gem. You chose a very good starting point.
- markus_zhang 7y agoHave you tried the book "Game Scripting Mastery"? It lays out the detail steps to construct a C-like scripting language and a Virtual Machine to run on top of a customized byte-code.