Compared with other natural sciences,computer science is a relatively new subject system,and there are still many technical fields to be further explored.Compilation technology is a design discipline of system software.As the cornerstone of computer technology development,the purpose of learning compiler design is not only the theory and technology,but also the ideas and methods which are more worth learning for scholars.Based on the object-oriented thinking of C++ programming language,this paper systematically implements a C compiler which we can call MiniC.Combining with the source code of MiniC,the design and implementation of data structure and algorithm framework of each module of the compiler are introduced in detail from the front-end and back-end perspectives.The main research work of the thesis is mainly reflected in the following aspects:The front-end part includes lexical analysis,grammar analysis and semantic analysis.The lexical analysis part uses the finite state automaton for word recognition,and then uses the hash table structure to implement word insertion and search.The syntax-directed translation is adopted to directly drive semantic analysis in the process of grammar analysis,and the stack structure is used to organize the symbol table to realize symbol storage and scope management.The back-end part implementation includes assembly and linking.In the assembly process,the source language is translated into machine instructions by direct translation,and the register allocation algorithm,the storage management at the run time of the program and the generation of object file are emphasized.In link process,symbols are analyzed and relocated,and then the references are combined with definitions.Finally,executable files in PE file format under Windows system are generated.Error management runs through the compilation system,counting errors in compilation and linking process,and printing them to users.Finally,the test results show that the C compiler implemented in this paper can compile and assemble the source program to generate the object file,and the generated executable file can successfully run on the target machine and get correct results through linking. |