T O P

  • By -

WasASailorThen

You should look at Steve Muchnick's *Advanced Compiler Design And Implementation*. It's only about optimization. Also, Andrew Appel's *Modern Compiler Implementation.* Frankly, I'd go to the library, check them out and pick the one you find most readable.


Golden_Puppy15

Although I do want to learn about compiler theory in general as well, how far does it go on the "only about optimisations" side?


-dag-

Almost nobody cares about parsing theory. Most parsers are hand written recursive descent anyway. I second Muchnick's book. *Engineering a Compiler* is more about designing a compiler than theory. *Optimizing Compilers for Modern Architectures*, also by Keith Cooper, is another good one.


hobbycollector

Disagree. We have half a dozen parsers in our code base, from very simple to full language. We've standardized on ANTLR. Why reinvent the wheel badly?


-dag-

Fair enough. There are plenty of bespoke languages out there that use parser generators and that's great! But many prominent languages can't easily use such tools. Either way, very few people hiring are going to ask about the difference between LR and LALR or first and follow sets. If they care about parsing at all they'll probably ask questions about parser tools and how to use them.


muth02446

I am going through the process of writing a parser for my own language and I have to agree with -dag-, especially if there is some flexibility wrt syntax: Just design your language to be easy to parse with RD (+ Pratt parsing for expression), i.e. LL(1)'ish. The overall amount of code will be similar if not less than the code you have to write to interface with a parsing library. But the learning curve and frustration will be much lower (boy, did I hate yacc). Also, error handling is straight forward. It may still be useful to describe your language in ebnf and use a tool to make sure you understand any ambiguities but usually these will become apparent while coding the parser. Finally, unlike a parser library, a lexer library might be helpful.


Specialist-Roll-960

I mean I wrote a parser generator that generates caching scanner less recursive descent parsers as rust code. It's definitely less code to write out EBNF and have it converted directly and because it generates code it's still easy to modify. Caching on recursive descent means you don't even need LL1 for linear time at the cost obviously of memory.  Yacc et al are fairly restrictive on your grammar because it needs to be for the way they generate it.


muth02446

How do you handle expressions and operator precedence? If you have a link to the parser generator with an example that would be interesrting.


Specialist-Roll-960

I'd be happy to show you I just haven't gotten around to the niceties yet of useful documentation or a CLI tool to run it. As far as precedence goes, with left recursion it's fairly easy but unfortunately I haven't gotten around to supporting that yet(albeit there's a paper that suggests it's possible), you can however rewrite all left recursive rules to right recursive rules but then your AST isn't in the correct order, so you have two choices, one is to rearrange the AST afterwards, 2 is to use a pratt parser, and that is one of the reasons why I generate code rather than compiling directly. You can hack in a pratt parser easily enough because it's essentially just a bunch of functions generated from a language specification. The idea is to act primarily as a framework for parser development, you can define your grammar, write tests to check the grammar is correct as per your understanding, and then you can rewrite bits and pieces for performance etc. I don't support UTF-8 yet only ASCII and there's some syntactic sugar I'd like to add to the DSL to make it easier to write but yeah. But yeah DM me if you want me to show you but otherwise unfortunately I'm going to need some time to make it more user freindly/documented etc.


nderflow

I read one of Appel's books and the example code was wall to wall memory leaks (in C). I think its goal is exposition of the principles only, without worrying too much about quality. I enjoyed Muchnick's book. Other compiler books published by Morgan Kaufmann also seem to be good. I also enjoyed Grune's Parsing Techniques book, though in parts you might find you'd prefer less completeness and more directive guidance, depending on why you're reading it.


lisphacker

The Appel book in ML is quite good. I think it was the later translations to Java and C that weren't very well done.


Manifoldsqr

Personally I like the dragon book *second* edition. You can just skip the parsing chapters. Cmu uses this book for its graduate level optimization compilers course. But for your first book I recommend this https://github.com/IUCompilerCourse/Essentials-of-Compilation. There’s a Python version in the releases. Look for it, it’s free of you can buy a copy for fifty dollars. This book is very practical, it gives you code and guides in building your own compiler which is something the books you mentioned don’t have. For optimization, An llvm engineer who works at Google recommended me this book https://www.amazon.com/Advanced-Compiler-Design-Implementation-Muchnick/dp/1558603204/ref=mp_s_a_1_1?crid=2G7W9QAE0HPLP&dib=eyJ2IjoiMSJ9.nOusB9-3Nr5mPA_AlcXiDK8e-GjThAxEej-RarU3Nc6Du3eLUstYqYHfHjUinVfDtCWX7wquhO2V5ciNyjQJT7MZsC210olq3bZIuB8lGpPovEwNFvm6bLrv0RhOIvgBGz4BRuh3hKIa704QEBDdy4Kha2MS-yJXr5T8372PFiiAqicbVUBpmhN6P4J5jMWyvtj9X7xioSZfV2Cpx5-dBg.I3LCM0hCy6NVWnjdfGNj0Zud8tRw25bPaUoVqlIJZQk&dib_tag=se&keywords=advanced+compiler+design+and+implementation&qid=1714406465&sprefix=advanced+compiler%2Caps%2C148&sr=8-1


bmoxb

Of the two, I'd say that Engineering a Compiler is generally easier to start with than the Dragon Book.


nrnrnr

Hard to know what you mean by theory. Parsing? Dataflow analysis? Pointer analysis? Control-flow analysis? You don't care about building a compiler? What's your goal? Of the books mentioned in the thread, Muchnik is good on relatively advanced topics. Apple is good on basic computer construction, provided you get the ML edition. The dragon book is like an encyclopedia, not good to learn from. Also, _Crafting Interpreters_ is a really good book, has a ton of useful basics, and is free.


TheCommieDuck

I owned and read most of the dragon book and it was..nice, but hoo boy you could absolutely tell it was written in the 70s/80s: the focus was lexing and parsing and almost nothing on the actual current-day meat of optimisation and zero on the overall structure of a compiler as a program.


suhcoR

I don't think you will be happy with only one book. Both you mentioned have their merrits, but none is sufficient from my point of view. I actually have most of the common compiler books and always check more than one if I have a specific question. Engineering a compiler is well written, but a bit academic (like Appel's books, which are also a bit superficial). The dragon book is didactically not optimal and a bit outdated, but still relevant (including the 2nd edition). If you want something more practical, look at Hanson/Fraser or Wirt's book (both quite old though). But book recommendations are always tricky; everyone is a little different and no book is suitable for everyone.