There is a lot of variety when it comes to the different types of programmers. In general, is beneficial for a programmer to learn how to build a compiler? In what cases would
Compiler programming is multi-faceted since it includes parsing of code into logical trees and then translating code into another form of code. And potentially analyzing the input once its in trees for optimizations.
So in a round about way it will help every programmer, because you know how statements can be interpreted faster, or how to write a precompiler to make macros for your language of choice. Or even just how to parse that flat file better than your colleague.
For most script based languages, which don't have a compiler, you won't be able to optimize your day to day work flow much with learning how to make a compiler. But you will still understand parsing.
I've up-voted most of these answers.
I think compiler work gives you important ways of thinking about programming in general.
Whenever you write any program you are, in a sense, defining a language, and every program is, at some level, a language processor.
It makes you think about the best way to represent information, and understand that information can be encoded many different ways, not just as data structure and lots of classes.
Once you know that every problem has multiple valid solutions, with pros and cons, you can usually choose a much better solution than with the old data-centered one-size-fits-all paradigm.
I'm building a compiler as part of one side project and i must say it's a very satisfying task were you'll learn lots about how programming languages work and how code can be optimized.
knowing how code compiles and executes in both native and bytecode is also a great tool in C++ vs Java/C# vs C++/C# vs Java threads and flame wars ;)
In some ways, this question is a lot like "Should I learn C?"
As others have pointed out, some elements of a compiler — lexical analysis, parsing — can be used in many other applications. You'll learn some useful techniques even if you never have to implement a compiler.
In addition, the code generation phase will give you a better understanding of how the computer works. You'll see how the algorithms and data structures from higher-level languages actually get processed when they get to the CPU (or to the VM, as the case may be). This should help you write better algorithms in your day to day programming.
If you're designing your own language too, then you'll learn a lot through the process of thinking through the details of how it should work, what control flow elements you need, how expressions should be parsed, which order function paramaters should be read, etc. This should give you a better understanding of languages in general, which ought to make you a better programmer in whichever language you use.
Finally, there's always a chance you'll find yourself stuck with a legacy application in an old language that is no longer supported, and the easiest way to add new features will be to build your own compiler to extend the language.
I think my compiler & language theory course at University really had an enormous influence on my understanding of computer languages, even a decade afterwards. But I'm not really sure I'd need to implement a compiler for this.
I learned a lot.
Audrey Tang, developer of PUGS, recommends Types and Programming Languages.
Personally, I loved From NAND to Tetris - probably the best course I've taken, and a shining example of what higher education should be.