logo资料库

Programming_language_principle_and_practice(3rd).pdf

第1页 / 共666页
第2页 / 共666页
第3页 / 共666页
第4页 / 共666页
第5页 / 共666页
第6页 / 共666页
第7页 / 共666页
第8页 / 共666页
资料共666页,剩余部分请下载后查看
LibraryPirate
Kenneth C. LoudenSan Jose State UniversityKenneth A. LambertWashington and Lee UniversityPrinciples and PracticeThird EditionProgramming LanguagesAustralia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United StatesC7729_fm.indd iC7729_fm.indd i03/01/11 10:51 AM03/01/11 10:51 AM
52609_00_fm_pi-pxxvi.indd ii52609_00_fm_pi-pxxvi.indd ii2/1/10 11:37:43 PM2/1/10 11:37:43 PMThis an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorialreview has deemed that any suppresed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequentrights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visitwww.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest.sis
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher.Library of Congress Control Number: 2010939435ISBN-13: 978-1-111-52941-3ISBN-10: 1-111-52941-8Course Technology20 Channel Center StreetBoston, MA 02210USACourse Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice.The programs in this book are for instructional purposes only.They have been tested with care, but are not guaranteed for any particular intent beyond educational purposes. The author and the publisher do not offer any warranties or representations, nor do they accept any liabilities with respect to the programs.Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: www.cengage.com/globalCengage Learning products are represented in Canada by Nelson Education, Ltd.To learn more about Course Technology, visit www.cengage.com/coursetechnology Purchase any of our products at your local college store or at our preferred online store www.cengagebrain.comProgramming Languages: Principles and Practice, Third EditionKenneth C. Louden and Kenneth A. LambertExecutive Editor: Marie LeeAcquisitions Editor: Brandi ShailerSenior Product Manager: Alyssa PrattDevelopment Editor: Ann Shaff erEditorial Assistant: Jacqueline LacaireAssociate Marketing Manager: Shanna SheltonContent Project Manager: Jennifer FeltriArt Director: Faith BrosnanPrint Buyer: Julio EsperasCover Designer: Saizon DesignCover Photo: © Ocean/CorbisCompositor: IntegraCopyeditor: Foxxe EditorialProofreader: Christine ClarkIndexer: Sharon HilgenbergPrinted in the United States of America 1 2 3 4 5 6 7 17 16 15 14 13 12 11For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706For permission to use material from this text or product, submit all requests online at www.cengage.com/permissionsFurther permissions questions can be emailed topermissionrequest@cengage.comC7729_fm.indd iiC7729_fm.indd ii 03/01/11 10:51 AM03/01/11 10:51 AM
Table of ContentsiiiPreface vChapter 1Introduction1.1 The Origins of Programming Languages. . . . .31.2 Abstractions in Programming Languages . . . .81.3 Computational Paradigms. . . . . . . . . . . . . .151.4 Language Definition . . . . . . . . . . . . . . . . . .161.5 Language Translation . . . . . . . . . . . . . . . . .181.6 The Future of Programming Languages . . . .19Chapter 2Language Design Criteria2.1 Historical Overview . . . . . . . . . . . . . . . . . . .272.2 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . .282.3 Regularity. . . . . . . . . . . . . . . . . . . . . . . . . .302.4 Security . . . . . . . . . . . . . . . . . . . . . . . . . . .332.5 Extensibility . . . . . . . . . . . . . . . . . . . . . . . .342.6 C++: An Object-Oriented Extension of C . . .352.7 Python: A General-Purpose Scripting Language. . . . . . . . . . . . . . . . . . . . . . . . . .38Chapter 3Functional Programming3.1 Programs as Functions . . . . . . . . . . . . . . . .473.2 Scheme: A Dialect of Lisp . . . . . . . . . . . . . .503.3 ML: Functional Programming with Static Typing. . . . . . . . . . . . . . . . . . . . . . . .653.4 Delayed Evaluation . . . . . . . . . . . . . . . . . . .773.5 Haskell—A Fully Curried Lazy Language with Overloading . . . . . . . . . . . . . . . . . . . .813.6 The Mathematics of Functional Programming: Lambda Calculus. . . . . . . . . 90Chapter 4Logic Programming4.1 Logic and Logic Programs. . . . . . . . . . . . .1054.2 Horn Clauses . . . . . . . . . . . . . . . . . . . . . .1094.3 Resolution and Unification . . . . . . . . . . . .1114.4 The Language Prolog . . . . . . . . . . . . . . . .1154.5 Problems with Logic Programming. . . . . . .1264.6 Curry: A Functional Logic Language . . . . . .131Chapter 5Object-Oriented Programming5.1 Software Reuse and Independence . . . . . .1435.2 Smalltalk . . . . . . . . . . . . . . . . . . . . . . . . .1445.3 Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . .1625.4 C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . .1815.5 Design Issues in Object-Oriented Languages . . . . . . . . . . . . . . . . . . . . . . . .1915.6 Implementation Issues in Object-Oriented Languages . . . . . . . . . . . . . . . . . . . . . . . .195Chapter 6Syntax6.1 Lexical Structure of Programming Languages . . . . . . . . . . . . . . . . . . . . . . . .2046.2 Context-Free Grammars and BNFs. . . . . . .2086.3 Parse Trees and Abstract Syntax Trees . . . . . . . . . . . . . . . . . . . . . . .2136.4 Ambiguity, Associativity, and Precedence. . . . . . . . . . . . . . . . . . . . . . . .2166.5 EBNFs and Syntax Diagrams . . . . . . . . . . .2206.6 Parsing Techniques and Tools . . . . . . . . . .2246.7 Lexics vs. Syntax vs. Semantics . . . . . . . . . .2356.8 Case Study: Building a Syntax Analyzer for TinyAda. . . . . . . . . . . . . . . . . . . . . . . .237Chapter 7Basic Semantics7.1 Attributes, Binding, and Semantic Functions . . . . . . . . . . . . . . . . . . . . . . . . .2577.2 Declarations, Blocks, and Scope. . . . . . . . .2607.3 The Symbol Table . . . . . . . . . . . . . . . . . . .2697.4 Name Resolution and Overloading . . . . . .2827.5 Allocation, Lifetimes, and the Environment. . . . . . . . . . . . . . . . . . . . . . .2897.6 Variables and Constants . . . . . . . . . . . . . .2977.7 Aliases, Dangling References, and Garbage . . . . . . . . . . . . . . . . . . . . . . . . . .3037.8 Case Study: Initial Static Semantic Analysis of TinyAda . . . . . . . . . . . . . . . . . .309C7729_fm.indd iiiC7729_fm.indd iii03/01/11 10:51 AM03/01/11 10:51 AM
iv Table of ContentsChapter 8Data Types8.1 Data Types and Type Information. . . . . . .3288.2 Simple Types . . . . . . . . . . . . . . . . . . . . . .3328.3 Type Constructors . . . . . . . . . . . . . . . . . .3358.4 Type Nomenclature in Sample Languages . . . . . . . . . . . . . . . . . . . . . . .3498.5 Type Equivalence. . . . . . . . . . . . . . . . . . .3528.6 Type Checking . . . . . . . . . . . . . . . . . . . .3598.7 Type Conversion . . . . . . . . . . . . . . . . . . .3648.8 Polymorphic Type Checking. . . . . . . . . . .3678.9 Explicit Polymorphism. . . . . . . . . . . . . . .3768.10 Case Study: Type Checking in TinyAda . . .382Chapter 9Control I—Expressions and Statements9.1 Expressions . . . . . . . . . . . . . . . . . . . . . .4039.2 Conditional Statements and Guards . . . .4109.3 Loops and Variations on WHILE. . . . . . . .4179.4 The GOTO Controversy and Loop Exits. . .4209.5 Exception Handling. . . . . . . . . . . . . . . . .4239.6 Case Study: Computing the Values of Static Expressions in TinyAda. . . . . . . .432Chapter 10Control II—Procedures and Environments10.1 Procedure Definition and Activation. . . . .44510.2 Procedure Semantics. . . . . . . . . . . . . . . .44710.3 Parameter-Passing Mechanisms. . . . . . . .45110.4 Procedure Environments, Activations, and Allocation . . . . . . . . . . . . . . . . . . . .45910.5 Dynamic Memory Management. . . . . . . .47310.6 Exception Handling and Environments. . .47710.7 Case Study: Processing Parameter Modes in TinyAda . . . . . . . . . . . . . . . . . .479Chapter 11 Abstract Data Types and Modules11.1 The Algebraic Specification of Abstract Data Types. . . . . . . . . . . . . . .49411.2 Abstract Data Type Mechanisms and Modules. . . . . . . . . . . . . . . . . . . . . . . . .49811.3 Separate Compilation in C, C++ Namespaces, and Java Packages. . . . . . . . . . . . . . . . . . . . .50211.4 Ada Packages. . . . . . . . . . . . . . . . . . . . .50911.5 Modules in ML . . . . . . . . . . . . . . . . . . . .51511.6 Modules in Earlier Languages . . . . . . . . .51911.7 Problems with Abstract Data Type Mechanisms . . . . . . . . . . . . . . . . . . . . . .52411.8 The Mathematics of Abstract Data Types 532Chapter 12Formal Semantics12.1 A Sample Small Language. . . . . . . . . . . .54312.2 Operational Semantics . . . . . . . . . . . . . .54712.3 Denotational Semantics . . . . . . . . . . . . .55612.4 Axiomatic Semantics. . . . . . . . . . . . . . . .56512.5 Proofs of Program Correctness . . . . . . . .571Chapter 13Parallel Programming13.1 Introduction to Parallel Processing . . . . . .58313.2 Parallel Processing and Programming Languages . . . . . . . . . . . . . . . . . . . . . . .58713.3 Threads . . . . . . . . . . . . . . . . . . . . . . . . .59513.4 Semaphores . . . . . . . . . . . . . . . . . . . . . .60413.5 Monitors . . . . . . . . . . . . . . . . . . . . . . . .60813.6 Message Passing . . . . . . . . . . . . . . . . . .61513.7 Parallelism in Non-Imperative Languages . . . . . . . . . . . . . . . . . . . . . . .622C7729_fm.indd ivC7729_fm.indd iv03/01/11 10:51 AM03/01/11 10:51 AM
PrefaceThis book is an introduction to the broad field of programming languages. It combines a general presentation of principles with considerable detail about many modern languages. Unlike many intro-ductory texts, it contains significant material on implementation issues, the theoretical foundations of programming languages, and a large number of exercises. All of these features make this text a useful bridge to compiler courses and to the theoretical study of programming languages. However, it is a text specifically designed for an advanced undergraduate programming languages survey course that covers most of the programming languages requirements specified in the 2001 ACM/IEEE-CS Joint Curriculum Task Force Report, and the CS8 course of the 1978 ACM Curriculum.Our goals in preparing this new edition are to bring the language-specific material in line with the changes in the popularity and use of programming languages since the publication of the second edition in 2003, to improve and expand the coverage in certain areas, and to improve the presentation and usefulness of the examples and exercises, while retaining as much of the original text and organization as possible. We are also mindful of the findings and recommendations of the ACM SIGPLAN Programming Language Curriculum Workshop [2008], which reaffirm the centrality of the study of programming languages in the computer science curriculum. We believe that the new edition of our book will help students to achieve the objectives and outcomes described in the report, which was compiled by the leading teachers in our field.To complete this book, students do not have to know any one particular language. However, experi-ence with at least one language is necessary. A certain degree of computational sophistication, such as that provided by a course in data structures (CS2) and a discrete mathematics course, is also expected. A course in computer organization, which provides some coverage of assembly language programming and virtual machines, would be useful but is not essential. Major languages used in this edition include C, C++, Smalltalk, Java, Ada, ML, Haskell, Scheme, and Prolog; many other languages are discussed more briefly.Overview and OrganizationIn most cases, each chapter largely is independent of the others without artificially restricting the material in each. Cross references in the text allow the student or instructor to fill in any gaps that might arise even if a particular chapter or section is skipped.Chapter 1 surveys the concepts studied in later chapters, provides an overview of the history of programming languages, and introduces the idea of abstraction and the concept of different language paradigms.C7729_fm.indd vC7729_fm.indd v03/01/11 10:51 AM03/01/11 10:51 AM
vi PrefaceChapter 2 provides an overview of language design criteria. Chapter 2 could serve well as a culminating chapter for the book, but we find it arouses interest in later topics when covered here.Chapters 3, 4, and 5 concretely address three major language paradigms, beginning with the function-oriented paradigm in Chapter 3. Scheme, ML, and Haskell are covered in some detail. This chapter also introduces the lambda calculus. Chapter 4, on logic programming, offers an extended section on Prolog, and devotes another section to the functional logic language Curry. Chapter 5 deals with the object-oriented paradigm. We use Smalltalk to introduce the concepts in this chapter. Individual sections also feature Java and C++.Chapter 6 treats syntax in some detail, including the use of BNF, EBNF, and syntax diagrams. A brief section treats recursive definitions (like BNF) as set equations to be solved, a technique that recurs periodically throughout the text. One section is devoted to recursive-descent parsing and the use of parsing tools. The final section of this chapter begins a multi-chapter case study that develops a parser for a small language similar to Ada.Chapters 7, 8, 9, and 10 cover the central semantic issues of programming languages: declaration, allocation, evaluation; the symbol table and runtime environment as semantic functions; data types and type checking; procedure activation and parameter passing; and exceptions and exception handling.Chapter 11 gives an overview of modules and abstract data types, including language mechanisms for equational, or algebraic, specification.Chapter 12 introduces the three principal methods of formal semantics: operational, denotational, and axiomatic. This is somewhat unique among introductory texts in that it gives enough detail to provide a real flavor for the methods.Chapter 13 discusses the major ways parallelism has been introduced into programming languages: coroutines, threads, semaphores, monitors, and message passing, with examples primarily from Java and Ada. Its final section surveys recent efforts to introduce parallelism into LISP and Prolog, and the use of message passing to support parallel programming in the functional language Erlang.Use as a TextLike any programming languages text, this one covers a great deal of material. It should be possible to cover all of it in a two-semester or two-quarter sequence. Alternatively, there are two other, very dif-ferent ways of delivering this material. They could loosely be called the “principles” approach and the “paradigm” approach. Two suggested organizations of these approaches in a semester-long course are as follows:The principles approach: Chapters 1, 2, 3, 6, 7, 8, 9, and 10.The paradigm approach: Chapters 1, 2, 3, 4, 5, 6, 7, 8, and 13. If there is extra time, selected topics from the remaining chapters.Summary of Changes between the Second and Third EditionsThe most obvious change from the second edition is the shifting of the three chapters on non-imperative programming languages to a much earlier position in the book (from Chapters 10-12 to Chapters 3-5, with the chapter on object-oriented programming now coming after those on functional and logic C7729_fm.indd viC7729_fm.indd vi03/01/11 10:51 AM03/01/11 10:51 AM
分享到:
收藏