Compilation Principle - Compiler

xiaoxiao2021-03-06  41

Word method analysis

The lexical analyzer identifies the respective marks in the export program according to the term rules, and each mark represents a class of words. The common marks in the source program can be classified into a variety of categories: keywords, identifiers, literals, and special symbols. The input of the lexical analyzer is the source program, and the output is the identified marker stream. The words of the lexical analyzer is to convert the character stream of the source file into a marker stream. Essentially it looks at the continuous characters and then identifies them as "words".

2. Grammatical analysis

The syntax analyzer identifies the structure (phrase, sentence) in the marker stream according to the syntax rules, and constructs a grammar tree that can correctly reflect the structure.

3. Semantic analysis

Semantic analyzer performs static semantic semantics in the syntax unit in the syntax tree according to the semantic rules. If type checks and conversion, the purpose is to ensure that the correct structure of syntax is also legal.

4. Middle code generation

The intermediate code generator generates an intermediate code according to the output of the semantic analyzer. The intermediate code can have several forms, and their common feature is independent of the specific machine. The most commonly used intermediate code is a three-in-three address code. It is an implementation of four yuan. The advantage of the three address code is to read, easy to optimize.

5. Middle code optimization

Optimization is an important part of the compiler. Since the compiler translates the source program into an intermediate code is mechanical, according to the fixed mode, the generated intermediate code is often a lot of waste on time and space. Optimization must be made when generating an efficient target code is required.

6. Target code generation

The target code generation is the last stage of the compiler. Consider the following questions when generating a target code: The system structure of the computer, the allocation of the instruction system, register, and organization of memory, etc. The target program code generated by the compiler can have a variety of forms: assembly language, repositionable binary code, memory form.

7 symbol table management

The role of the symbol table is the necessary information of the symbol in the source program and is reasonably organized to quickly and accurately find and operate them at all stages of the compiler. Some of the symbol tables is even retained to the running phase of the program.

8 error handling

There are often some errors in the source program written by the user, which can be divided into static errors and dynamic errors. The so-called dynamic errors are logical errors in the source program, which occurs when the program is running, also referred to as a dynamic semantic error, such as the variable value is zero as the division, and the array element is bound to the boundary. Static errors can also be divided into syntax errors and static semantic errors. Syntax errors refer to errors on language structures, such as words spell fair, missing operands, begin and end mismatch in expressions. Static semantic errors refer to the language sense of the language that can be discovered when the analyzing source program, such as one of the two operands of the addition is an integer variable name, and the other is a group name, etc.

转载请注明原文地址:https://www.9cbs.com/read-75049.html

New Post(0)