
Yunlin Su Song Y. Yan Principles of Compilers A New Approach to Compilers Including the Algebraic Method Yunlin Su Song Y. Yan Principles of Compilers A New Approach to Compilers Including the Algebraic Method With 129 figures Authors Prof. Yunlin Su Prof. Song Y. Yan Head of Research Center of Information Department of Mathematics Technology Universitas Ma Chung Massachusetts Institute of Technology Villa Puncak Tidar No-01 Malang 77 Massachusetts Avenue Java Timur, Indonesia Cambridge MA 02139, U.S.A. E-mail: [email protected] E-mail: [email protected] ISBN 978-7-04-030577-7 Higher Education Press, Beijing ISBN 978-3-642-20834-8 ISBN 978-3-642-02835-5 (eBook) Springer Heidelberg Dordrecht London New York Library of Congress Control Number: 2011926225 @ Higher Education Press, Beijing and Springer-Verlag Berlin Heidelberg 2011 This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilm or in any other way, and storage in data banks. Duplication of this publication or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, in its current version, and permission for use must always be obtained from Springer. Violations are liable to prosecution under the German Copyright Law. The use of general descriptive names, registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com) Preface The compiler is one of the most important aspects of system software. When any computer user develops a computer program, one must use some pro- gramming language, rather than using a computer instruction set. This im- plies that there must be the compiler of the programming language that has been installed on the computer one uses, and otherwise the developed program cannot be run. There are some differences between a compiler and programming lan- guage. Once language is designed, it must be kept unchanged (except when it contains a mistake that has to be corrected), while the techniques for im- plementing compilation might be changed over time. Hence people always explore the more efficient and more advanced new techniques to raise the quality of compilers. The course similar to “The principles of Compilers” has become one of the most important courses in computer science within higher institutes. Ac- cording to our knowledge, the development of compilation techniques evolves in two directions. One is towards the improvement of the compilation tech- niques for existing languages. Another is towards the research and develop- ment of the compilation techniques of new languages. These new languages include object-oriented languages, distributed languages, parallel languages, etc. This book introduces the newest knowledge in the field, and explores the compilation techniques suitable for the languages and computation. It associates the compilation of programming languages with the translation of natural languages in human brains so that the reader can easier understand the principles of compilers. Meanwhile, it introduces the algebraic method of compilation that belongs to formal technology. This book consists of 16 chapters. Chapter 1, Introduction, outlines the process of compilation and associates the compilation of programming lan- guages with the comprehension and generation of natural languages in human brains. Chapter 2 introduces the grammar and language. The generation of the language is based on the grammar and languages are the fundamentals of the compilation process. Chapter 3 introduces finite automata and regu- lar languages, together with Chapter 4, it is devoted to lexical analysis, the first task of analysis stage. Chapter 3 may be regarded as the theoretical preparation of lexical analysis; while Chapter 4 is the concrete practice of vi Preface lexical analysis. Chapters 5 – 7 commonly work together to discuss syntacti- cal analysis. Chapter 5 introduces push-down automata that correspond to context-free grammars. Chapter 6 devotes to the discussion of context-free grammars and the context-free languages which they generate. Chapter 7 explores the second task of analytical stage — syntactical analysis. Following this is the semantic analysis. After the analytical stage finishes, the synthetic stage starts. The main task of the synthetic stage is to generate object code. Chapter 8 introduces and analyzes attribute grammars. Chapter 9 introduces a new compilation method — the formal method of compilation. Chapter 10 discusses the generation of the intermediate code. Chapter 11 expatiates the debugging and optimization techniques for compilers. Chapter 12 explicates the memory management that is related to compilation of programs. Chap- ter 13 is the destination of the compilation, the generation of object code. The chapter introduces the virtual machine MMIX that is proposed by D.E. Knuth in his book The Art of Computer Programming. This virtual machine is the mixture of features of 14 most popular machines in the current mar- ket, it has rich an instruction set, and makes object codes flexible. Chapters 14 and 15 expound the compilation techniques for object-oriented program- ming languages and parallel programming languages. Chapter 16 discusses issues for grid computing. Though grid computing has attracted one’s atten- tion there is no any language especially suitable for grid computing at the present. Hence, we just focus on its features, pointing out the issues which the compilation of the language should be tackled when the language exists. We would like to express our sincere appreciation to Ms. Chen Hongying of Higher Education Press. Without her encouragement, help and patience, we could not finish the writing of this book. We also want to thank the authors whose contributions were referred to the book. A great part of the contents of the book is taken from them. We would like to acknowledge Tim Lammertink and Myrte de Vos for their kind help. Finally, we would like to express our gratitude to our family and students for their long-term support and understanding. No doubt, there might be neglects or mistakes remaining in the book. We hope that the reader would be generous with your criticism. Yunlin Su Song Y. Yan March 2011 Contents Chapter 1 Introduction ······························· 1 1.1 Language and Mankind ···························· 1 1.2 Language and Computer ··························· 3 1.3 Compilation of Programming Languages ················ 12 1.4 Number of Passes of Compiler ······················· 17 1.5 An Example of Compilation of a Statement ·············· 19 1.6 Organization of the Book ··························· 21 Problems··········································· 23 References·········································· 23 Chapter 2 Grammars and Languages ···················· 25 2.1 Motivation of the Chapter ·························· 25 2.2 Preliminary Knowledge····························· 25 2.3 Grammar······································· 27 2.4 Language······································· 31 2.5 Language Generated by a Grammar ··················· 34 2.6 Turing Machine ·································· 37 2.7 Issues Concerning Grammars and Languages ············· 52 Problems··········································· 53 References·········································· 54 Chapter 3 Finite State Automata and Regular Languages ································· 55 3.1 Motivations of the Chapter·························· 55 3.2 Languages, Grammars and Automata ·················· 55 3.3 Deterministic Finite Automata ······················· 59 3.4 Nondeterministic Finite Automata ···················· 64 3.5 Regular Expressions ······························· 65 3.6 Regular Grammar ································ 66 3.7 Kleene’s and Moore’s Theorems ······················ 68 3.8 Pumping Theorems and Closure Properties for LREG ······· 69 viii Contents 3.9 Applications of Finite Automata······················ 70 3.10 Variants of Finite Automata ························ 72 Problems··········································· 77 References·········································· 78 Chapter 4 Lexical Analysis ···························· 79 4.1 Motivation of the Chapter ·························· 79 4.2 Lexical Analyzer ································· 80 4.2.1 Role of Lexical Analyzer······················· 81 4.2.2 Identifier Analysis ··························· 84 4.2.3 Handling of Constants ························ 86 4.2.4 Structure of Lexical Analyzer ··················· 87 4.3 Output of Lexical Analyzer·························· 95 4.4 Error Handling ·································· 97 Problems··········································· 98 References·········································· 98 Chapter 5 Push-Down Automata and Context-Free Languages ································· 101 5.1 Motivation of the Chapter ·························· 101 5.2 Push-Down Automata ····························· 102 5.3 Context-Free Languages (LCF) ······················· 103 5.4 Pumping Theorems for Context-Free Languages··········· 105 5.5 Push-Down Automata and Context-Free Languages ········ 106 5.6 Applications
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages458 Page
-
File Size-