Preface 8
Chapter 1th Introduction 10
1.1Hello World Compilation Process Analysis 10
1.1.1Hello World Program source file 10
1.1.2 Lexical Analysis 11
1.1.3 Syntax Analysis 12
1.1.4 Semantic Analysis 13
1.1.5 Linker 14
Introduction to the 1.2SCC compiler 16
1.2.1SCC Compiler Architecture 16
1.2.2SCC Compiler development Environment 17
1.2.3SCC Compiler Runtime Environment 18
2nd. Grammatical Knowledge 20
2.1 Language Overview 20
2.2 Formal Language 21
2.2.1 Alphabet and Symbol strings 21
2.2.2 Grammar and the formal definition of language 22
2.2.3 Grammar and the type of language 23
2.2.4 Programming Language Description Tool 25
2.3 Lexical Analysis Methods 26
2.3.1 Lexical Definitions Example 27
2.3.2 Status Conversion Diagram 27
2.3.3 Lexical analysis program Flowchart 28
2.4 Syntax Analysis Method 28
2.4.1LL Analyzer 28
2.4.2LL (k) Grammar 29
2.4.3LL (1) Grammar 29
2.4.4 Recursive subroutine Method 31
Equivalent transformation of 2.4.5 Grammar 34
3rd SC language Definition 36
Blueprint selection for 3.1SC language 36
3.1.1Traditional C36
3.1.2c8936
3.1.3c9937
3.2SC language-to-C89 simplification principle 37
Character set for 3.3SC languages 37
3.3.1 Basic Character Set 38
3.3.2 Extended Character Set 39
3.4SC language Lexical definition 39
3.4.1 Keyword 39
3.4.2 Identifier 40
3.2.3 Integer Constant 41
3.2.4 Character Constant 41
3.4.3 String Constant 42
3.4.4 Operators and Separators 42
3.4.5 Notes 43
3.5SC language Syntax Definitions 43
3.5.1 External Definition 43
3.5.2 Statement 46
3.5.3 Expression 50
3.6SC language vs. C language Features 57
3.6.1 Keyword 57
3.6.2 Data Type 58
3.6.3 Storage Type 59
3.6.4 Constant 59
3.6.5 Variable 59
3.6.6 function 60
3.6.7 Statement 60
3.6.8 Expression 63
4th Chapter SC Language lexical Analysis 65
4.1 Official statement of the lexical analysis task 65
4.2 Word Code 66
4.3 Data structures used in lexical analysis 68
4.3.1 Dynamic String 69
4.3.2 Dynamic Array 71
4.3.3 Hash Table 74
4.3.4 Word Table 75
4.4 Error handling, proactive 80
4.5 Lexical Analysis Process 85
4.5.1 Lexical Analysis Main program 85
4.5.2 pretreatment 89
4.5.3 Parsing identifier 92
4.5.4 parsing integers 93
4.5.5 Parsing string 93
4.5.6 Lexical Analysis Flowchart 96
4.6 Lexical Coloring 98
4.7 Control Procedures 99
4.8 Lexical analysis results show 100
5th. SCC Syntax Analysis 101
5.1 External Definitions 102
5.1.1 Translation Unit 102
5.1.2 External Declarations 103
5.1.3 Type delimiter 105
5.1.4 Structure Delimiter 107
5.1.5 function calling convention 110
5.1.6 Structure Member Alignment 110
5.1.7 declarators 111
5.1.8 Initial character 115
5.2 Statement 115
5.2.1 Compound Statement 117
5.2.2 Expression Statement 118
5.2.3 SELECT statement 119
5.2.4 Loop Statement 119
5.2.5 Jump Statement 120
5.3 Expression 122
5.3.1 Assignment Expression 123
5.3.2 equality-Class expression 124
5.3.3 Relationship Expression 124
5.3.4 Plus and minus class expression 125
5.3.5 multiplication-Class expression 126
5.3.61-Tuple Expression 126
5.3.7 suffix Expression 128
5.3.8 Initial-Value expression 129
5.4 Syntax Indentation 131
Global variables and enumerations used by 5.4.1 131
5.4.2 Grammar Indentation Program 131
5.5 Control Procedures 133
5.6 Results show 133
Chapter 6th Symbol Table 134
6.1 Introduction to the symbol table 137
6.1.1 Collecting Symbol Attributes 137
The legality check of 6.1.2 semantics 138
6.2 Key data structures used in the symbol table 139
6.2.1 Stack Structure 139
6.2.2 Symbol Table Structure 143
6.2.3 Data Type Structure 148
6.2.4 Storage Type 149
6.3 Construction process of the symbol table 150
6.3.1 External Declarations 150
6.3.2 Type delimiter 153
6.3.3 Structure delimiter 154
6.3.4 declarators 161
6.3.5 Initialization of variables 167
6.3.6 Compound Statement 167
6.3.7sizeof Expression 168
6.3.8-Elementary Expression 169
6.4 Control Procedures 171
6.5 Results show 173
7th. Generate COFF target file 175
7.1COFF File Structure 175
7.1.1 Basic Concepts 175
7.1.2 Overall structure 176
7.1.3COFF File Header 177
7.1.4 Section Head Table 180
7.1.5 Code Section Content 187
7.1.6 Data section and Import section content 187
7.1.7COFF Symbol Table 188
7.1.8COFF String Table 192
7.1.9COFF Relocation Information 193
7.2 Generating the COFF target file 195
7.2.1 Generating Section Table 196
7.2.2 Generating Symbol Table 198
7.2.3 Generating Relocation Information 201
7.2.4 Generating a target file 202
7.3 Results show 205
8th Chapter x86 Machine Language 207
8.1x86 Machine Language Introduction 207
8.2 General Instruction Format 207
8.2.1 Instruction Prefix 208
8.2.2 Operation Code 210
8.2.3modr/m Byte 210
8.2.4 offset vs. immediate number 213
8.3x86 Register 213
8.3.1 Data Register 213
8.3.2 Variable Address register 213
8.3.3 Pointer Register 214
8.3.4 Segment Register 214
8.3.5 instruction Pointer Register 215
8.3.6 Flag Register 215
8.4 Instruction Reference 216
8.4.1 Symbol Description 216
8.4.2 Data Transfer Instructions 218
8.4.3 arithmetic Operations Directive 221
8.4.4 Logic Operations Directive 225
8.4.5 Control Transfer Command 228
8.4.6 string Operation instruction 232
8.4.7 Processor Control Instructions 232
8.5 Generating X86 Machine language 233
8.5.1 Operand Stack 233
8.5.2 Generating Generic Directives 235
8.5.3 Generating Data transfer Instructions 238
8.5.4 generating arithmetic and logic operations Directive 242
8.5.5 Generating Control transfer Directives 246
8.5.6 Register Use 250
8.6 Results show 252
9th SCC Semantic Analysis 254
9.1 External Definitions 254
9.1.1 declaration and function Definition 254
9.1.2 Initial character 257
9.1.3 Function Body 259
9.2 Statement 262
9.2.1 Expression Statement 262
9.2.2 SELECT statement 263
9.2.3 Loop Statement 264
9.2.4 Jump Statement 266
9.3 Expression 269
9.3.1 Assignment Expression 270
9.3.2 equality-Class expression 271
9.3.3 Relationship Expression 272
9.3.4 Plus and minus class expression 273
9.3.5 multiplication-Class expression 274
9.3.61-Tuple Expression 276
9.3.7 suffix Expression 278
9.3.8 Initial-Value expression 282
9.4 Results show 284
10th Chapter Linker 286
10.1 Linking methods with library files 286
10.2PE file Format 288
10.2.1 Overall Structure 288
10.2.2DOS Part 289
10.2.3NT Head 291
10.2.4 Section Head Table 298
10.2.5 Code Section 298
10.2.6. Data section 300
10.2.7 Import Section 301
10.3 Linker Code Implementation 305
10.3.1 Generating PE file Header 305
10.3.2 loading the target file 308
10.3.3 Loading Introduction Library file 310
10.3.4 Parsing external Symbols 311
10.3.5 to calculate the RVA address for the section area 314
10.3.6 Reposition Symbol Address 315
10.3.7 fix addresses that need relocation 316
10.3.8 Writing PE files 317
10.3.9 Generating EXE files 319
10.4SCC Compiler Master Program 320
10.5 Results show 324
11th. SC Language Program Development 326
11.1 Program development process with SCC 326
11.2SCC Compiler test Program 326
11.2.1 Expression Test 326
11.2.2 Statement Test 330
11.2.3 Structural Test 332
11.2.4 function parameter passing test 335
11.2.5 String Test 337
11.2.6 Global Variables Test 338
11.3SC Language Application Example 339
11.3.1 Console program that can receive command parameters 339
11.3.2 Win32 program that can receive command parameters 340
11.3.3HelloWindows Window Program 341
11.3.4 File Replicator 346
11.3.599 Multiplication Table 349
11.3.6 Print Diamond 350
11.3.7 Screen Capture Program 353
12th Appendix I the definition of SCC grammar in bilingual form 361
The 13th chapter references 366
Write your own compiler, linker directory structure