当前位置:网站首页>Compilation principle reading notes (1/12)
Compilation principle reading notes (1/12)
2022-07-05 12:43:00 【NLP journey】
The first 1 Chapter Introduction
1.1 What is a compiler
Compiler is the translator of programming language , Translate the code written in high-level programming language into machine code that the machine can execute .
1.2 The compilation process and the structure of the compiler
1.2.1 Compilation process overview
The typical compilation process is divided into lexical analysis 、 Syntax analysis 、 Semantic analysis 、 Intermediate code generation 、 Code optimization and object code generation 6 Stages .
- Lexical analysis
Read the source program from left to right , Recognize the words one by one , Such as identifier 、 Reserved words 、 operator 、 Boundary symbol, etc .- Syntax analysis
On the basis of lexical analysis, the word sequence is decomposed into various grammatical phrases , Like the procedure 、 sentence 、 Expression etc. . You can generate a syntax tree in the form of a table .- Semantic analysis
Semantic analysis is to check whether the source program has semantic errors , Collect type information for the code generation phase .- Intermediate code generation
Intermediate code is a simple structure 、 A well-defined system of notation , There are two important design principles : Easy to generate and translate into object code . Many translation programs use an approximation “ Three address instructions ” Of “ Quaternion ” In the middle of the code , In the form of ( Operator , Operands 1, Operands 2, result ).- Code optimization
Change and transform the intermediate code , The purpose is to make the generated object code more efficient . Such as the deletion of common subexpressions 、 The intensity is weakened 、 Cycle optimization, etc .- Target code generation
Transform the intermediate code into absolute instructions or relocatable instruction code or assembly instruction code on a specific machine . This stage is related to hardware system structure and instruction meaning .
The division of the above stages is a typical logical division , Not all of them are needed in the actual program .
1.2.2 The structure of the compiler
In addition to the above six components of the compiler , There are also forms management and error handling procedures , These two procedures are similar to the above 6 All stages are related .
The table processing program is equivalent to a small database , Store all kinds of data in the whole compiler processing process of the program .
1.2.3 Combination of compilation stages
The compilation process can be divided into front-end and back-end , The work of the front end mainly depends on the source language and has nothing to do with the target machine . The back end refers to the work that depends on the target machine and generally does not depend on the source language .
It is similar to the separation of front end and back end in software development , So the same front end , Equipped with multiple backend , You can generate multiple machine languages for one language .
1.3 Interpreter and some software tools
1.3.1 Interpretation procedure
The interpreter does not need to translate the source program into object code before running . Its working mode is to acquire one by one 、 Analyze and execute source program statements , There will be results immediately after the implementation , Suitable for interactive working .
python It's interpretive language , and java、C All compiled languages .
The interpretation of the program is very slow , Sometimes the interpretation of a high-level language source program is slower than running an equivalent machine code program 100 times . When running speed is very important , It cannot be explained . in addition , The space cost of interpreting language is also great .
The advantage of interpretation language is that the program written is easy to debug .
1.3.2 Software tools for processing source programs
Structured editor of language
Debugging tools for language programs
Program formatter
Language program testing tools
Program understanding tools
Conversion tools between high-level languages
These tools are found in many integrated development environments .
1.4 PL/0 Language compiler system
PL/0 The language compilation system will PL/0 The source program language is transformed into class P-code Target language . The programming language of this compiler given in this book is C Language .
Yes PL/0 And the class P-code I don't know much about the language . Baidu said PL/0 yes pascal A subset of .
1.4.1 PL/0 Language compilation system composition
PL/0 The language compilation system consists of two parts: compiler and interpreter , Known as PL/0 Compilers and classes P-code Interpretation procedure .PL/0 Language program is PL/0 The compiler converts to an equivalent class P-code Program , When the compiler ends normally ,PL/0 The language compilation system will call the interpreter , Explain and execute the generated target program .
PL/0 The compiler of is not directly programming machine code , Instead, it is compiled into a class P-code Code , This class P-code The code needs to be interpreted by the interpreter before it can be executed .
1.4.2 PL/0 Language
PL/0 What's important about language Pascal A subset of .
In practice , The syntax description of program languages often adopts an extended barks paradigm (EBNF) To describe .
ENBF It describes program syntax , It is also the rule that the language constructor follows .
1.4.3 class P-code Language
class P-code Languages can be regarded as classes P-code Assembly language of virtual machine .
class P-code Virtual machine is a simple machine with pure Trestle Structure , There is a stack memory , Yes 4 Control registers .
class P-code Data storage and arithmetic and logic operations during program operation are carried out at the top of the stack . class P-code The instruction format of the virtual machine is as follows :
F L A
F Represents the opcode of an instruction ;L: The function represents the hierarchical difference between the reference layer and the declaration layer , If it doesn't work, set it to 0; A: Different instructions have different meanings .
class P-code Similar to assembly language . Operate directly on the register .
1.4.4 PL/0 compiler
PL/0 The compiler adopts the compilation process of single pass scanning , By lexical analysis program 、 Parser and code generator 3 An independent process .PL/0 The compiler takes the semantic analysis program as the core , When the grammar analysis needs to read words, it calls the lexical analysis program , When grammatical and semantic analysis correctly generates the target code of the corresponding language component , Just call the code generator .
1.4.5 PL/0 Driver code of language compilation system
The so-called driver code , Is the main function of the program , Connect the various parts of the compiler in series in the main function .
边栏推荐
- Language model
- ZABBIX monitors mongodb (template and deployment operations)
- MySQL view
- Interviewer: is acid fully guaranteed for redis transactions?
- JDBC -- use JDBC connection to operate MySQL database
- GPS數據格式轉換[通俗易懂]
- Deep discussion on the decoding of sent protocol
- Understand kotlin from the perspective of an architect
- Why learn harmonyos and how to get started quickly?
- Leetcode-1. Sum of two numbers (Application of hash table)
猜你喜欢
Redis highly available slice cluster
Constructing expression binary tree with prefix expression
UNIX socket advanced learning diary - advanced i/o functions
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
Pytoch loads the initialization V3 pre training model and reports an error
10 minute fitness method reading notes (3/5)
Master the new features of fluent 2.10
Keras implements verification code identification
Take you hand in hand to develop a service monitoring component
Making and using the cutting tool of TTF font library
随机推荐
Pytoch through datasets Imagefolder loads datasets directly from files
Kotlin函数
View and terminate the executing thread in MySQL
ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
PXE startup configuration and principle
Clear neo4j database data
Keras implements verification code identification
Introduction to GNN
Pytorch two-layer loop to realize the segmentation of large pictures
Just a coincidence? The mysterious technology of apple ios16 is actually the same as that of Chinese enterprises five years ago!
Full text search of MySQL
MySQL regular expression
Ecplise development environment configuration and simple web project construction
Distributed solution - completely solve website cross domain requests
JDBC -- extract JDBC tool classes
MySQL view
C language structure is initialized as a function parameter
Redis highly available slice cluster
JDBC -- use JDBC connection to operate MySQL database
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"