Compiler development: uses the graph topological sorting algorithm to calculate dependencies between source code files.

Source: Internet
Author: User

The mainstream compilers we usually use have multi-source code file support. for example, to define some classes in the corresponding files and use these classes, you need to include the files that define the classes (such as C ++ ), or reference the namespace of the class (such as Java), or reference the file as a unit (such as Object Pascal)
To implement a compiler that supports multiple source code files, compile the files referenced by the source code before compiling a source code file. for example, there is a source file. SRC, which defines a class with the following content:
Class list
{
Public void add (Object OBJ)
{
...
}
}
Then there is a source file B. SRC, which uses the List class. The content is as follows:
Using "A. SRC"

Class Test
{
Public static main (string argv [])
{
List objs = new list;
List. Add (10, 20); // syntax error
}
}
Compile B. if. if the SRC file is not pre-compiled, the compiler cannot identify the List class or determine whether the list class has the member function add or whether the list of parameters called for the add operation is correct. in this case, you need to analyze B. which files are referenced by Src, which are referenced by other files, and the files at the top of the reference list are compiled first, and so on.
For example, the following source code files A, B, C, D, and E. References are as follows:
Reference A: B, c
Reference B: D, E
Reference C: B, E
D reference: E
E does not reference other files. The compilation sequence is as follows:
E d B C

In addition, mutual references are not allowed in the file reference relationship, which leads to compilation failure.

After learning why source code dependency is calculated, you can start to implement specific algorithms. You can perform this step after lexical analysis and before syntax analysis. because after lexical analysis, it is easy to analyze which other source files are referenced by a source file. If you put this step in preprocessing, you also need to annotate it, word splitting and other work produce unnecessary repetition.
The algorithm for calculating source code dependency is relatively simple. You can first regard all source code files as one vertex. If one vertex (source code file) references another vertex, add an outbound edge from the current vertex to the referenced vertex. After adding the outbound edge of all vertices, under normal circumstances, these vertices form a directed acyclic graph, such as: (if a ring occurs, it indicates that the source code file produces a wrong circular reference)

In this case, you can use the topological sorting method of no successor vertex in the graph. If there is no successor, the degree is 0 (that is, there is no outbound edge ), that is, each time you delete the vertex with a degree of 0 and the inbound edge of the vertex, It is deleted until the vertex with no degree of 0 is deleted. If the number of deleted vertices is smaller than the number of all vertices in the graph, this graph has loops and an error is reported. the algorithm is described as follows:
// G is the Graph
Void firsttopsort (g)
{
While (G contains vertices with an outbound degree of 0)
{
Find a vertex v with an outbound degree of 0 from G and output V;
Delete all input edges of V and V from G.
}
If (number of output vertices <G's vertices)
Printf ("circular reference exists in the source code file! ");
}
As follows:
Delete the inbound edges of vertex E and E whose first degree is 0, and record E

In this case, if the outbound edge of D is 0, delete the inbound edge of D and D, and record

Delete and record B again

Delete and record C, and finally
The record order is the result e d B C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.