Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much of the compiler should we know?

In order to write better code, is it worth to know deeply what the compiler does?

Just how much would be enough? I'm not a bit scrubber, but I was thinking that knowing how the compiler operates would make me a better programmer. Am I wrong?

If so, what resources would you recommend?

like image 625
Decio Lira Avatar asked Dec 09 '08 03:12

Decio Lira


People also ask

Do I need to know about compilers?

Anyone who does any software development needs to use a compiler. It is a good idea to understand what is going on inside the tools that you use. Compilers are sophisticated text processors. Most programs need to do some text processing, even if only to read in the contents of a configuration file.

Is the compiler ever wrong?

Yep. If the spec says "code X does Y" and a compiler takes code X and does Z, then it is quite simply wrong. You have to deal with it, but it's still a compiler bug. Even if you can use a patched compiler, the compiler is always right until you can require everyone who wants to compile the code to use a fixed compiler.

Are compilers difficult?

Compilers and interpreters are not hard to write. They involve a few well known algorithms/patterns, and are fairly straightforward. If you want to make use of tools like Yacc and Lex or their derivatives, a complete functional compiler can be done in hours.

What should I learn before compiler design?

You should have good knowledge and understanding of any programming language such as Java or C to learn the concept of compiler design. Also, good knowledge about the concept of Assembly Programming would be an added advantage for learning the concept of compiler design.


2 Answers

Probably wouldn't hurt to know how the compiler will optimize your code, but don't write for the compiler, but write for people to read.

Writing code in a way that is more optimized for the compiler may make it more difficult for people to read it, and these days the compiler probably knows better to optimize the code for you.

like image 154
coobird Avatar answered Sep 28 '22 07:09

coobird


Without any proof of effectiveness at all, I feel better about understanding what happens to my code for knowing just a little about compilers and a bit of assembly. You can learn a lot by reading the Jack Crenshaw's Let's Build a Compiler.

Then you might look into more sophisticated compiler methods if you find yourself interested.


Edit: It is also worth noting that a lot of problems that don't call for a "compiler" are still best served by compiler methods. Parsing any modestly complicated command language is a compiler problem, even if you are not writing an executable.


Edit2: Many of the usual texts take a fairly abstract, mathematical approach to the compiler problem, which can be intimidating or confusing at first. The Crenshaw tutorial takes a "start banging out code" approach that is informed by the author's more subtle understanding. Nice intro, but if you are serious your should follow up with a more formal study.

like image 29
dmckee --- ex-moderator kitten Avatar answered Sep 28 '22 08:09

dmckee --- ex-moderator kitten