Compiler Design: Theory, Tools, and Examples
Total Page:16
File Type:pdf, Size:1020Kb
Rowan University Rowan Digital Works Open Educational Resources University Libraries 5-1-2017 Compiler Design: Theory, Tools, and Examples Seth D. Bergmann Rowan University Follow this and additional works at: https://rdw.rowan.edu/oer Part of the Computer Sciences Commons DOI: 10.31986/issn.2689-0690_rdw.oer.1001 Let us know how access to this document benefits ouy - share your thoughts on our feedback form. Recommended Citation Bergmann, Seth D., "Compiler Design: Theory, Tools, and Examples" (2017). Open Educational Resources. 1. https://rdw.rowan.edu/oer/1 This Book is brought to you for free and open access by the University Libraries at Rowan Digital Works. It has been accepted for inclusion in Open Educational Resources by an authorized administrator of Rowan Digital Works. Compiler Design: Theory, Tools, and Examples Seth D. Bergmann February 12, 2016 2 Contents Preface v 1 Introduction 1 1.1 WhatisaCompiler?......................... 1 1.1.1 Exercises ........................... 7 1.2 ThePhasesofaCompiler ...................... 8 1.2.1 Lexical Analysis (Scanner) - Finding the Word Boundaries 8 1.2.2 SyntaxAnalysisPhase. 10 1.2.3 Global Optimization . 12 1.2.4 CodeGeneration ....................... 13 1.2.5 Local Optimization . 15 1.2.6 Exercises ........................... 17 1.3 ImplementationTechniques . 19 1.3.1 Bootstrapping ........................ 20 1.3.2 Cross Compiling . 21 1.3.3 Compiling To Intermediate Form . 22 1.3.4 Compiler-Compilers . 24 1.3.5 Exercises ........................... 24 1.4 CaseStudy:Decaf .......................... 25 1.5 ChapterSummary .......................... 27 2 Lexical Analysis 28 2.0 FormalLanguages .......................... 28 2.0.1 LanguageElements.. .. .. .. .. .. .. .. .. 28 2.0.2 Finite State Machines . 29 2.0.3 RegularExpressions . .. .. .. .. .. .. .. .. 33 2.0.4 Exercises ........................... 36 2.1 LexicalTokens ............................ 38 2.1.1 Exercises ........................... 41 2.2 Implementation with Finite State Machines . 42 2.2.1 Examples of Finite State Machines for Lexical Analysis . 42 2.2.2 Actions for Finite State Machines . 44 2.2.3 Exercises ........................... 46 2.3 LexicalTables............................. 47 i ii CONTENTS 2.3.1 SequentialSearch.. .. .. .. .. .. .. .. .. .. 47 2.3.2 BinarySearchTree. .. .. .. .. .. .. .. .. .. 48 2.3.3 HashTable .......................... 49 2.3.4 Exercises ........................... 49 2.4 Lexical Analysis with SableCC . 51 2.4.1 SableCC Input File . 51 2.4.2 RunningSableCC ...................... 59 2.4.3 Exercises ........................... 62 2.5 CaseStudy: LexicalAnalysisforDecaf. 62 2.5.1 Exercises ........................... 65 2.6 ChapterSummary .......................... 65 3 Syntax Analysis 67 3.0 Grammars,Languages,and Pushdown Machines . 68 3.0.1 Grammars........................... 68 3.0.2 ClassesofGrammars. 70 3.0.3 Context-FreeGrammars . 73 3.0.4 PushdownMachines .. .. .. .. .. .. .. .. .. 75 3.0.5 Correspondence Between Machines and Classes of Languages 79 3.0.6 Exercises ........................... 84 3.1 Ambiguities in Programming Languages . 87 3.1.1 Exercises ........................... 89 3.2 TheParsingProblem......................... 90 3.3 Summary ............................... 91 4 Top Down Parsing 93 4.0 RelationsandClosure ........................ 94 4.0.1 Exercises ........................... 96 4.1 SimpleGrammars .......................... 97 4.1.1 Parsing Simple Languages with Pushdown Machines . 98 4.1.2 Recursive Descent Parsers for Simple Grammars . 100 4.1.3 Exercises ........................... 104 4.2 Quasi-SimpleGrammars. 105 4.2.1 Pushdown Machines for Quasi-Simple Grammars . 107 4.2.2 Recursive Descent for Quasi-Simple Grammars . 107 4.2.3 A Final Remark on ǫ Rules ................. 108 4.2.4 Exercises ........................... 111 4.3 LL(1)Grammars ........................... 111 4.3.1 Pushdown Machinesfor LL(1)Grammars . 116 4.3.2 RecursiveDescentforLL(1)Grammars . 118 4.3.3 Exercises ........................... 120 4.4 ParsingArithmeticExpressionsTopDown. 121 4.4.1 Exercises ........................... 130 4.5 Syntax-DirectedTranslation. 131 4.5.1 Implementing Translation Grammars with Pushdown Translators132 4.5.2 Implementing Translation Grammars with Recursive Descent134 CONTENTS iii 4.5.3 Exercises ........................... 137 4.6 AttributedGrammars . 137 4.6.1 Implementing Attributed Grammars with Recursive Descent139 4.6.2 Exercises ........................... 142 4.7 An Attributed Translation Grammar for Expressions . 143 4.7.1 Translating Expressions with Recursive Descent . 144 4.7.2 Exercises ........................... 147 4.8 DecafExpressions .......................... 147 4.8.1 LBL,JMP,TST,andMOVatoms . 148 4.8.2 Booleanexpressions . 148 4.8.3 Assignment .......................... 150 4.8.4 Exercises ........................... 152 4.9 TranslatingControlStructures . 153 4.9.1 Exercises ........................... 158 4.10 CaseStudy: ATopDownParserforDecaf . 159 4.10.1 Exercises ........................... 161 4.11 ChapterSummary . .. .. .. .. .. .. .. .. .. .. 162 5 Bottom Up Parsing 164 5.1 ShiftReduceParsing. .. .. .. .. .. .. .. .. .. .. 164 5.1.1 Exercises ........................... 170 5.2 LRParsingWithTables . .. .. .. .. .. .. .. .. .. 171 5.2.1 Exercises ........................... 176 5.3 SableCC................................177 5.3.1 OverviewofSableCC . 177 5.3.2 StructureoftheSableCCSourceFiles . 177 5.3.3 An Example Using SableCC . 179 5.3.4 Exercises ........................... 187 5.4 Arrays.................................192 5.4.1 Exercises ........................... 196 5.5 CaseStudy: SyntaxAnalysisforDecaf . 197 5.5.1 Exercises ........................... 199 5.6 ChapterSummary .......................... 200 6 Code Generation 202 6.1 IntroductiontoCodeGeneration . 202 6.1.1 Exercises ........................... 205 6.2 ConvertingAtomstoInstructions. 206 6.2.1 Exercises ........................... 208 6.3 Single Pass vs. Multiple Passes . 209 6.3.1 Exercises ........................... 214 6.4 Register Allocation . 215 6.4.1 Exercises ........................... 219 6.5 Case Study: A Code Generator for the Mini Architecture . 219 6.5.1 Mini: The Simulated Architecture . 220 6.5.2 TheInputtotheCodeGenerator. 222 iv CONTENTS 6.5.3 TheCodeGeneratorforMini . 223 6.5.4 Exercises ........................... 224 6.6 ChapterSummary .......................... 225 7 Optimization 227 7.1 Introduction and View of Optimization . 227 7.1.1 Exercises ........................... 229 7.2 Global Optimization . 230 7.2.1 BasicBlocksandDAGs . 230 7.2.2 Other Global Optimization Techniques . 237 7.2.3 Exercises ........................... 242 7.3 LocalOptimization.......................... 246 7.3.1 Exercises ........................... 248 7.4 ChapterSummary .......................... 250 Glossary 251 Appendix A - Decaf Grammar 263 Appendix B - Decaf Compiler 266 B.1 InstallingDecaf............................ 266 B.2 SourceCodeforDecaf . 267 B.3 CodeGenerator............................ 285 Appendix C - Mini Simulator 291 Bibliography 298 Index 301 Preface Compiler design is a subject which many believe to be fundamental and vital to computer science. It is a subject which has been studied intensively since the early 1950’s and continues to be an important research field today. Compiler design is an important part of the undergraduate curriculum for many reasons: (1) It provides students with a better understanding of and appreciation for programming languages. (2) The techniques used in compilers can be used in other applications with command languages. (3) It provides motivation for the study of theoretic topics. (4) It is a good vehicle for an extended programming project. There are several compiler design textbooks available today, but most have been written for graduate students. Here at Rowan University, our students have had difficulty reading these books. However, I felt it was not the subject matter that was the problem, but the way it was presented. I was sure that if concepts were presented at a slower pace, with sample problems and diagrams to illustrate the concepts, that our students would be able to master the concepts. This is what I have attempted to do in writing this book. This book is a revision of earlier editions that were written for Pascal and C++ based curricula. As many computer science departments have moved to Java as the primary language in the undergraduate curriculum, I have produced this edition to accommodate those departments. This book is not intended to be strictly an object- oriented approach to compiler design. Though most Java compilers compile to an intermediate form known as Byte Code, the approach taken here is a more traditional one in which we compile to native code for a particular machine. The most essential prerequisites for this book are courses in Java application programming, Data Structures, Assembly Language or Computer Architecture, and possibly Programming Languages. If the student has not studied formal languages and automata, this book includes introductory sections on these the- oretic topics, but in this case it is not likely that all seven chapters will be covered in a one semester course. Students who have studied the theory will be able to skip the preliminary sections (2.0, 3.0, 4.0) without loss of continuity. The concepts of compiler design are applied to a case study which is an implementation of a subset of Java which I call Decaf. Chapters 2, 4, 5, and 6 include