Compilation process overview
Lexical Analysis: scans source programs from left to right character by character to identify word symbols.
Example: regular and automatic machines
Syntax analysis-Based on lexical analysis, word sequences are divided into various syntax units based on the syntax rules of the language, such as expressions, statements, and programs.
Example: Type 0 grammar, context-related grammar, and context-independent grammar.
Semantic Analysis: Performs Type Analysis and checks. A Data Type in a programming language generally includes two aspects: the type carrier and its operation. For example, the remainder operator can only perform operations on integer data. If there is a floating point in the operation object, it is considered as a Type Mismatch Error.
Intermediate code generation-the intermediate code generated based on the output of semantic analysis. Intermediate code is a simple and clearly-defined mark system. It can be in several forms. Their common features are irrelevant to specific machines.
Semantic Analysis and intermediate code generation are based on semantic rules.
Code optimization-the optimization process can be performed in the intermediate code generation phase or in the target code generation phase. Optimization is based on the equivalent conversion rule of the program.
Target code generation-converts intermediate code into absolute code, relocatable code, or assembly code on a specific machine. The generation of target code is closely related to the specific code.
1. the compiler processes source programs in advanced languages in several stages, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and target code generation, among them, intermediate code generation and code optimization are not required by every compiler.
2. The compilation process of an advanced language source program is divided into several stages. The assignment register is used in the code generation phase.
3. When compiling a program to analyze the syntax of the C language source program, you can determine whether the variable is defined or declared.
Explain BASIC Program Principles
The interpreter works basically the same as the compiler in terms of lexical, syntax, and semantic analysis. However, when running a user program, it directly executes the intermediate representation of the source program or source program.
The interpreter does not generate the target program of the source program, which is the main difference between it and the compiled program.
Differences between compilation and interpretation:
· Efficiency: compilation may be more efficient than interpretation. The explanation method may need to scan the source program repeatedly during running. The compilation method creates the target program for a longer time than the explanation time, but the compilation method can be run multiple times.
· Flexibility: The interpreter needs to check the source program repeatedly, so it is more flexible than the compilation method.
· Portability: The interpreter is generally written in a programming language. Therefore, the interpreter can run in different environments as long as it is re-compiled.