Interpreter and compiler, compiler principle and explanatory principle

Source: Internet
Author: User

Interpreter:

    • Interpreter (English: Interpreter), also translated as a computer program, can be a high-level programming language line direct translation run. The interpreter does not translate the entire program at once, just like an "intermediary", and each time the program is run, it is turned into another language to run, so the interpreter's program runs slower. It runs at once with every translation of a single line of programming, then translates the next line, then runs, so it goes on.
    • the way the interpreter runs the program is: 1. Run the Advanced programming language directly (such as the Shell's own interpreter) 2. Convert the high-level programming language code to some efficient bytecode (bytecode) and run these byte codes 3. Compile the high-level language with the compiler that the interpreter contains, and instruct the processor to run the compiled program (for example: JIT)
    • Interpretation has good dynamic characteristics and portability, such as the ability to dynamically change the type of variables during interpretation, modify the program, and insert good debugging diagnostic information into the program, and then port the interpreter to a different system, then the program can run on the system porting the interpreter without modification. At the same time, the interpreter also has a lot of shortcomings, such as inefficient execution, occupy large space, because not only to the user program to allocate space, the interpreter itself also occupies a valuable system resources.

Compiler:



    • This is because the computer does not directly recognize and execute the statements we write, it only knows the machine language (in binary form)
    • Compilation is the translation of the source program into an executable target code, translation and execution are separate, and interpretation is the source program translation and execution of a one-time completion, do not generate the target code can be stored. This is only the appearance, the biggest difference between the two is: for the interpretation of execution, the control of the program runtime in the interpreter and not the user program, to compile execution, run-time control in the user program.
    • Simply put, a compiler is a program that translates "one language (usually a high-level language)" into "another language (usually a low-level language)". A modern compiler's main workflow: source code → preprocessor (preprocessor) → compiler (compiler) → object code → linker (Linker) → Executable Program (E Xecutables)

Compiled and interpreted languages, mixed languages

To implement a language on a specific computer, the first thing to determine is the virtual computer that represents the semantic interpretation of the language, and a key question is whether the basic representation of the program execution is the machine language of the actual computer or the machine language of the virtual machine. This question determines the realization of language. According to the answer of this question, the programming language can be divided into two categories: compiled language and interpretive language.
compile-language:
Compiling means that the program source code is "translated" Into the target code (machine language) before the source program executes, so its target program can be executed independently from its language environment, which is more convenient and efficient to use. However, once the application needs to be modified, the source code must be modified before the new target file (*) is recompiled. OBJ) to execute only the target file without source code, modification is inconvenient. Most programming languages are now compiled. After the compiler translates the source program into a target program, it is persisted in another file, which can run multiple times directly on the computer from the compiler. Most software products are distributed to the user in the form of a target program, which is not only easy to run directly, but also makes it difficult for others to steal the technology C, C + +, Fortran, Visual Foxpro, Pascal, Delphi, Ada are all compiled and implemented.

interpreted language:
In the implementation of the interpretive language, the translator does not produce the target machine code, but produces an easy-to-execute intermediate code, which is different from the machine code, and the solution of the intermediate codeinterpretation is made bySoftware-supported, and cannot use hardware directly, the software interpreter often results in less efficient execution. A program written in an interpreted language is executed by another interpreter that can understand the intermediate code. Unlike the compiler, the task of the interpreter is to interpret the source program's statements as executable machine instructions, without having to translate the source program into the target code before executing it. The advantage of the release program is that when a syntax error occurs in a statement, the programmer's attention can be drawn immediately, and the programmer can correct it during the development of the program. For the interpreted basic language, a specialized interpreter is required to interpret the basic program, and each language is translated only when it is executed. This interpreted language is translated once per execution and thus inefficient. In general, dynamic languages are interpreted, such as Tcl, Perl, Ruby, VBScript, JavaScript, and so on.
Mixed type:
Java is very special, Java programs need to be compiled, but not directly compiled called machine language, but rather compiled called bytecode, and then on the Java Virtual machine in the interpretation of the code to execute bytecode. Python also uses a Java-like compilation pattern, which first compiles the Python program into Python bytecode, which is then interpreted by a dedicated Python bytecode interpreter to interpret the execution byte code. (Java Virtual machine Execution of bytecode is equivalent to simulating a CPU, and ruby1.8--before the virtual machine is present--is performed by interpreting it into a syntax tree.) )

The difference between the interpreter and the compiler:

Rookie often confuse compiler and interpreter, helpless, so to master consult.

Master said:

The interpreter is a one-piece explanation of the execution source language. For example, Php,postscritp,javascript is a typical interpretive language.

The compiler compiles the source code all the way to the target code, does not need the compiler at execution time, runs directly on the platform which supports the target code, so the execution efficiency is much faster than the interpretation execution. For example, C language code is compiled into binary code (EXE program), executed on the Windows platform.

"

 Rookie said: "I still do not understand, can give an image of metaphor?" ”

Master said: "Tell you a story." ”

My mother called her son and said, "Your father has not been in good health, but he is not busy at home."

Son think, yes, Dad is old, poor health, buy some yak bone marrow strong bone meal is good, so the son went to the supermarket to buy yak bone marrow zhuang bone meal.

Son again want Ah, recently gold partner is very fire, buy some give dad try. So the son went to buy a gold partner again.

After returning from the supermarket, the son also thought mother said "home not lively", well, the home of the little emperor since boarding school is relatively less back, it is estimated that the elderly want to grandson. The son made a phone call to his high-up son and asked him to come back at the weekend.

  

  My mother called her daughter and said, "Your father has not been in good health recently, but the family is less busy."

My daughter thought, what should I do for Dad? So she took out a piece of paper to start listing articles, first written on the strong bone meal and gold partner. Then, think of the old man to see the grandson should be good, so on the paper added a sentence, family back to parents there to visit. On the last piece of paper it reads:

1. Zhuang bone meal and gold partner

2. A family to visit their parents

After the daughter saw her son-in-law, the information on this piece of paper was sent to the working son-in-law.

Son-in-law to see it, after work to the supermarket to buy a tonic, and then drive home with his wife and children to visit his father-in-law.

Did you think of anything?

Rookie said:

I don't know if it's right: My son is like an interpreter, a little bit of thinking. Daughter is like a compiler, son-in-law is like a platform, the daughter after listening, on the paper listed all the things to do, son-in-law to follow the instructions.

Master said:

That's the way it is. Son of Mother's words is a piece of execution, the daughter is the mother's words throughout the translation into the platform can understand the target language-text messages, the whole by the son-in-law directly executed. The latter will be more efficient to execute.

Functionally, the interpreter and the compiler are really different.

However, both in terms of process and structure, they are very similar.

  When the son and daughter heard the mother's words, they were thinking from two aspects: the old man's body and the old man's thoughts on the junior. According to this, the son and daughter have made their own decisions. Just a direct to do, the other is to do the things translated into another carrier-SMS-to the storage.

The same is true of the interpreter and compiler, which, after being read into the source language, is parsed, parsed, and semantically parsed by both the interpreter and the compiler, and then they begin to differ. The interpreter chooses to execute the statement directly after the semantic analysis, and the compiler chooses to store the semantics in an intermediate language after the semantic analysis, and then translates it into different machine languages (executable program) by different backend. As shown in the following:

   In short, parsers and compilers are functionally different, but there are many similarities in structure, and there is no intrinsic difference in development, which is one of the reasons why many people confuse the two. Are you developing parsers or compilers? It is OK to make a decision based on the actual needs of the function.

Interpreter and compiler, compiler principle and explanatory principle

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.