Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

compiler construction - How does compiling circular dependencies work?

I've made the example in Java but I think (not tested) that it works in other (all?) languages.

You have 2 files. First, M.java:

public class MType {
    XType x;
    MType() {x = null;}
}

Second, another file (in the same directory), XType.java:

public class XType {
   MType m;
   public XType(MType m) {this.m = m;}
}

Ok it's bad programming, but if you run javac XType it compiles: compiles even MType because XType needs it. But ... MType needs XType ... how does that work? How does the compiler know what is happening?

I would like to know how the compiler (javac or any other compilers you know) manages that situation, not how to avoid it.

I'm asking because I'm writing a precompiler and I would like to manage that situation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to take a 2-pass, or multi-pass approach:

Languages like Java require a multi-pass compiler since the definition of x would not be required to come before the use:

public class Example {  
public static void main(String [] args) {
    assert(x==0);           
    x++;
    assert(x==1);
}
static int x=0;
}

There are various approaches, for example you could do the following:

The first pass could look for all variable declarations, the second for method declarations, etc. until the last pass uses all this information to compile the final code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...