Why do you want to set up the principle of compiling? This course is concerned with the compiler aspects of the production of principles and technical issues, it seems that the basic area of the computer is not close, but the compiler principle has been as a compulsory course for undergraduate courses, but also become a postgraduate entrance examination of the necessary content. The compiler principle and the technology are essentially an algorithm problem, of course, because this problem is very complex, its solution algorithm is also relatively complex. We study the data structure and algorithm analysis is also the algorithm, but the basic algorithm, in other words, the introduction of the algorithm, and the compiler principle of this course is more focused on solving an algorithm. In the the 1950s, compiler writing has been considered to be very difficult things, the first Fortran compiler is said to have spent 18 years to complete. As people try to write compilers, there are many theories and techniques associated with compiling, and these theories and techniques are more valuable than the actual compiler itself. Just as mathematicians solve the famous Goldbach conjecture, although there is no final solution to the problem, but in the meantime the birth of many famous works of the relevant number theory.
Recommended reference Books
Although the compilation theory has developed to today, already has the relatively mature part, but as a university student, must write oneself a like Turboc c,java the compiler to say is still too difficult. Not only is it difficult to write a compiler, it is also difficult to learn the principles of compiling.
It is because the principle of compiling is relatively difficult to learn, then the need for good teachers and good textbooks. We can not change the teachers themselves, but in the teaching materials we can read according to their own wishes. I recommend several good compiling principles of the textbook below. I recommend books are the classic foreign textbooks, because in the domestic textbooks, it is true that there is no one to find satisfactory.
The first book's original name is "compilers Principles,techniques,and Tools", and another loud one is the Dragon book. The reason is that the cover of this book has a red dragon, also because Jue mortar Dingyi psssssssssssst qu updated x Xuan cover fat embankment xin ? So many foreign scholars are directly named Dragon Book. Recently, the mechanical industry Press has published the Chinese version of the book, the name is called "the principle of compiling." The book was written earlier, probably in 85 or 86 years, and one of the authors was the famous Bell Labs scientist. The core of the principles of compilation has not changed, so until today, its value is extraordinary. The most characteristic of this book is to start with a practical small example, to compile the principle of the outline of the content, so that many of the compiler principle of beginners soon have a bottom, but also know why there are these theories, how to use these theories. And this is what I feel the lack of domestic textbooks, so the domestic textbooks are not written to the readers who are willing to self-study, in short, let people look at a half-day, but do not know what the inside of the use of things.
The second book is called "Modern Compiler Design", the Chinese name is "Modern compiling programming". The book was published by the People's post and telecommunications press. This book is more concerned about the practice of compiling principles, the book gives a lot of the actual program code, there are many practical problems such as compiling technical issues and so on. Another feature of this book is its "modern" and the word. In the traditional textbook of compiling principles, it is impossible for you to see algorithms such as "garbage collection" in Java. Because Java is such an interpretation of the implementation of language is in recent years only popular things. If you want to learn more about the theory of compiling principles, then you must look at the previous Dragon book, if you want to do an advanced compiler, then you have to see this "Modern compiling program design."
The third book is a lot of domestic compiler principle scholars are recommended that the "compiler principle and practice." Perhaps this book was introduced into the country earlier, I remember that I bought the book in high School, but also in the last few days before the whole book read. This book is indeed a good choice as an introductory tutorial. The book gives the principle of compiling the explanation is also quite meticulous, although not as deep as the Dragon book in front, but a lot of places are no point, as a university undergraduate teaching is very deep. The book is characterized by a focus on practice, but the feeling is not as good as the previous "Modern Compiling program design" practice tastes heavier. The focus of this book is on the principles of practice, not on the technical practices of the previous one. "Compiling principle and practice" in explaining the various parts of compiling principle, also is gradually practicing a modern compiler tiny C. When you finish reading the whole book, you can write a tiny c. The author also explains in detail the two commonly used compilation-related tools of Lex and YACC, which is also difficult to see in the domestic textbooks.
These three textbooks have been recommended in both English and Chinese versions. A lot of good English students only like to see the original book, not my feeling is that the translation of these three books are very good, there is no need to buy special English version. It is more important to understand the essence of theory than to understand the surface of words.
The essence of compiling principle
As has been said before, learning to compile the principle is actually learning algorithm, nothing special. But the generation of these algorithms has formed a set of theories. Let me take a look at the compiler principle of what the profound theory of it.
Almost every textbook of compiling principle is divided into lexical analysis, syntax analysis (ll algorithm, recursive descent algorithm, LR algorithm), semantic analysis, runtime Environment, intermediate code, code generation, code optimization, these parts. In fact, many of the principles of compiling textbooks are in accordance with the 85,86 published by the Dragon book to arrange the teaching content, so the content format of the Dragon book has become almost the current compiler principles of teaching materials, including domestic textbooks are also so. Generally speaking, undergraduate teaching in university is impossible to finish all the above, but more emphasis on the previous parts. Like code optimization that part of things, like a bottomless pit, if you want to seriously, that is a separate semester of the class can not speak clearly. Therefore, generally for undergraduates, the analysis of the word Law and grammar analysis of the requirements on the relatively high point.
Lexical analysis is comparatively simple. May be the lexical analysis of the program itself to achieve a very simple bar, many people who have not learned the principles of compiling can also write a variety of lexical analysis procedures. However, the compiler principle in the interpretation of lexical analysis, the focus of the regular expression and automata principle added, and then in a very standard way to explain the formation of lexical analysis procedures. This practice is obvious, is to let lexical analysis from the process to the point of theory.
The grammar analysis section is a bit more troublesome. Now there are generally two kinds of syntax analysis algorithms, LL Top-down algorithm and LR bottom-up algorithm. ll algorithm Fortunately, to the LR algorithm, when the difficulties came. Many self-study compiler principle is to encounter the LR algorithm to understand the problem after the abandonment of self-study. In fact, these things are as long as we understand it, and it is not like lexical analysis to have to write out the real will. Like LR algorithm of the parser, is generally used to generate the tool YACC, in practice there is no comparison of their own to achieve. For the special recursive descent algorithm in ll algorithm, because its practice is very simple, then should be required for each student to write their own. Of course, there are also a lot of good ll algorithm syntax Analyzer, but if the switch in the non-C platform, such as Java,delphi, you can not use the YACC tool, then you have to write the parser yourself.
When you learn lexical analysis and grammar analysis, you may have the question: "Lexical analysis and grammar analysis in the end." "From the compiler's point of view, the compiler needs to convert the programmer's source program into a convenient data structure (abstract syntax tree or syntax tree), then the transformation process is through lexical analysis and syntax analysis." In fact, lexical analysis is not at the beginning of the compiler is included in the necessary parts, but we in order to simplify the process of parsing, lexical analysis of this tedious work alone extracted, became now the lexical analysis part. In addition to the compiler section, lexical analysis and parsing are useful elsewhere. For example, when we enter the command under the Dos,unix,linux, how the program analyzes the command form you entered, which is also a simple application. In a word, the work of these two parts is to transform the text information of the "rule" into a kind of data structure which is better analyzed and processed. So why does the tutorial of compiling principle finally convert the source analysis to analyze to "tree" this kind of data structure? The data structure has stack, line,list ... So many data structures, each have their own characteristics. But the tree has a very strong recursive structure, which means that we can extract any node node of the tree, and it is still a complete one. This is in line with our current form of compiling principles analysis, such as we use the function inside the tree, loop in the loop, conditions used in the condition, etc., then can be very intuitive representation in the tree of this data structure. Similarly, we are so recursive in executing the program of a formal language. In the code generated after the compilation principle, we will introduce a kind of stack of intermediate code, we can based on the analysis of the abstract syntax tree, it is easy, very mechanically using recursive traversal of the abstract syntax tree can generate this instruction code. This code is also widely used in other interpretive languages. Like the current popular java,.net, its underlying bytecode bytecode, which can be said to be based on the stack of instruction code.
As for semantic analysis, grammar-guided translation, type checking and so on, it is actually a process of perfecting the abstract syntax tree. For example, when we write the C language program, we all know that if a floating-point number directly assigned to an integer, there will be type mismatch, then the C language compiler how to know it. Is through this step type check. As with the C + + language, which supports polymorphic functions, this part of the problem is more complex. In this part, the textbook of large compiling principle is to explain some better strategies. Because the new problem is always happening, the old method is not enough to solve.
Originally said, as a compiler, the part that works is the user input source program to the final code generation. But in explaining the final code generation, but also have to explain the machine operating environment and so on. Because if you don't know how the machine executes the final code, then of course you don't know how to generate the right final code. This part of the content I feel its meaning even more than the compiler principle itself. Because it will be the process of running a computer program are all in front of you, you may not be engaged in the development of the compiler, but as long as the computer software development related to the field, will involve the implementation process of the program. An explanation of the runtime environment will give you a clearer idea of how a computer program is stored, loaded, and executed. On the part of the content, I strongly recommend that you look at the Dragon book on the interpretation, the author from the most basic storage organization, storage allocation strategy, nonlocal name access, parameter transfer, symbol table to dynamic storage allocation (malloc,new) are described in detail. These are the things that we often do when we write normal programs, but we are less about how the interior is done.
About the middle code generation, code generation, code optimization part of the content is really difficult to say. Many domestic textbooks to this part will be very simple to talk about the past, students listen to also just as understanding, do not know how to use. But this part of the content if you want to seriously, a separate semester of the course can not finish. In the book "Principles and practices of compiling", the explanation for this part is just right. The author mainly explains or a stack based on the instruction code, very easy to understand, let a person read, it is easier to imitate, after their own down can write their own code generation. Of course, for other code generation technology, the Explanation of code optimization technology is very simple. If you want to carefully study the code generation technology, in fact, there is also called "Advance Compiler Desgin and implement", which is now introduced by the machinery industry press, very heavy, but also the original English. But this book I did not list it as a recommendation to everyone, after all, can make the contents of the Dragon book Clear, in China has been even a good master, to that time to see the "Advance Compiler Desgin and implement" is not too late. Code optimization part of undergraduate teaching is still a less important part of the practice process, I believe that we do not use it. After all, it's good to have your own compiler build the code that executes correctly, and talk about optimization.
About Practice
After all, the course of compiling principles is just a course of explaining principles, not a specialized course in compiling technology. There is a big difference between the two courses. The compiler technology is more concerned with the actual writing of the technology used in the compiler process, while the principle of the class focus on explaining its basic theory. But computer science itself is a highly practical course, and if it can be learned, it's called a real society. When you talk about crazy English, Li Yang says that when you actually apply a word to a phrase, you can learn the word or phrase instead of just knowing its spelling and meaning. In fact, any learning is the same, if the lack of a combination of practice, you can not calculate the institute.
The course of compiling principle is mainly to explain the theory and principle that compiler produces, so very simple, write a compiler oneself is the best practice process. But you have to be careful. Compiling a system may be one of the most complex systems in any software system, or why the compiler will be written into a course called compiling principles in the university. I admire those who learned the principle of operating system began to write their own operating system, learned the principle of compiling the compiler began to write their own people, indeed, in China, dare to do so few students. And whether you can do it or not, at least with this attempt, will make your program design, system planning, the foundation of the skills to improve a lot. I'm going to give you some of the difficulties you might encounter in the process, hoping to help you before you get into trouble.
1. Lex and YACC. These two tools are a very grammatical tool for lexical analysis. If you write a compiler yourself, I do not recommend that you analyze this kind of thing by hand. Lex and YACC should be the necessary content for each textbook of compiling principles, but they are rarely seen in the domestic textbooks. These two tools are small things under the UNIX system, and if you want to use it in Windows, you'd better go down and cygwin the software. It is a simulation of UNIX in Windows, which contains the Flex.exe and Bison.exe (YACC) two tools. These two tools are cumbersome to use (in fact, many of the most useful tools under Unix), but in the principles and practices of compiling The two tools are explained in detail in this book, as well as a number of practical examples.
2. It is simpler to do an interpreted language than to do a compiler that generates machine code. Although, as an explanatory compiler, like Java, you have to write an interpreter yourself, but you don't have to look up the machine code. If you do the generated final machine code compiler may encounter problems and a register based code generation method. As I said earlier, if you generate a stack based code, then the code generation process is very simple, there are not many things to consider, if you consider the final machine code generation, you have to consider the machine register how to allocate the problem.
3. Consider using other people have generated grammar files, try not to write their own lexical files and grammar files. A friend once said that writing a syntax definition for a good program language almost completes half of a compiler. It's true, Writing a grammar file is a difficult thing to do. Now the internet can be found everywhere, such as C language, C++,java, Tiny C,minus C and other language lexical files and grammar files, you can completely down to use.
In the book compiling principle and practice, the author gives the whole code of a tiny C. I feel the author of this compiler does very well, compared to other languages such as Php,perl source code, easy to read, And it clearly shows the implementation process of a completed compilation system. The source code can be downloaded from the author's website.
Something
Compiling the principles of learning can be a difficult process, especially for those who are not interested in compiling the system. Now that it has been a compulsory course for undergraduate courses, the theory that it has extended has a relatively important position in the field of computer science.
If we look at the history, we will find that many people called the program Design Master is a master in the field of compilation. Write the first microcomputer running the basic language of Bill Gates, designed the Delphi Borland "The world's most powerful programmer", Sun Java father, Bell Lab's C + + Father ...
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.