13
FUNDAMENTALS OF PROGRAMMING LANGUAGES

FUNDAMENTALS OF PROGRAMMING LANGUAGES - …978-3-642-96729-0/1.pdf · Ellis Horowitz Fundamentals of Programming Languages With 43 Figures and 34 Tables Springer-Verlag Berlin -Heidelberg

  • Upload
    doxuyen

  • View
    221

  • Download
    2

Embed Size (px)

Citation preview

FUNDAMENTALS OF PROGRAMMING LANGUAGES

Ellis Horowitz

Fundamentals of Programming Languages

With 43 Figures and 34 Tables

Springer-Verlag Berlin -Heidelberg New York 1983

Copyright © 1983 Computer Science Press

All rights reserved. No part of this work may be reproduced, transmitted, or stored in any form or by any means, without the prior written consent of the publisher, except by a reviewer who may quote brief passages in a review or as provided for in the Copyright Act of 1976.

Computer Science Press. Inc. 11 Taft Court Rockville, Maryland 20850

1 2 3 4 5 6

Library of Congress Cataloging in Publication Data

Horowitz, Ellis. Fundamentals of programming languages.

Bibliography: p. 1. Programming languages (Electronic computers)

I. Title. QA76.7.H67 001.64'24 82-5178

87 86 85 84 83

ISBN-13: 978-3-642-96731-3 e-ISBN-13: 978-3-642-96729-0 DOl: 10.1007/978-3-642-96729-0 AACR2

This book is distributed exclusively by Springer-Verlag Berlin-Heidelberg New York outside of North America, The People's Republic of China, Taiwan, and Japan.

ISBN-13: 978-3-642-96731-3

Copyright © 1983 by the author. All rights reserved. So1'tcover reprint of the hardcover 1st edition 1983

2145/3140-543210

Dedicated to Maryanne, Ruth, Edward,

Cecile, Mom and Dad.

Preface

" .. .1 always worked with programming languages because it seemed to me that until you could understand those, you really couldn't understand computers. Understanding them doesn't really mean only being able to use them. A lot of people can use them without understanding them." Christopher Strachey

The development of programming languages is one of the finest intellectual achievements of the new discipline called Computer Science. And yet, there is no other subject that I know of, that has such emotionalism and mystique associated with it. Thus my attempt to write about this highly charged subject is taken with a good deal of caution. Nevertheless, in my role as Professor I have felt the need for a modern treatment of this subject.

Traditional books on programming languages are like abbreviated language manuals, but this book takes a fundamentally different point of view. I believe that the best possible way to study and understand today's programming languages is by focusing on a few essential concepts. These concepts form the outline for this book and include such topics as variables, expressions, statements, typing, scope, procedures, data types, exception handling and concurrency. By understanding what these concepts are and how they are realized in different programming languages, one arrives at a level of comprehension far greater than one gets by writing some programs in a

vi

Preface vB

few languages. Moreover, knowledge of these concepts provides a framework for understanding future language designs.

This approach to the study of programming languages is analogous to the way natural languages are studied by linguists. According to the linguists Victoria Fromken and Robert Rodman, (in An Introduction to Language, Holt-Rinehart-Winston), "a linguist attempts to find out the laws of a language, and the laws which pertain to all languages." In their book they try to identify the universals of syntax, semantics and phonology (sounds). But unlike the linguists who see their role as identifying these universals, computer scientists can hope for more. The study of the nature of programming languages can lead both to better understanding of existing languages and also to superior language designs in the future. The approach taken here hopes to support those goals.

When Jean Sammet's book Programming Languages: History and Fundamentals first appeared, with its famous cover of the Tower of Babel of programming languages, FORTRAN and COBOL were overwhelmingly used in scientific/commercial computing and in academia FORTRAN was preeminent. ALGOL60, PUl, BASIC and others were all being used, but they only accounted for a small percentage of the programs written in a higher level language. Today the situation is far more diverse. Pascal and its derivatives are making major inroads in academic and microprocessor use. PUl usage has increased substantially. The successful UNIX operating system has given us a cadre of programmers who know their (A, Band) C. APL continues to attract a devoted body of followers who use it to solve every type of computing task. And, many other languages continue to be used. To further compound this diversity, new languages 100m on the horizon with the definite possibility of their becoming very important. I'm thinking in particular of the new U.S. Department of Defense sponsored language called Ada. The Tower of Babel of '69 which seemed to capture our sense of frustration over the proliferation of languages has today grown even larger. Thus, it becomes ever more essential that when we study the subject of programming languages we concentrate on the fundamental concepts and issues.

Is a comparative study of programming languages possible? The imperative programming language has now been with us for over 25 years. Many of its concepts are well-understood. Other concepts, though still controversial, assume a very similar form when the decision is made to incorporate them into a language. Thus, it is possible to present a great deal about these languages in an analytical manner. Other concepts such as exception handling and concurrency are newer

viii Preface

and hence less well-understood. A treatment of these topics can only hope to present the issues and point out the strengths and weaknesses of existing language features which support these concepts, but it cannot provide all the answers.

Though much work has been done on imperative programming languages, one must not overlook the area of functional programming languages. As a radically different model of programming they must be studied. Moreover, the continuing success of LISP indicates that programming languages based on these ideas have practical as well as theoretical merit. Nevertheless the bulk of the research on and development of programming languages has been in the imperative school and that is necessarily reflected here.

This book follows the guidelines which were stated by the ACM Curriculum Committee for the design of the course CS 8, "Organization of Programming Languages." See the Communications of the ACM, March 1979 for complete details. It is appropriate for undergraduate computer science majors who have had one year of programming. Knowledge of Pascal is desirable, but PU1, ALGOL60, ALGOL68 or something similar are adequate substitutes. The book has also been used successfully in a first year graduate course whose objective was to survey the important features of current languages.

I have taught a course based on this book to two different audiences. The first group consisted of sophomores and juniors with Pascal and data structures in their background. I had a lab in which the instructors first taught PUI and then LISP. Programming exercises were included. In the lectures I worked straight through the book, selecting many of my examples from PUI and Pascal. I recently replaced PUI by Ada. The second group consisted of seniors and graduate students in computer science. There was no laboratory. Instead each student was given responsibility for a different aspect of a programming language. For example one student presented an overview of Ada, another discussed Ada packages and another Ada tasks. There were Pascal, APL, ALGOL68 and Euclid experts and they all lectured on the salient features of their respective programming languages. By working closely with each student I felt I was able to maintain a consistently high level of presentation.

When discussing a programming language it often becomes essential to refer to its defining document. This caused me to put together a set of language reference manuals for my classes. In addition I found it educational and entertaining to include a variety of articles by well­known researchers. This effort eventually led me to produce an anthology of source material which I am publishing jointly with this text.

Preface ix

It is called Programming Languages: A Grand Tour. It contains some classic papers such as the" ALGOL60 report," some controversial papers such as "Can Programming be Liberated from the von Neumann Style?" by John Backus, plus overviews of Pascal, Concurrent-Pascal, Ada, MODULA, C, ALGOL68, CLU, Euclid, FORTRAN77, LISP and APL. In addition it contains the complete language reference manuals for Ada, ALGOL60, ALGOL-W, C, and MODULA. I hope the reader and instructor will find the anthology a useful reference and adjunct to this text.

A few sentences about Ada seem appropriate. During the time I was writing this book I watched Ada grow from a requirements document to a standardized programming language. As I studied the language, I began to incorporate it into my classes. Now I observe that many of my examples come from Ada. My main justification is because Ada is one of the very few languages which has included, in some way, all of the language features I wish to discuss. Therefore it provides a common thread, especially throughout the later chapters. This use of Ada caused me to reprint the entire Ada language reference manual in the accompanying anthology. Ada will doubtlessly gain a significant share of the programming language market. Its use here is not an endorsement, but the recognition of its scope and its growing importance.

Many people have contributed their efforts to the improvement of this book and I would like to acknowledge their assistance. Of primary help was the students who attended my classes at USC and at the Technion in Israel. John Guttag and Barbara Liskov of MIT through their own course on programming languages were an inspiration. Several people provided in-depth reviews including Richard Gillmann, Paul Hilfinger, David Jefferson, and Richard Schwartz. Their insights supplied a great deal of guidance to me for which I am grateful. Students Alfons Kemper, Georg Raeder, Simon Wegner and Ronald Williamson also supplied helpful remarks. Brian Reid and Ray Bates helped me with the text preparation system, Scribe. And I thank USC's Information Sciences Institute for access to their facilities.

The study of programming languages is fascinating but not easy. hope that this book sets people looking and thinking in the right direction.

Ellis Horowitz University of Southern California 1982

Acknowledgments

I would like to officially acknowledge and thank the following people and organizations for granting me permission to reprint various programs and algorithms: the inner product program of Chapter 1 comes from [Backus 78], reprinted by permission from ACM; Algorithm 6-7 comes from [Johnston 711, reprinted by permission from ACM; Algorithm 7-5 reprinted by permission of the author Bob Schwanke; Algorithm 7-11 comes from [Grune 771, reprinted by permission from ACM; Structure 8-3 comes from [Hoare 72] reprinted by permission of the publisher; Structures 8-5 and 8-6 come from [Chang 78] reprinted by permission of the authors; Algorithm 9-2 comes from [Liskov 79] and is reprinted by permission of the author; Algorithm 10-6 comes from [Holt 78] and is reprinted by permission of the author; Algorithm 10-7 comes from [Brinch-Hansen 75] and is reprinted by permission of the author; Algorithm 10-8 comes from [Wirth 77b] and is reprinted by permission of the publisher; Algorithm 10-9 comes from [Hoare 78] and is reprinted by permission of ACM; Algorithms 10-10 - 10-14 come from [Welsh 79] and are reprinted by permission of the author; Algorithms 10-16 and 10-17 come from [Welsh 811 and are reprinted by permission of the author; Three examples of VAL programs come from [Dennis 79] and are reprinted by permission of the author; Structure 8-7, Table 9-3, Algorithms 6-10, 7-6 and 10-15 and Table 11-6 all come from [Ada 80). Figure 1-4 was originally conceived and appeared as the front cover of the Jan. 1961 issue of C.ACM and is reprinted by their permission; Figure 7-4 comes from [Jensen-Wirth]; Algorithm 12-1 comes from [Lisp] reprinted by permission of the author.

This book was composed and typeset entirely by computer. The text preparation system is Scribe from Unilogic Inc. and the phototypsetter was the CAT /4 from Graphics Systems Inc ..

x

CONTENTS

PREFACE...................................................... vi

1. THE EVOLUTION OF PROGRAMMING LANGUAGES.... 1

1.1 Early History. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1. 2 Early Modern History ...................................... 5 1.3 FORTRAN and ALGOL 60 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.4 The Stormy '60s ........................................... 13 1.5 Advances in the '70s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 22

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 29 Exercises ................................................. 30

2. THE CHALLENGE OF PROGRAMMING LANGUAGE DESIGN ................................................. 33

2.1 Criteria for Language Design ................................ 35 2.2 Some Possible Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 40 2.3 Imperative vs. Applicative Languages. . . . . . . . . . . . . . . . . . . . . . . .. 45

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 48 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 49

3. DEFINING SYNTAX...................................... 51

3.1 The Character Set. ... . . . .. . . .. . .. . . .. . . ... . . . .. . . . . . . . . . .. 51 3.2 BNF ..................................................... 55 3.3 Snytax Graphs ............................................ 61 3.4 Syntax and Program Reliability .............................. 70

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 76 Exercises ................................................. 76

4. VARIABLES, EXPRESSIONS AND STATEMENTS ......... 81

4.1 Variables and the Assignment Statement. . .. . . . . . .... . . . . . . . .. 81 4.2 Binding Time and Storage Allocation. . . . . . . . . . . . . . . . . . . . . . . .. 86 4.3 Constants and Initialization ................................. 90

xi

xii Contents

4.4 Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 93 4.5 Conditional Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 98 4.6 Iterative Statements ........................................ 101 4.7 The GOTO Statement and Labels ............................ 106 4.8 A First Look at Ada ........................................ 109

Concepts Discussed in This Chapter .......................... 113 Exercises ................................................. 113

5. TYPES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 121

5.1 Data Types and Typing ..................................... 121 5.2 Enumerated Data Types .................................... 123 5.3 Elementary Data Types ..................................... 125 5.4 Pointer Data Type ......................................... 136 5.5 Structured Data Types ..................................... 140 5.6 Type Coercion ............................................. 152 5.7 Type Equivalence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 155 5.8 A Look at Ada and Types ................ . . . . . . . . . . . . . . . . . .. 159

Concepts Discussed in This Chapter .......................... 164 Exercises ........ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 164

6. SCOPE AND EXTENT ................................... 169

6.1 The Basics ................................................ 169 6.2 Run-time Implementation .................................. 179 6.3 An Extended Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 187 6.4 Binding, Scope and Extent Revisited ......................... 189 6.5 A Look at Ada and Scope ................................... 195

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 199 Exercises .............................. . . . . . . . . . . . . . . . . . .. 199

7. PROCEDURES ........................................... 203

7.1 General Features .......................................... 203 7.2 Parameter Evaluation and Passing ............................ 206 7.3 Call-By-Name ............................................. 209 7.4 Specification of Objects in a Procedure ........................ 211 7.5 Aliasing .................................................. 218 7.6 Overloading ............................................... 219 7.7 Generic Functions ......................................... 223 7.8 Coroutines ................................................ 226

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 235 Exercises ............................ . . . . . . . . . . . . . . . . . . . .. 235

Contents xiii

8. DATA ABSTRACTION ................................... 241

8.1 An Introduction ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 241 8.2 MODULA ................................................ 245 8.3 Euclid .................................................... 251 8.4 Ada ...................................................... 255 8.5 SIMULA 67 ............................................... 258 8.6 Abstract Data Types ...................................... , 263

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 270 Exercises ........... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 270

9. EXCEPTION HANDLING ................................. 273

9.1 Design Issues .............................................. 273 9.2 PLiION-Conditions ....................................... 276 9.3 Exception Handling in CLU ................................. 281 9.4 Exception Handling in MESA ............................... 284 9.5 Exception Handling in Ada ................................. , 286

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 292 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 292

10. CONCURRENCY ......................................... 295

10.1 Basic Concepts ............................................ 295 10.2 Semaphores ............................................... 300 10.3 Monitors ................................................. 305 10.4 Message Passing ........................................... 313 10.5 Concurrency in Ada ........................................ 320

Concepts Discussed in This Chapter .......................... 332 Exercises ................................................. 332

11. INPUT-OUTPUT ......................................... 337

Concepts Discussed in This Chapter .......................... 351 Exercises ................................................. 352

12. FUNCTIONAL PROGRAMMING .......................... 355

12.1 What is Functional Programming ............................ 355 12.2 The Basics of LISP ......................................... 358 12.3 The LISP Interpreter ....................................... 367 12.4 FUNARGs and FEXPRs .................................... 373 12.5 The PROG Feature ........................................ 378

xiv Contents

12.6 Delayed Evaluation ........................................ 381 Concepts Discussed in This Chapter .......................... 385 Exercises .............. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 385

13. DATA FLOW PROGRAMMING LANGUAGES ............ 389

13.1 The Data Flow Model ...................................... 389 13.2 Language Design Goals ..................................... 397 13.3 VAL - A Data Flow Programming Language .................. 400

Concepts Discussed in This Chapter . . . . . . . . . . . . . . . . . . . . . . . . .. 407 Exercises ....... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 407

14. OIDECT ORIENTED PROGRAMMING LANGUAGES ..... 409

14.1 What Is Object Oriented Programming ....................... 409 14.2 The Smalltalk Programming Language ........................ 412 14.3 The Smalltalk Environment ................................. 419 14.4 A Retrospective ............................................ 423

Concepts Discussed in This Chapter .......................... 425 Exercises ................................................. 425

REFERENCES ............................................ 427

INDEX ................................................... 439