Design and Implementation of Compiler

Design and Implementation of Compiler

This page intentionally left blank Copyright © 2009, New Age International (P) Ltd., Publishers Published by New Age International (P) Ltd., Publishers All rights reserved. No part of this ebook may be reproduced in any form, by photostat, microfilm, xerography, or any other means, or incorporated into any information retrieval system, electronic or mechanical, without the written permission of the publisher. All inquiries should be emailed to [email protected] ISBN (13) : 978-81-224-2865-0 PUBLISHING FOR ONE WORLD NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS 4835/24, Ansari Road, Daryaganj, New Delhi - 110002 Visit us at www.newagepublishers.com Dedicated totoDedicated Dear Shantanu This page intentionally left blank PREFACE This book attempts to provide a unified overview of the broad field of compiler and their practical implementa- tion. The organization of book reflects an attempt to break this massive subject into comprehensible parts and to build piece by piece, a survey of the state of art. This book emphasizes basic principles and topics of fundamental importance concerning the design issues and architecture of this field and provides detailed discussion leading edge topics. Although the scope of this book is broad, there are a number of basic principles that appear repeatedly as themes and that unify this field. This book addresses a full spectrum of compiler design. The emphasis is on solving the problems universally encountered in designing a compiler regardless of the source language or target machine. The book examines alternative approaches to meeting specific design requirements of compiler. The concepts of designing and step-by-step practical implementation of compiler in ‘C’ language are main focus of the book. The book is intended for students who have completed a programming based two-semester introductory computer sequence in which they have written programs that implemented basic algorithms manipulate discrete structures and apply basic data structures and have studied theory of computation also. An important feature of the book is the collection of problems and exercises. Across all the chapters, the book includes over 200 problems, almost all of the developed and class-tested in homework and exams as part of our teaching of the course. We view the problems as a crucial component of the book, and they are structured in keeping with our overall approach to the material. Basically, the book is divided according to different phases of compilers. Each chapter includes problems and suggestions for further reading. The chapters of the book are sufficiently modular to provide a great deal of flexibility in the design of course. The first and second chapters give a brief introduction of the language and compiler is important to understand the rest of the book. The approach used in different phases of compiler covers the various aspects of designing a language translator in depth. To facilitate easy understanding, the text book and problems have been put in simpler form. The concepts are supported by programming exercise in ‘C’ language. This book certainly helps the readers to understand the subject in better way. For many instructors, an important component of compiler course is a project or set of projects by which student gets hands-on experience to reinforce concepts from the text. This book provides an unparalleled degree of support for including a projects component in the course. The two projects on compiler in Chapter 12 will be helpful to students to deep understanding the subject. The book is intended for both academic and professional audience. For the professional interested in this field, the book serves as a basic reference volume and suitable for self study. The book is primarily designed for use in a first undergraduate course on the compiler; it can also be used as the basis for an introductory graduate viii Preface course. This book covers the syllabus of U.P. Technical University, Lucknow and other Universities of India. As a textbook it can be used for a one semester course. It is said, “To err in human, to forgive divine.” In this light I wish that the short comings of the book will be forgiven. At the same time the authors are open to any kind of constructive criticism and suggestions for further improvement. Intelligent suggestions are welcome and we will try our best to incorporate such valuable suggestions in the subsequent edition of this book. AUTHORS Contents ix ACKNOWLEDGEMENTS Once again it is pleasure to acknowledge our gratitude to the many persons involved, directly, or indirectly, in production of this book. First of all, we would like to thank almighty God who gave us the inspiration to take up this task. Prof. S.K. Bajpai of Institute of Engineering and Technology Lucknow and Shantanu student at S.R.M.S., CET Bareilly were key collaborator in the early conceptualization of this text. For the past several years, the development of the book has benefited greatly from the feedback and advice of the colleagues of various institutes who have used prepublication drafts for teaching. The course staffs we’ve had in teaching the subject have been tremendously helpful in the formulation of this material. We thank our undergraduate and graduate teaching assistants of various institutes who have provided valuable in-sights, suggestions, and comments on the text. We also thank all the students in these classes who have provided comments and feedback on early drafts of the book over the years. Our special thanks to Shantanu for producing supplemen- tary material associated with the book, which promises to greatly extend its utility to future instructors. We wish to express our deep sense of gratitude to Mrs. Veena Mathur, Executive Chairperson, Prof. Manish Sharma, Dr. Neeraj Saxena of RBMI Group of Institutions, and Mr. Devmurtee, Chairman, Prof. Prabhakar Gupta, Saket Aggrawal of SRMS CET, Bareilly, Mr. Zuber Khan, Mr. Ajai Indian of IIMS, Bareilly and Dr. S.P. Tripathi, Dr. M H. Khan of IET, Lucknow for their kind co-operation and motivation for writing this book. We would particularly like to thank our friends Dr. Verma S. at Indian Institute of Information Technology, Allahabad and Prof. Anupam Shrivastav at IFTM, Moradabad for numerous illuminating conversations and much stimu- lating correspondence. We are grateful to M/s New Age International (P) Limited, Publishers and the staff of editorial department for their encouragement and support throughout this project. Finally, we thank our parents and families— Dr. Deepali Budhoria, Mrs. Nishi Sharma, Mrs. Pragati Varshney, and kids Ritunjay, Pragunjay and sweet Keya. We appreciate their support, patience and many other contributions more than we can express in any acknowledgements here. AUTHORS This page intentionally left blank CONTENTS Preface vii Acknowledgements ix CHAPTER 1: Introduction to Language 1–40 1.1 Chomsky Hierarchy 2 1.2 Definition of Grammar 3 1.2.1 Some Other Definitions 3 1.3 Regular Languages 4 1.3.1 Algebraic Operation on RE 5 1.3.2 Identities for RE 5 1.3.3 Algorithm 1.1 (Anderson Theorem) 6 1.4 Regular Definitions 6 1.4.1 Notational Shorthand 7 1.4.2 Closure Properties for Regular Language 7 1.5 Automata 8 1.5.1 Definition of an Automata 8 1.5.2 Characteristics of an Automata 8 1.5.3 Finite Automata 8 1.5.4 Mathematical Model 9 1.6 Context Free Languages 19 1.6.1 Closure Properties 19 1.6.2 Derivations/Parse Tree 19 1.6.3 Properties of Context-free Languages 21 1.6.4 Notational Shorthand 22 1.6.5 Pushdown Automaton 22 1.7 Context Sensitive Languages 26 xii Contents 1.7.1 Linear Bounded Automata (LBA) 26 1.8 Recursively Enumerable Languages 26 1.8.1 Turing Machines 26 1.9 Programming Language 26 1.9.1 History 27 1.9.2 Purpose 29 1.9.3 Specification 30 1.9.4 Implementation 30 Examples 32 Tribulations 36 Further Readings and References 39 CHAPTER 2: Introduction to Compiler 41–77 2.1 Introduction to Compiler 42 2.1.1 What is the Challenge? 44 2.2 Types of Compilers 44 2.2.1 One-pass versus Multi-pass Compilers 44 2.2.2 Where Does the Code Execute? 47 2.2.3 Hardware Compilation 47 2.3 Meaning of Compiler Design 47 2.3.1 Front End 48 2.3.2 Back End 48 2.4 Computational Model: Analysis and Synthesis 49 2.5 Phases of Compiler and Analysis of Source Code 49 2.5.1 Source Code 49 2.5.2 Lexical Analysis 50 2.5.3 Syntax Analysis 51 2.5.4 Semantic Analysis 52 2.5.5 Intermediate Code Generation 52 2.5.6 Code Optimization 53 2.5.7 Code Generation 54 2.5.8 Out Target Program 56 2.5.9 Symbol Table 56 2.6 Cousins/Relatives of the Compiler 57 2.6.1 Preprocessor 57 2.6.2 Assembler 58 Contents xiii 2.6.3 Loader and Linker 58 2.7 Interpreter 60 2.7.1 Byte Code Interpreter 60 2.7.2 Interpreter v/s Compiler 60 2.8 Abstract Interpreter 60 2.8.1 Incentive Knowledge 60 2.8.2 Abstract Interpretation of Computer Programs 61 2.8.3 Formalization 61 2.8.4 Example of Abstract Domain 61 2.9 Case Tool: Compiler Construction Tools 62 2.10 A Simple Compiler Example (C Language) 64 2.11 Decompilers 64 2.11.1 Phases 64 2.12 Just-in-Time Compilation 66 2.13 Cross Compiler 67 2.13.1 Uses of Cross Compilers 68 2.13.2 GCC and Cross Compilation 68 2.13.3 Canadian Cross 69 2.14 Bootstrapping 69 2.15 Macro 71 2.15.1 Quoting the Macro Arguments 71 2.16 X-Macros 72 Examples 73 Tribulations 75 Further Readings and References 76 CHAPTER 3: Introduction to Source Code and Lexical Analysis 79–99 3.1 Source Code 80 3.1.1 Purposes 80 3.1.2 Organization 80 3.1.3 Licensing 81 3.1.4 Quality 81 3.1.5 Phrase Structure and Lexical Structure 82 3.1.6 The

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    423 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us