Talk about javac

The Java language has the Java language specification, which describes in detail the morphology and syntax of the Java language, and the Java virtual machine also has its Java virtual machine specification. Similarly, the Java virtual machine specification is different from the Java language specification. They all have their own lexical and syntax parsing rules, and the parsing rules are also different. So how to adapt the syntax rules of Java to the syntax rules of Java virtual machine? This task is done by the javac compiler. Its task is to transform the Java language specification into the Java virtual machine language specification and complete the "translation work".

This paper mainly focuses on the following three aspects:

First, what is javac;

Second, the basic structure of javac compiler;

1、 What is javac

Javac is a compiler that can convert one language specification into another. Usually, the compiler converts the language specification that is easy to understand into the language specification that is easy to understand by the machine. For example, C, C + +, python or assembly language directly compiles the source code into the target machine code, which is the instruction set directly executed by the CPU. These instruction sets are the underlying language specifications, which can be directly recognized by the machine, but it is impossible for people to write the target machine code directly. Although the execution of this machine code is very efficient, it is too unfriendly to people. The cost of developing this code is much higher than the execution cost of the saved machine. Therefore, there is a compiler. With a compiler, so many high-level languages can appear.

In a sense, the prosperity of programming language comes from the compiler, because the compiler is a link between human and machine. In retrospect, the javac compiler also compiles Java, a very friendly programming language, into a language that is very friendly to all machines. Note that this language is not specific to a certain machine, and even includes different types and platforms. How to eliminate the differences between machines of different types and platforms is done by the JVM. The task of javac is to convert the Java source code language into a language recognized by the JVM, and then the JVM converts the JVM language into a machine language recognized by the current machine.

So it seems that the Java language has one more layer of transformation than other languages (such as C language). Although this layer of transformation sacrifices some execution efficiency, it shields many details related to the target machine from the Java language developers, making the execution of the Java language irrelevant to the platform, which also makes the java language prosperous.

2、 Basic structure of java compiler

For example, take a brief overview of the operating principle of Java,

Generally speaking, high-level languages basically follow a general principle: source code - > machine code

technological process:

Java source file generates bytecode file (. Class) through Java compiler (javac), and then bytecode is transformed into machine code through Java virtual machine.

. java->. Class - > JVM - > machine code

It also shows that Java is one more step than other programming languages (such as C language), that is, JVM.

It can also be seen from the above figure that javac is mainly composed of four modules: lexical analyzer, parser, semantic analyzer and code generator.

Lexical analyzer:

Lexical analysis process: read the source code and identify keywords, such as if, else, while, for, swich, etc. Those are Java keywords and those are not.

The result of lexical analysis: find out the standard token stream from the source code. Just like in human language, give you a sentence. You can distinguish those nouns, verbs, punctuation, etc.

Parser:

Syntax analysis process: perform syntax analysis on the above token stream to check whether the combination of keywords meets the Java specification. For example, string s cannot be declared without assignment, because there is no default value for non basic data types.

The result of syntax analysis: an abstract syntax tree conforming to the Java language specification is formed,

For example:

Java code:

Abstract idiom tree:

Semantic analyzer:

The process of semantic analysis: difficult and complex grammar is transformed into simple and easy to understand grammar, which is related to the classical Chinese learned in school, that is, the classical Chinese is translated into vernacular.

Results of semantic analysis: complex grammar - > simple grammar

Code generator:

Generate bytecode file

This article mainly refers to in-depth analysis of Java Web technology

Summary:

Java needs to be converted into bytecode by the compiler, and the bytecode can be identified by the JVM to convert the machine code that can be executed by the CPU. High level languages such as C / C + + are directly source code - > compiler - > machine code.

When it comes to real life, such as translation officers, especially during diplomatic visits, there are usually translation officers around. The translator's contact with this article is equivalent to "compiler".

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>