[Compilation Principles] Overview of Compilation Principles——Summary of Basic Knowledge Points

halo~I’m bay_Tong Tong Xiaobai. The
content of this article is a summary and sharing of what he has learned by Tong Xiaobai. The knowledge points will be edited and updated from time to time. For the latest updates, please refer to the update log. Welcome to leave a message, Pointing

【Update log】

Recently updated:

  • There is no editing content yet, it is being updated continuously...
Structure map

Insert picture description here

Translator, compiler, interpreter, assembler

Translation program: Translate a program (source program) written in a computer programming language into an equivalent program (target program) in another computer language. The program that completes this translation is called a translation program. Compilers, interpreters, and assemblers are all considered to be translators

Insert picture description here
Compiler: The source program language is a high-level language, and the target program language is a low-level language such as assembly language or machine language. Such a translation program is called a compiler
Insert picture description here
interpreter: the working principle of the compiler in terms of lexical, grammatical, and semantic analysis Basically the same, but the target program is not generated during the execution process, but the source program or the internal form of the source program (intermediate code) is directly interpreted, that is, the execution is performed while the source program is being interpreted.
Insert picture description here

Assembler: The source program language is assembly language, and the output target program language is machine language. Such a translation program is called assembler.
Insert picture description here
Usually the target program file cannot be directly executed by the CPU, and the executable program can be generated after connecting through the linker. .EXE. Modern compilers usually include the following main workflows:
Insert picture description here

or
Insert picture description here

Compile more basic concepts and related knowledge content in the column of "Microcomputer Principles and Interface Technology"

The basic structure of the compilation process and the compiler

The compilation process is mainly divided into five stages (lexical analysis, grammatical analysis, semantic analysis and intermediate code generation, code optimization, and target code generation). The tasks of the five stages are completed by five programs.

Overview of Lexical Analysis

The task at this stage is to scan and decompose the strings that make up the source program from left to right. According to the lexical rules of the language, the words with independent meaning are identified, the type of the word is determined, and the recognized words are converted into a unified machine. Internal representation-lexical unit (token) form. The program to accomplish this task is a lexical analysis program

[Simply put, lexical classification] Take two lines of C language code as an example
Insert picture description here
token: <species code, attribute value> as
Insert picture description here
an example:
Insert picture description here

Syntactic analysis overview

The task of this stage is to identify various phrases from the token sequence output by the lexical analyzer based on the grammatical rules of the language based on lexical analysis, and construct a grammatical analysis tree. The program to accomplish this task is a syntactic analysis program

[Simply put, the analysis and construction of the grammatical structure. Such as variable = expression constitutes an assignment statement]
Insert picture description here

Overview of semantic analysis and intermediate code generation

First, conduct a static semantic review of each grammatical unit, and then analyze its meaning, and use another language form (an intermediate code closer to the target language than the source language or directly in the target language) to describe this semantics. The program to complete this task is semantic analysis and intermediate code generation program

[Simply put, semantic review and intermediate code generation]

The main tasks of semantic analysis:

1) Collect the attribute information of the identifier

  • Species: simple variable, compound variable (array...), process...
  • Type: integer, real, character, boolean, pointer...
  • Storage location, length
  • value
  • Scope
  • Parameters and return value information: number of parameters, parameter types, parameter transfer methods, return value types...

2) Semantic check

Insert picture description here
Commonly used intermediate representation forms: three-address code (composed of a sequence of instructions similar to assembly language, which can be expressed as quaternary, ternary, and indirect ternary), grammar structure tree (referred to as grammar tree) [PS: grammatical structure Trees are different from parse trees]

Code optimization overview

Perform equivalent transformation or transformation to the intermediate code generated in the previous stage, in order to obtain a more efficient (saving time and space) target code. The program to complete this task is a code optimization program

Optimization mainly includes local optimization and loop optimization to make it run faster, or take up less space, or both

Overview of object code generation

Transform the intermediate code into absolute instruction code or relocatable instruction code or assembly instruction code on a specific machine. An important task of object code generation is to allocate registers reasonably for the variables used in the program

[Simply put, the target code is generated by the optimized intermediate code conversion]

Form management and error handling

In each stage of the compilation program, table management [record variable names, attributes and other related information used by the source program] and error handling [errors and accurately report the type and location of the error] are involved in the table management program. And the error handler is completed

The above are the main components of a typical compiler. The structure diagram is as follows:
Insert picture description here
Compiler front-end and compiler back-end:
Insert picture description here
Advantages: clear program logic structure; more optimized optimization

PS: The work of several adjacent and related stages in the compilation process can be carried out together, such as syntactic analysis for sentence structure analysis and direct semantic analysis combined with semantic rules. This technology is called grammar-guided translation.

Compiler generation method

Generally, the following aspects should be considered when generating a compiler:

  • Careful analysis of the source language and target language
  • Design compilation algorithm
  • Select language programming
  • Debug compiler
  • Submit relevant documents

Continuously updating...
I am Tong Xiaobai, a computer noob

Guess you like

Origin blog.csdn.net/bay_Tong/article/details/114286621