Java Code compilation process brief __java

Source: Internet
Author: User

Code compilation is done by the Javac compiler, as shown in Figure 1 below:


Figure 1

Javac is a compiler that translates a language specification into another language specification, usually compilers translate language specifications that are easy to understand into machines that are easily understandable, such as C + + or assembly language, which compiles the source code directly into the target machine code, This target machine code is a collection of instructions that the CPU executes directly. These instruction sets are a language specification at the bottom.

The compiler for Javac is also a language that compiles the Java-friendly programming language into pairs that are very friendly to all machines. This language is not for a machine or a platform. How to eliminate the differences between different kinds of platforms the task has a JVM to complete , and JAvac's task is to translate the Java source language into a language that the JVM can recognize , The JVM then converts the JVM language into the machine language that the machine can recognize.

The task of Javac is to compile the Java source code into Java bytecode, the binary code that the JVM can recognize, from the surface to the. java file into a. class file. Instead of translating the Java source code into a series of binary digits, these binary numbers are formatted, and only the JVM can actually identify what they mean.

What steps are required for the compiler to translate a language specification into another language specification. To answer this question you need to refer to the "compiler principle", the summary process is as follows:

1 Lexical Analysis: Read the source code, a byte of a byte read in, find out these lexical words we define the language keywords such as: if, else, while, etc., to identify which if is legitimate and which is illegal. This step is the lexical analysis process.

Lexical analysis results: from the source code to find some standardized token flow, like human language, give you a word you have to tell what is a word, which is the punctuation, which verbs, which are nouns.

2 Grammar Analysis: Is the analysis of the token of the word method to analyze the grammar, this step is to check the combination of these keywords is not in line with the Java language specification. If the following is not followed by a Boolean-judging expression.

The result of parsing: to form an abstract syntax tree in accordance with the Java language, the abstract syntax tree is a structured grammatical expression which is used to organize the main lexical morphology of the language in a structured form. The tree can be later organized in accordance with the new rules.

3 Semantic Analysis: After the completion of the grammar analysis there will be no grammatical problems, the main task of semantic analysis is to make some difficult to understand, complex grammar into a simpler syntax. As difficult to understand the classical Chinese into the hundred text that everyone knows, or to annotate some of the idioms do not understand.

Semantic analysis results: The complex syntax is translated into a simple syntax, corresponding to Java is to convert foreach to A For loop, and some comments. Finally, an abstract syntax tree is generated, and the syntax tree is closer to the grammatical rules of the target language.

4 bytecode generation: A byte code will be generated based on the annotated abstract syntax tree, which translates a data structure into another data structure. Just like to translate all the Chinese words into English words and then assemble the English sentences according to the English grammar. The result of the code generator is to generate bytecode that conforms to the Java Virtual Machine specification. The components required in this process are shown in Figure 2 below:


Figure 2

From the description above, we know that compiling is the decomposition of a language through analysis, Then in a certain way to form a simple framework (the Java source file byte flow into the corresponding token stream) and then in the detailed analysis according to certain rules in the this frame adds something to make this token flow form a more structured syntax tree (that is, to assemble a single word in the token stream that was generated earlier). , but this tree is still a little bit out of our target-java bytecode, so semantic analysis makes the rough tree more complete (add a default constructor to the class, check that the variable is initialized before use, check that the action variable type matches), The Javac compiler then calls the Com.sun.tools.javac.jvm.Gen class to traverse the syntax tree to convert a block of code in a Java method into binary data in the form of a command that conforms to the JVM syntax. Output bytecode to a file with class extension in the file organization format of the JVM, that is, to generate the final Java byte code. Lexical analysis is to organize keywords into token flow that is, check the source of the keyword is true and organized into token flow, and grammar analysis is to check whether the source code conforms to the Java Grammar specification and the word composition statement. Semantic analysis is to simplify the complex add missing, check whether the variable type is legitimate. Code generators are traversing this tree to generate code that conforms to the JVM specification .

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.