0% found this document useful (0 votes)
4K views668 pages

Terrence W. Pratt, Marvin V. Zelkowitz-Programming Languages - Design and Implementation-Pearson (2000) PDF

Uploaded by

Shivkant Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4K views668 pages

Terrence W. Pratt, Marvin V. Zelkowitz-Programming Languages - Design and Implementation-Pearson (2000) PDF

Uploaded by

Shivkant Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 668
TERRENCE W. PRATT MARVIN V. ZELKOWITZ Fourth Edition PROGRAMMING LANGUAGES Design and Implementation Terrence W. Pratt Center of Excellence in Space Data and Information Sciences NASA Goddard Space Flight Center, Greenbelt, MD (retired) Marvin V. Zelkowitz Department of Computer Science and Institute for Advanced Computer Studies University of Maryland, College Park, MD aaa PRENTICE HALL, Upper Saddle River, New Jersey 07458 Library of Congress Cataloging-in-Publication Data Pratt, Terrence ¥. Programming enguages : deaign and isplesentation / Terrence ¥. Pratt, Marvin Y. aetkowits, —= Ath ef, Poe Includes bibliographical references and index. {ISBN 0-19-027678-2 (alk. paper) 4, Programming languages (Electronic computers) I. Zelkovitz, Marvin ¥., 1946- IT. nie 176.7 Pr 2000 008.18--ae20 Vice president and editorial director, ECS: Marcia Horton Acquisition editor: Petra Recter “Amociate editor: Sarah Burrows Editorial assistant: Karen Schultz Production editor: Leslie Galen Managing editor: David A. George Executive managing editor: Vinee O'Brien Art director: Heather Scott “Assistant act director: John Christiana ‘Cover design: Marjory Dressler Marketing manager: Jennie Burger Manufacturing buyer: Dawn Murrin Manufacturing manager: Trudy Pisciotth Assistant vice president of production and manufacturing: David W. Riccardi © 2001, 1996, 1984, 1975 by PRENTICE-HALL, Ine. Upper Saddle River, New Jersey 07458 All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher. ‘The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness, The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in this book. The author and publisher shall not bbe liable in any event for incidental or consequential damages In connection with, or arising out of, the furnishing, performance, of use of these programs. Printed in the United States of America wos Te saa ISBN 0-13-027678-2 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall Canada Ine., Toronto Prentice-Hall Hispanoamericana, S. A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Pearson Education Asia, Pte. Ltd. Editora Prentice-Hall do Brasil, Ltda., Rio de Janciro For Kirsten, Randy, Laurie, Aaron, and Elena Preface This fourth edition of Programming Languages: Design and Implementation contin- ues the tradition developed in the earlier editions to describe programming language design by means of the underlying software and hardware architecture that is re- quired for execution of programs written in those languages. This provides the programmer with the ability to develop software that is both correct and efficient in execution. In this new edition, we continue this approach, as well as improve on the presentation of the underlying theory and formal models that form the basis for the decisions made in creating those languages. Programming language design is still a very active pursuit in the computer science community as languages are born, age, and eventually die. ‘This fourth edition represents the vital languages of the early 21" century. Postscript, Java, HTML, and Perl have been added to the languages discussed in the third edition to reflect the growth of the World Wide Web as a programming domain. The discussion of Pascal, FORTRAN, and Ada has been deemphasized in recognition of these languages’ aging in anticipation of possibly dropping them in future editions of this book. ‘At the University of Maryland, a course has been taught for the past 25 years that conforms to the structure of this book. For our junior-level course, we as- sume the student already knows C, Java, or C++ from earlier courses. We then emphasize Smalltalk, ML, Prolog, and LISP, as well as include further discussions of the implementation aspects of C++. The study of C++ furthers the students’ knowledge of procedural languages with the addition of object-oriented classes, and the inclusion of LISP, Prolog, and ME provides for discussions of different program- ming paradigms. Replacement of one or two of these by FORTRAN, Ada, or Pascal would also be appropriate. It is assumed that the reader is familiar with at least one procedural language, generally C, C++, Java, or FORTRAN. For those institutions using this book at a lower level, or for others wishing to review prerequisite material to provide a framework for discussing programming language design issues, Chapters 1 and 2 provide a review of material needed to understand later chapters. Chapter 1 is a v vi Preface general introduction to programming languages, while Chapter 2 is a brief overview of the underlying hardware that will execute the given program. ‘The theme of this book is language design and implementation issues. Chapter 3, and 5 through 12 provide the basis for this course by describing the underlying grammatical model for programming languages and their compilers (Chapter 3), elementary data types (Chapter 5), data structures and encapsulation (Chapter 6), inheritance (Chapter 7), statements (Chapter 8), procedure invocation (Chapter 9), storage management (Chapter 10), distributed processing (Chapter 11) and network programming (Chapter 12), which form the central concerns in language design. Chapter 4 is a more advanced chapter on language semantics that includes an introduction to program verification, denotational semantics, and the lambda cel- culus. It may be skipped in the typical sophomore- or junior-level course. As with the previous editions of this book, we include a comprehensive appendix that is a brief summary of the features in the 12 languages covered in some detail in this book. The topics in this book cover the 12 knowledge units recommended by the 1991 ACM/IEEE Computer Society Joint Curriculum Task Force for the programming languages subject area {TUCKER et al. 1991] Although compiler writing was at one time a central course in the computer science curriculum, there is increasing belief that not every computer science student, needs to be able to develop a compiler; such technology should be left to the compiler specialist, and the hole in the schedule produced by deleting such a course might be better utilized with courses such as software engineering, database engineering, or other practical use of computer science technology. However, we believe that aspects of compiler design should be part of the background for all good programmers. ‘Therefore, a focus of this book is how various language structures are compiled, and Chapter 3 provides a fairly complete summary of parsing issues. ‘The 12 chapters emphasize programming language examples in FORTRAN, Ada, C, Java, Pascal, ML, LISP, Perl, Postscript, Prolog, C++, and Smalltalk, Ad- ditional examples are given in HTML, PL/I, SNOBOLA, APL, BASIC, and COBOL, as the need arises. The goal is to give examples from a wide variety of languages and let the instructor decide which languages to use as programming examples during the course. Although discussing all of the languages briefly during the semester is appropri- ate, we do not suggest that the programming parts of this course consist of problems in each of these languages. We think that would be too superficial in one course. Ten programs, each written in a different language, would be quite a chore and would provide the student with little in-depth knowledge of any of these languages. We assume that each instructor will choose three or four languages and emphasize those. All examples in this book, except for the most trivial, were tested on an appro- priate translator; however, as we clearly point out in Section 1.3.3, correct execution on our local system is no guarantee that the translator is processing programs ac- cording to the language standard. We are sure that Mr. Murphy is at work here, Preface vii and some of the trivial examples may have errors. If so, we apologize for any problems that may cause. ‘To summarize, our goals in producing this fourth edition were as follows: * Provide an overview of the key paradigms used in developing modern pro- gramming languages; © Highlight several languages, which provide those features, in sufficient detail to permit programs to be written in each language demonstrating those features; « Explore the implementation of each language in sufficient detail to provide the programmer with an understanding of the relationship between a source program and its exeeution behavior; * Provide sufficient formal theory to show where programming language design fits within the general computer science research agenda; # Provide a sufficient set of problems and alternative references to allow stu- dents the opportunity to extend their knowledge of this important topic. We gratefully acknowledge the valuable comments received from the users of the third edition of this text and from the hundreds of students of CMSC 330 at the Uni- versity of Maryland who provided valuable feedback on improving the presentation contained in this book. Changes to the Fourth Edition, For users familiar with the third edition, the fourth edition has the following changes: 1. A chapter was added (Chapter 12} on the World Wide Web. Java was added as a major programming language, and an overview of HTML and Postscript were added to move the book away from the classical “FORTRAN number-crunching” view of compilers. 2. The material on object-oriented design was moved earlier in the text. to indicate its major importance in software design today. In addition, numerous other changes were made by moving minor sections around to better organize the material into a more consistent presentation. 3. We have found that the detailed discussions of languages in Part II of the third edition were not as useful as we expected. A short history of each of the 12 languages was added to the chapter that best represents the major features of that language, and the language summaries in Part II of the third edition were shortened 1s the appendix. Despite these additions, the size of the book has not increased because we deleted some obsolete material. ‘Terry Pratt, Howardsville, Virginia Marv Zelkowitz, College Park, Maryland Preface 1 Language Design Issues aa 1.2 13 14 15 16 LT Why Study Programming Languages? A Short History of Programming Languages 1.2.1 Development of Early Languages 1.2.2 Evolution of Software Architectures 1.23 Application Domains Role of Programming Languages 1.3.1 What Makes a Good Language? 1.3.2 Language Paradigms 1.3.3. Language Standardization 1.3.4 Internationalization Programming Environments 14.1 Effects on Language Design 14.2 Environment Frameworks 143 Job Control and Process Languages C Overview Suggestions for Further Reading Problems 2 Impact of Machine Architectures 21 ‘The Operation of a Computer 2.1.1 Computer Hardware 2.1.2 Firmware Computers 2.1.3 Translators and Virtual Architectures Contents wee Contents 22. Virtual Computers and Binding Times 2.2.1 Virtual Computers and Language Implementations 2.2.2 Hierarchies of Virtual Machines 2.23 Binding and Binding Time 224 Java Overview 2.3 Suggestions for Further Reading 24 Problems Language ‘Translation Issues 3.1 Programming Language Syntax 3.1.1 General Syntactic Criteria 31.2 Syntactic Elements of a Language 3.1.3 Overall Program-Subprogram Structure 3.2. Stages in Translation 3.2.1 Analysis of the Source Program 3.22 Synthesis of the Object Program 3.3 Formal Translation Models 3.3.1 BNF Grammars 3.3.2 Finite-State Automata 3.3.3. Perl Overview 33.4 Pushdown Automata 3.3.5 General Parsing Algorithms 34 Recursive Descent Parsing 3.5 Pascal Overview 3.6 Suggestions for Further Reading 3.7 Problems Modeling Language Properties 4.1 Format Properties of Languages 4.1.1 Chomsky Hierarchy 4.12 Undecidability 4.13 Algorithm Complexity 42 Language Semantics 4.2.1 Attribute Grammars 4.2.2 Denotational Semantics 4.23 ME Overview 4.24 Program Verification 4.25 Algebraic Data Types 87 59 61 67 67 69 69 70 4 7 81 87 97 100 103 104 105 107 110 110 113 4 15 118 123 125 128 130 138 139 143 CONTENTS, xi 43 44 Suggestions for Further Reading Problems 5 Elementary Data Types 5a 52 53 5A 55 5.6 Properties of Types and Objects 5.1.1 Data Objects, Variables, and Constants 5.12 Data Types 5.1.3 Declarations 5.14 ‘Type Checking and Type Conversion 5.1.5 Assignment and Initialization Scalar Data Types 5.2.1 Numeric Data Types 5.2.2 Enumerations 5.2.3. Booleans 5.2.4 Characters Composite Data Types 5.3.1 Character Strings 5.3.2 Pointers and Programmer-Constructed Data Objects 5.3.3. Files and Input-Output FORTRAN Overview Suggestions for Further Reading Problems 6 Encapsulation 6.1 6.2 63 Structured Data Types 6.1.1 Structured Data Objects and Data Types 6.1.2 Specification of Data Structure Types 6.1.3 Implementation of Data Structure Types 6.14 Declarations and Type Checking for Data Structures Vectors and Arrays Records Lists Sets 6.1.9 Executable Data Objects Abstract Data Types 6.2.1 Evolution of the Data Type Concept 6.2.2 Information Hiding Encapsulation by Subprograms 146 uaz 150 150 150 155 161 163 168 17 172 179 180 182 182 183 186 189 194 196 196 200 201 202 202 208 220 227 231 234 234 235, 235, 238 Contents 6.3.1 Subprograms as Abstract Operations 6.3.2 Subprogram Definition and Invocation 6.3.3 Subprogram Definitions as Data Objects 64 ‘Type Definitions 64.1 ‘Type Equivalence 6.4.2 ‘Type Definitions with Parameters 6.5 C++ Overview 6.6 Suggestions for Further Reading 6.7 Problems Inheritance 7.1 Abstract Data Types Revisited 7.2 Inheritance 7.24 Derived Classes 7.2.2 Methods 7.23 Abstract Classes 7.2.4 Smalltalk Overview 7.2.5 Objects and Messages 7.2.6 Abstraction Concepts 7.3 Polymorphism 7.4 Suggestions for Further Reading 7.5 Problems Sequence Control 8.1 Implicit and Explicit Sequence Control 8.2 Sequencing with Arithmetic Expressions 8.2.1 ‘Tree-Structure Representation 82.2 Execution-Time Representation 8.3 Sequence Control Between Statements 8.3.1 Basic Statements 83.2. Structured Sequence Control 8.3.3 Prime Programs 84 Sequencing with Nonarithmetic Expressions 84.1 Prolog Overview 8.4.2 Pattern Matching 84.3 Unification 844 Backtracking 8.4.5 Resolution 238 240 246 246 249 252 254 256 257 264 264 272 273, 27 279 280 282 286, 288 291 291 293 293 294 295 302 308 308 34 323 327 327 329 333 340 CONTENTS: xiii 8.5 Suggestions for Further Reading 342 8.6 Problems 342 9 Subprogram Control 345 9.1 Subprogram Sequence Control 345 9.1.1 Simple Call-Return Subprograms 347 9.1.2 Recursive Subprograms 353 9.13 The Pascal Forward Declaration 355 9.2 Attributes of Data Control 387 9.2.1 Names and Referencing Environments 358 9.2.2. Static and Dynamic Scope 363 9.2.3 Block Structure 366 9.2.4 Local Data and Local Referencing Environments 368 9.3 Parameter Transmission 374 9.3.1 Actual and Formal Parameters 375 9.3.2 Methods for Transmitting Parameters 377, 9.3.3 Transmission Semantics 380 9.3.4 Implementation of Parameter ‘Transmission 382 94 Explicit Common Environments 393 9.4.1 Dynamic Scope 396 9.4.2 Static Scope and Block Structure 400 9.5 Suggestions for Further Reading 408 96 Problems 408 10 Storage Management 415 10.1 Elements Requiring Storage 416 10.2 Programmer. and System-Controlled Storage 47 10.3 Static Storage Management 419 10.4 Heap Storage Management 419 10.4.1 LISP Overview 420 10.4.2 Fixed-Size Elements 422 10.4.3. Variable-Size Elements 430 10.5 Suggestions for Further Reading 432 10.6 Problems 432 11 Distributed Processing 436 11.1 Variations on Subprogram Control 436 11.1.1 Exceptions and Exception Handlers 437 xiv 12 113 14 15 116 11.1.2 Coroutines 11.1.3 Scheduled Subprograms Parallel Programming 11.2.1 Concurrent Execution 11.2.2 Guarded Commands 11.2.3 Ada Overview 1.2.4 Tasks 11.2.5 Synchronization of Tasks Hardware Developments 11.3.1 Processor Design 11.3.2 System Design Software Architecture 11.4.1 Persistent Data and Transaction Systems 11.4.2 Networks and Client-Server Computing Suggestions for Further Reading Problems 12 Network Programming 12.1 12.2 12.3 124 Desktop Publishing 12.1.1 IATEX Document Preparation 12.1.2 WYSIWYG Editors 12.1.3 Postscript 12.14 Postscript Virtual Machine ‘The World Wide Web 12.2.1 The Internet 122.2 CGI Scripts 12.2.3 Java Applets 12.2.4 XML Suggestions for Further Reading Problems A Language Summaries AL A2 Ada, A.L.1 Data Objects A.1.2 Sequence Control c A21 Data Objects A2.2 Sequence Control 441 443, 445 447 448 450, 455 465 467 470 472 472 a7 475, 476 479 481 481 484 485 490 491 505 507 508 510 510 513 520 528 530, 534

You might also like