[ACCEPTED]-Is it beneficial for a programmer to learn how to build a compiler?-compiler-construction
Compiler programming is an interesting topic 18 and there is some great value to it. At 17 the college I went to it was an elective, Language 16 Design and Implementation. I'm personally 15 grateful to have taken it. We learned the 14 various ways to implement lexers, parsers, and 13 bytecode emitters.
The real value that I've 12 seen is that it illuminated the black box 11 that I depend on to get my program up and 10 running. It gave me a better insight into 9 how the compiler works, and helped me better 8 understand compiler errors.
The process of 7 compiling source into code is actually in 6 a general way what most programs do, take 5 some input, perform some process, and output 4 the result. A compiler has some very well-defined 3 ideas on how this should best be done.
I 2 think in all it was beneficial to me, and 1 currently I work on Java based web apps.
I am in the process of reading through The 27 Dragon Book (Compilers) and during the beginning 26 of the book you are greeted with the following:
Although 25 few people are likely to build or even 24 maintain a compiler for a major programming 23 language, the reader can profitably apply 22 the ideas and techniques discussed in 21 this book for general software design. For example, the 20 string matching techniques for building 19 lexical analysers have also been used 18 in text editors, information retrieval systems, and 17 pattern recognition programs. Context-free 16 grammars and syntax-directed definitions 15 have been used to build many little languages such 14 as the typesetting and figure drawing 13 systems that produced this book. The techniques 12 of code optimisation have been used in 11 program verifiers, and in programs that produce 10 "structured" programs from unstructured 9 code.
In short, you won't just be learning 8 how to build a compiler. You'll be learning 7 many different lower-level techniques along 6 the way to assist you in everyday programming. Although 5 some say it is a dated book I still enjoy 4 it and I would recommend it, even though 3 the reading can get a bit heavy. If you 2 do get it leave a good amount of time to 1 read it and understand it.
I took two compilers courses in university 10 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 9 would counter that there are more useful 8 things you could be doing with your time, like 7 learning a popular programming language 6 or library that could help get you a job 5 (this argument is often used as a reason 4 not to learn assembly language). However 3 knowing how compilers work will probably 2 make it easier to learn new programming 1 languages (see point #4).
While few programmers will ever end up having 7 to implement a compiler, the earlier stages 6 of compiler building, namely lexing and 5 parsing are something that can come up far 4 more often: Who hasn't had to write a parser 3 for some strange file format? Usually, those 2 are simple enough to manage without experience 1 in compiler building, but not always.
I learned a lot.
Audrey Tang, developer of PUGS, recommends 3 Types and Programming Languages.
Personally, I loved From NAND to Tetris - probably the best 2 course I've taken, and a shining example 1 of what higher education should be.
Compiler programming is multi-faceted since 15 it includes parsing of code into logical 14 trees and then translating code into another 13 form of code. And potentially analyzing 12 the input once its in trees for optimizations.
So 11 in a round about way it will help every 10 programmer, because you know how statements 9 can be interpreted faster, or how to write 8 a precompiler to make macros for your language 7 of choice. Or even just how to parse that 6 flat file better than your colleague.
For 5 most script based languages, which don't 4 have a compiler, you won't be able to optimize 3 your day to day work flow much with learning 2 how to make a compiler. But you will still 1 understand parsing.
I think, it is better to learn from knowledge 4 point of view. At least you can look into 3 existing compiler source code and understand 2 the typical compilation steps and complexity 1 involved in each phase of compilation.
I think my compiler & language theory 5 course at University really had an enormous 4 influence on my understanding of computer 3 languages, even a decade afterwards. But 2 I'm not really sure I'd need to implement 1 a compiler for this.
I've up-voted most of these answers.
I think 13 compiler work gives you important ways of 12 thinking about programming in general.
Whenever 11 you write any program you are, in a sense, defining 10 a language, and every program is, at some 9 level, a language processor.
It makes you 8 think about the best way to represent information, and 7 understand that information can be encoded 6 many different ways, not just as data structure 5 and lots of classes.
Once you know that every 4 problem has multiple valid solutions, with 3 pros and cons, you can usually choose a 2 much better solution than with the old data-centered 1 one-size-fits-all paradigm.
Understanding the process of compilation 20 is a general approach to understanding how 19 the computer works, and therefore provides 18 a broad scope of understanding. Many modern 17 programmers work in complex environments 16 that require little of this understanding, at 15 least on basic levels. An example is java, which 14 hides the linking step of compilation from 13 the developer.
Knowledge is knowledge, it 12 is almost always useful. In my case, I 11 found understanding compilation process 10 exceptionally usefull when doing performance 9 enhancement, which is my job. (I work in 8 a super low latency environment)
If you 7 do all your work in PHP mySql that is great. Just 6 remember that all technologies get outdated, and 5 you will need to understand the next great 4 thing. Having "general knowledge" like 3 understanding compilation provides a conceptional 2 buffer between you and those shmucks who 1 can't adapt.
Learn how to learn.
This is like asking "is it beneficial for 9 a programmer to have more programming knowledge?". The 8 simple is that yes, it is beneficial. How 7 much will it benefit day to day non-compiler-building 6 programing affairs is hard to guess. But 5 it will definitely teach you about how the 4 internals of what you are doing work, how 3 to manipulate strings to dictate logic and 2 possibly help you debug better regardless 1 of what you use.
I'm building a compiler as part of one side 7 project and i must say it's a very satisfying 6 task were you'll learn lots about how programming 5 languages work and how code can be optimized.
knowing 4 how code compiles and executes in both native 3 and bytecode is also a great tool in C++ vs 2 Java/C# vs C++/C# vs Java threads and flame 1 wars ;)
In some ways, this question is a lot like 28 "Should I learn C?"
As others have 27 pointed out, some elements of a compiler 26 — lexical analysis, parsing — can 25 be used in many other applications. You'll 24 learn some useful techniques even if you 23 never have to implement a compiler.
In addition, the 22 code generation phase will give you a better 21 understanding of how the computer works. You'll 20 see how the algorithms and data structures 19 from higher-level languages actually get 18 processed when they get to the CPU (or to 17 the VM, as the case may be). This should 16 help you write better algorithms in your 15 day to day programming.
If you're designing 14 your own language too, then you'll learn 13 a lot through the process of thinking through 12 the details of how it should work, what 11 control flow elements you need, how expressions 10 should be parsed, which order function paramaters 9 should be read, etc. This should give you 8 a better understanding of languages in general, which 7 ought to make you a better programmer in 6 whichever language you use.
Finally, there's 5 always a chance you'll find yourself stuck 4 with a legacy application in an old language 3 that is no longer supported, and the easiest 2 way to add new features will be to build your own compiler to extend 1 the language.
I recently did an independent study on what 21 we called Language Processing (my final 20 project was no so much a compiler as a c++ file 19 parser/interpreter with compiler-like features). I 18 was required to use the "Dragon" book that 17 was mentioned above. I thought that as 16 a software developer this was one of the 15 more important things that I have done in 14 my college career. I found it not only 13 interesting and rewarding for my own personal 12 benefit but it also allowed me to see deeper 11 into the language. On top of the because 10 the Dragon book is not language specific 9 it helped me to understand the similarities 8 and more importantly the reasons behind 7 the differences in different languages. I 6 do however agree with the fact that not 5 all programmers may find it necessary. Yet 4 in the field of software development I think 3 that if you have an interest in expanding 2 your understanding of language design it 1 can be very helpful to look at compilers.
I'm a web developer; I spend most of my 5 time in PHP and MySQL. I don't think learning 4 to build a compiler would benefit me much.
Different 3 types of programmers will get differing 2 amounts of benefit.
Bit of a non-answer, I 1 know...
Yes, it is a good idea. Learning how all 12 this stuff works can only benefit the programmer. I've 11 wrote a BASIC compiler in SX ASM and learned 10 a ton from it.
As someone else mentioned 9 though, there's many degrees of programmers 8 and knowledge. A web dev who is mainly 7 into markup & scripting languages probably 6 wouldn't benefit as much from it as a hardcore 5 C or ASM programmer who writes embeded systems 4 software - that's not to say it wouldn't 3 be useful knowledge though.
Re-inventing 2 wheels is always useful for educational 1 value.
In todays' industry, if you can do a compiler, then 38 your like a 3 y/o kid thats' learning how 37 to count (that being the upper limit of 36 my intelligence gives me an IQ of about 35 18 as far as the field is concerned); and 34 its just as essential too: as quoted in 33 the dragon book, every application with 32 a user interface defines a programming language.
Furthermore, new 31 programming languages, such as zonnon and composita, use 30 syntax directed protocols for communication 29 between live objects. They define protocol 28 types which specify the interface to a server 27 thread as an EBNF grammar. That makes it 26 impossible to code the message handlers 25 if you havent' written a compiler!
Syntax 24 directed protocols are the best way to deal 23 with things like web servers that use text-based 22 protocols; so its very possible that this 21 will become the method of choice. However, both 20 languages restrict the protocols to LL(1) grammars 19 (for very obvious reason), and this may 18 prove to be too restrictive.
While the current 17 implementation of zonnon is still a bit 16 on the glitchy side; and the language definition 15 doesnt' seem to be complete yet either; i 14 will nevertheless be so bold as to venture 13 that zonnon (or something similar) will 12 put C# in the garbage bin where it belongs, if 11 they can get these issues ironed out.
Composita 10 is an intriguing language; but in the real 9 world, its prolly undesirable to require 8 every object to tbe a live thread and every 7 function call to be a message, since the 6 designers of composita had to override the 5 OS to make it run fast enough.
That having 4 been said, ill close by repeating myself: if 3 programming server applications does go 2 the way of EBNF protocol types, then you 1 had best learn how to write a compiler.
More Related questions