Lucy
Lucy

Reputation: 491

Compiler Constuction

I am doing Compiler design and construction. I've been wondering why do we actually need six phases in compilers. could some one please point out some advantages of having a number of phases in compilation process?

Upvotes: 0

Views: 89

Answers (3)

Pascal Cuoq
Pascal Cuoq

Reputation: 80325

As delnan says, you should explicitly say which phases you are talking about. They are not so standardized that we know exactly the six you are talking about. A compiler is still an ordinary program, and like ordinary programs, may reflect the style of its authors.

Part of the answer is because compilation is a hugely difficult problem. I used to have the exact same question, but for what is probably only considered one phase in the 6-phase scheme you are thinking of: parsing. Why does everyone always first divide the input stream into tokens (lexing), and then only give structure to the sequence of tokens (parsing)? Why do people teach it as if it was the only way? Is there a theorem that says it has to be done this way?

In retrospect, the answer was that there may be other ways to do it, but this one was found to work, and since just the problem of converting linear text into structured ASTs is a hugely difficult one (when considered in abstracto, without the knowledge we have accumulated), any sub-division of the initial problem into well-identified sub-tasks helps. If a particular sub-division worked in the past, why not continue to do it this way?

Of course, parsing is itself only one sub-task in the hugely difficult problem of converting text to executable code. I suspect that the same empirical approach was used at each level of compiler design. The way we do it now reflects the history of past tries at the problem.

Upvotes: 4

Christopher Creutzig
Christopher Creutzig

Reputation: 8774

It is absolutely possible to write a single-pass compiler. It may not be a good idea, but it is possible, so no, we do not “need” six passes.

As @transporter_room_3 correctly pointed out, having multiple passes (and having those passes work with well-defined interfaces, more often than not by having all intermediary passes receive and emit the same IR) simply makes it easier to read and therefore to design and write as well as change and maintain the compiler.

As to why exactly six – I don’t think there is any agreement on that number, and there certainly are people who would count each distinct optimization module as a separate pass and therefore would, in their own count, happily change the number of passes frequently. I assume you are currently in a compiler course and your teacher just told you you were using six phases? Well, then there’s your answer: To make learning easier, we usually do not learn/teach the fully general picture right from the beginning. We look at some restricted area first and then later check which of the boundaries are just for learning and which ones are inherent in the problem. Or would you teach a four to six year old about sets and monoids before starting to count?

Upvotes: 2

transporter_room_3
transporter_room_3

Reputation: 2633

Modularity, simply. So when you want to write a compiler for a different language or target machine then you only have to change those modules.

Upvotes: 3

Related Questions