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 be, or not be, needed?
I'm a web developer; I spend most of my time in PHP and MySQL. I don't think learning to build a compiler would benefit me much.
Different types of programmers will get differing amounts of benefit.
Bit of a non-answer, I know...
I think, it is better to learn from knowledge point of view. At least you can look into existing compiler source code and understand the typical compilation steps and complexity involved in each phase of compilation.
While few programmers will ever end up having to implement a compiler, the earlier stages of compiler building, namely lexing and parsing are something that can come up far more often: Who hasn't had to write a parser for some strange file format? Usually, those are simple enough to manage without experience in compiler building, but not always.
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.
Compiler programming is an interesting topic and there is some great value to it. At the college I went to it was an elective, Language Design and Implementation. I'm personally grateful to have taken it. We learned the various ways to implement lexers, parsers, and bytecode emitters.
The real value that I've seen is that it illuminated the black box that I depend on to get my program up and running. It gave me a better insight into how the compiler works, and helped me better understand compiler errors.
The process of compiling source into code is actually in a general way what most programs do, take some input, perform some process, and output the result. A compiler has some very well-defined ideas on how this should best be done.
I think in all it was beneficial to me, and currently I work on Java based web apps.
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.
Yes, it is a good idea. Learning how all this stuff works can only benefit the programmer. I've wrote a BASIC compiler in SX ASM and learned a ton from it.
As someone else mentioned though, there's many degrees of programmers and knowledge. A web dev who is mainly into markup & scripting languages probably wouldn't benefit as much from it as a hardcore C or ASM programmer who writes embeded systems software - that's not to say it wouldn't be useful knowledge though.
Re-inventing wheels is always useful for educational value.
Understanding the process of compilation is a general approach to understanding how the computer works, and therefore provides a broad scope of understanding. Many modern programmers work in complex environments that require little of this understanding, at least on basic levels. An example is java, which hides the linking step of compilation from the developer.
Knowledge is knowledge, it is almost always useful. In my case, I found understanding compilation process exceptionally usefull when doing performance enhancement, which is my job. (I work in a super low latency environment)
If you do all your work in PHP mySql that is great. Just remember that all technologies get outdated, and you will need to understand the next great thing. Having "general knowledge" like understanding compilation provides a conceptional buffer between you and those shmucks who can't adapt.
Learn how to learn.
I am in the process of reading through The Dragon Book (Compilers) and during the beginning of the book you are greeted with the following:
Although few people are likely to build or even maintain a compiler for a major programming language, the reader can profitably apply the ideas and techniques discussed in this book for general software design. For example, the string matching techniques for building lexical analysers have also been used in text editors, information retrieval systems, and pattern recognition programs. Context-free grammars and syntax-directed definitions have been used to build many little languages such as the typesetting and figure drawing systems that produced this book. The techniques of code optimisation have been used in program verifiers, and in programs that produce "structured" programs from unstructured code.
In short, you won't just be learning how to build a compiler. You'll be learning many different lower-level techniques along the way to assist you in everyday programming. Although some say it is a dated book I still enjoy it and I would recommend it, even though the reading can get a bit heavy. If you do get it leave a good amount of time to read it and understand it.
This is like asking "is it beneficial for a programmer to have more programming knowledge?". The simple is that yes, it is beneficial. How much will it benefit day to day non-compiler-building programing affairs is hard to guess. But it will definitely teach you about how the internals of what you are doing work, how to manipulate strings to dictate logic and possibly help you debug better regardless of what you use.
I took two compilers courses in university and found them useful because:
- Writing a compiler requires knowledge of a lot of areas of computer science - regular expressions, context-free grammars, syntax trees, graphs, etc. It can help you see how to apply the theory of computer science to real-world problems.
- By understanding how a compiler generates and optimizes code, you will waste less time doing foolish "optimizations" yourself.
- The process of scanning/lexing a file and building a syntax tree out of it is applicable to a much larger set of problems then just building a compiler.
- Helps "generalize" programming languages - once you see that every programming language ends up as machine code in the end, it makes it easier to learn new languages because you see that every language is just a different way of expressing the same basic ideas.
Some people would counter that there are more useful things you could be doing with your time, like learning a popular programming language or library that could help get you a job (this argument is often used as a reason not to learn assembly language). However knowing how compilers work will probably make it easier to learn new programming languages (see point #4).
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.
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 ;)
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 recently did an independent study on what we called Language Processing (my final project was no so much a compiler as a c++ file parser/interpreter with compiler-like features). I was required to use the "Dragon" book that was mentioned above. I thought that as a software developer this was one of the more important things that I have done in my college career. I found it not only interesting and rewarding for my own personal benefit but it also allowed me to see deeper into the language. On top of the because the Dragon book is not language specific it helped me to understand the similarities and more importantly the reasons behind the differences in different languages. I do however agree with the fact that not all programmers may find it necessary. Yet in the field of software development I think that if you have an interest in expanding your understanding of language design it can be very helpful to look at compilers.