Write the compiler, linker, and compiler by yourself

Source: Internet
Author: User

Write the compiler, linker, and compiler by yourself

A complete set of Compiler Principles and Techniques play an important role in the entire computer science field. Learning it can be of great help to programmers. After studying the history, we will find that the program design masters praised by everyone are masters in the compilation field, such as the bill gates who write the BASIC language and the father of sun java. They have deep accomplishments in compilation. Bill Gates, who once sat down on the throne of the richest man in the world for many years, started from compiling the Basic language compiler for the microcomputer. It is precisely this BASIC compiler that laid the foundation for Bill Gates and Paul Allen's Microsoft empire. It was the experience of writing the Basic language compiler that started Gates's brilliant career.

A compiler is a very complex program. It is not easy to write or even understand such a program. Most computer scientists and professionals have never compiled a complete compiler. However, almost all forms of computation require the compiler, and any professional dealing with computers should master the basic structure and operations of the compiler. In addition, a common task in computer applications is the development of command interpreter programs and interface programs, which is smaller than the compiler, but uses the same technology. Therefore, mastering this technology is of great practical significance.

Academician Li guojie, director of the Institute of computing, Chinese Emy of Sciences, said: "With the rapid development of microprocessor technology, processor performance depends largely on the quality of compilers. compilation technology has become the core technology of computers, status becomes more and more important. To develop our own microprocessor business, we must have our own compilation technology as the backend ."

Let's look back at the reasons for writing this article. I feel that I have no big difference between learning other computer courses and reading martial arts novels, and I don't feel very difficult to understand. I just read the compilation principles of teaching materials, and I feel a little bit confused after reading the cloudification and fog, I felt that the teaching materials I learned were too theoretical. So I went to the bookstore and bought all the books related to the compilation principles, of course, this also includes three classic books, dragon, tiger, and whale, which are recognized as the compilation principles. I read each book from start to end, and I understand everything in my mind, I also feel that it is still possible for the master to write a compiler by myself. In addition, almost all books about compilation principles have the following sentence: "The existing compilers are constructed using Lex and Yacc. It is almost impossible to manually compile a complete compiler from the beginning ." But the author is the person who knows that there are tigers in the mountains and is biased towards Hushan. He needs to know that the early compilation can be constructed by hand. This is the beginning of the bitter and sour journey, but what language does the compiler write? How is this compiler positioned? All of these are at a loss.

I started to study the examples in the compilation principles book, hoping to find inspiration and answer the above questions. World-renowned computer scientist N. the Compilation Program in PL/0 language compiled by Worth is the first compiler studied by the author. It has simple functions, clear structure, and high readability, it is considered to be a very suitable learning model for small compilation programs. However, the author does not feel comfortable with this compilation program because it does not support arrays, structs, and strings, it is written using a hypothetical stack-type machine as an example, instead of directly generating a target language program that can be directly run in a certain CPU or operating system environment. As a teaching model for compiling principles, "PL/0 language compilation program" can only be regarded as a "general in the compiler". Because there is no better, it will have to be used. So far, the compiler's problem locating algorithm has some eyebrows, and the author hopes to construct a more suitable compiler for teaching.

However, another question comes one after another. Why can't many open-source compilers be used directly as the teaching model of compilation principles? I started to study the source code of various open-source compilers, including the source code of GCC. Since GCC supports multiple front-end languages and various backend machine platforms, AST (abstract Syntax Tree ), the RTL (Register Transfer Language) has become an insurmountable hurdle. We have not yet learned how to compile a compiler for a source Language or a target machine, it is necessary to learn the compiler that supports multiple machine platforms in multiple source languages. Just like a baby has to learn to run before learning to walk, it is doomed to fall below.

On the one hand, the teaching model of compiler is too simplified, and on the other hand, the open-source compiler is too complicated, which is not suitable for teaching models. Here, the compiler positioning problem is completely clear. The author wants to construct a teaching model that teaches you how to write the compiler by yourself. This model consists of two parts: the first part is the language definition, and the second part is the implementation of the language compiler. This compiler only supports one source language and only one target language. This language should have the most important features of popular advanced languages. This compiler should be clear in structure, with as few code as possible. It should be able to reflect the complete process and technology of compiling a practical compiler. This compiler can generate an exe file that can be run in the operating system. You only need to double-click the file or execute it in the command line to see the result.

Next, the author began to think about another question: Which language is the compiler written? The authors have studied the most popular programming languages C, C ++, C #, Objective-C, and Java. The C language is the simplest, with only 32 keywords, however, the authors found that the C language still has many redundant components, which can be simpler as a learning model. Finally, based on the C language, the author appropriately simplifies and defines a new language with only 15 keywords, called SC language. Select the familiar Intel X86 machine language as the target language. Our compiler is named SCC compiler.

In this series of articles, you will see the complete process from SC language definition to SCC compiler development. After reading this article, you will know how to define a brand new language and how to compile a real compiler, which will not be mysterious to you, the theory of compilation principles and the SC language definition described in this article and the SCC compiler development process are the best interpretations of the theory and practice in the compilation field.


What is the role of the compiler and the linker?

Compiler: Compile the source code into the target file.
Linker: link the target file and library file to an executable file.

I want to compile a C language compiler for Chinese programmers.

The C language is better and the efficiency is relatively high.
You can also use JAVA for debugging.
I have used the above two compilers. Other languages are hard to say.
It is not difficult to make a compiler. It takes too much time for a person to do it. But it takes time to do the relevant things. For example, editing the environment, such as debugging the tracker.

Success!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.