Detours: suggestions for learning compilation principles

Source: Internet
Author: User

Compilation principles are generally considered as a difficult course. from the comments on the Internet, some people say that after learning software theory for a year and a half, one compilation cannot be understood; some people even say that it is the most difficult one in the big software course; some people complain that none of the domestic textbooks is easy to understand.

From the perspective of my study and practice, I learned for more than a month for the first time. I learned about the theoretical part and the second time for a week. I basically understood the theoretical part of lexical analysis, and I learned about the syntax analysis, it took the third week to understand lexical analysis and syntax analysis. from this point of view, this course is indeed difficult. some posts on the Internet make the compilation of compilers uncertain and difficult.

Where is the compilation principle difficult? The author's experience mainly covers the following points:

1. incorrect understanding: Many people think that the compilation principle can only be used for writingProgramIn terms of the language compiler, I think it is of little use and I am not very interested in learning. In addition, I may think that writing the compiler must be completely written manually.

2. Theory of automation: Like NFA and DFA, It is abstract and requires some brains. Especially if the theory of automation is not used in discrete mathematics, it takes more time.

3. Deduction of Set Theory: It is mainly about closure operations and so on. If the mathematical foundation is poor, it will be difficult to learn it.

4. lr Syntax: mainly introduces the automatic mechanism.

No matter which textbook is compiled, even the Absolute classic "longshu" is no exception, it involves these difficulties. because the content itself is hard to understand, the author has a great skill, and it is difficult to make the writing as smooth and understandable as a novel.

After clarifying the difficulties, I want to solve the following problems:

1. correct understanding: the compilation principle is widely used in static text processing. For example, it is very simple to convert HTML files into plain text. understanding the practicality of compilation principles may increase your learning interest.

2. Read Books repeatedly: This method seems the most stupid, but it is the basic method. I forgot which celebrity said that as long as I read more books, I can always understand them.

3. Combined with the source code: This is the recommended method by Allen hollub, author of compiler design in C, a classic textbook. This textbook features a large segment of YACC and LexCode. This is also a good method. Only by understanding the code can we fundamentally understand the theory. of course, to fully understand the YACC code, the workload is huge, and we also need to understand the theory first.

4. simplified and simplified to avoid duplication. an Introduction to compilation principles (Tang Liang, Sichuan University) is widely circulated on the Internet. For lexical analysis, the author avoids introduction to the theory of automatic machines and deduction of set theory, directly moved out of the source code, greatly reducing the difficulty of understanding. For syntax analysis, the author introduced recursive descent and ll grammar and the corresponding source code, while for LR grammar, just say "understand the theory ". although this method avoids learning difficulties, you can use this method to compile a running lexical analyzer and syntax analyzer in a short period of time, which can greatly improve your learning enthusiasm.

The idea of the author is basically similar to 4th methods, but it is also slightly different. Due to an accidental reason, the author needs to compile a lexical analyzer and a syntax analyzer for the programSource codeStatic analysis. at first, I had no idea how to compile it. I felt very difficult. later, I found a similar open-source program, which was developed using a compiler named pccts to generate a tool automatically. open source is good! I found a tool called anlr (it is a new version of pccts that supports generating Java, C ++, and C # code), and downloadedC LanguageSyntax file (because the author needs to process the C code file), and then compiled a small number of action statements, interface code, analysis and processing code, etc, I wrote a program in just one week when I knew little about the compilation principle (I forgot to understand the regular expression.

This practice has greatly increased the author's interest in compilation principles, re-learned the compilation principles, and summarized the practical and effective learning steps of compilation principles:

1. first, use a compiler generation tool such as anlr to create a small program (for example, the above mentioned HTML file is converted into a plain text file program ), the required knowledge is only the basic knowledge of regular expressions and the use of the generation tool itself (you can refer to the online help and onlineTutorial(Tutorial) to master). The advantage of doing so is:

1) I can understand the practicality of compilation principles and increase my interest in learning.

2) Easy to get started, eliminating the fear of compilation principles.

3) obtain the perceptual knowledge of lexical analyzer and syntax analyzer, which is conducive to a deeper understanding of the theory.

4) obtain the use experience of the compiler Automatic Generation Tool (compiler) to improve the ability to solve practical problems. (most of the actual work is not hand-compiled but used by tools)

2. tools such as anlr are open source. You can study the source code to compile your own analysis programs if necessary.

3. Let's look back at the compilation principles and textbooks. At this time, we may find that many theories are easy to understand. The only thing left is the difficulties mentioned above.

4. Further deepen the understanding of the teaching materials based on the source code attached to the teaching materials

Here, by the way, some compilation principles of teaching materials, for the input subsystem not only set up chapter, some even completely ignored, the author believes that, the input subsystem is much simpler than the lexical analyzer and syntax analyzer, but it is also the basis of the two, so it is necessary to look at the source code to understand. in this regard, the implementation mechanism of anlr is different from that of lex (of course, it is related to the difference between Java and C.

When studying VC ++, I deeply understand the importance of teaching materials. I began to take advantage of the evil effects of the inferior optical disc version "teaching material" and "21-day learning VC ++, later, I came to the "VC ++ technology insider", and I was very enlightened. however, it seems that the quality difference between the textbooks of compilation principles is not very large, but the key lies in the appropriate method. I have summarized the above methods by mistake, hoping to provide some reference value.

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.