How does the program language source code -> Bytecode （or Machine code ）-> implement There was interest , That's for today , Make a study plan for yourself ,
The goal is to be able to Java How to source code to class File bytecode , How can I JVM Have a deeper understanding of the implementation of the .
The first step of learning , Start with the hard Compilation Principle .
I have some understanding of compilation principle before , In particular, we have a better understanding of lexical analysis in our work , So learning starts with semantic analysis ,
The corresponding page number of Longshu is P195
1. Related definitions
SSD ： syntax-directed definition .
Each grammar symbol introduces a set of attributes , Each grammar production is equipped with a set of semantic calculation rules associated with it .
Attributes can be divided into two categories ：
Comprehensive attribute ： For bottom-up messaging , The attribute value can be calculated from the attribute value of its child node .
Inheritance properties ： Transfer information from top to bottom , It is calculated by the attribute values of the sibling node and the parent node of the node .
It can cooperate with semantic rules in the process of parsing and forming parsing tree SSD evaluation .
From the examples in the book , With bottom-up grammatical analysis SSD Evaluation can be done from low to high , Corresponding to comprehensive attribute evaluation .
And it's in line with top-down grammatical analysis SSD Evaluation involves evaluation of inherited properties , The order of evaluation is not so intuitive . For example 5.3.
3. Dependency graph and evaluation order
For convenience SSD evaluation , The concept of dependency graph is introduced , If a syntax analysis tree has a corresponding dependency graph , It can be evaluated according to the topological order of the dependency graph .
Topological sorting is not unique , chart 5.7 in 1 2 3 4 5 6 7 8 9 Is a topological sort , and 1 3 5 2 4 6 7 8 9 It's also a topological sort .
If a syntax analysis tree has a dependency graph containing rings , Topology sorting is not possible .
But there are two types SSD It can be determined that there is no ring in the dependency graph , So you can sort the topology and then sort it according to this order SSD evaluation .
These two categories are ：S Attribute SSD,L Attribute SSD.
4. S Properties and L attribute
If one SSD Each attribute is a comprehensive attribute , That is S Type .
If one SSD The attribute of is either a comprehensive attribute , Either it only depends on the attributes of the upper parent node or the left sibling node , That is L Type .
Finally, upload two calculation examples of comprehensive attribute and inherited attribute ：
（1） Example of comprehensive attribute calculation
（2） Inherit attribute calculation instance