Springer Books on Professional Computing Edited bl,J Henn,J Ledgord Springer Books on Professional Computing Computer Confidence: A Human Approach to Computers Bruce D. Sanders. viii, 90 pages. 23 figures. 1984. ISBN 0-387-90917-6 The Unix System Guidebook: An Introductory Guide for Serious Users Peter P. Silvester. xi, 207 pages. 6 figures. 1984. ISBN 0-387-90906-0 The American Pascal Standard: With Annotations Henry Ledgard. vii, 97 pages. 1984. ISBN 0-387-91248-7 Modula-2 for Pascal Progrflmmers Richard Gleaves. x, 145 pages. 18 figures. 1984. ISBN 0-387-96051-1 Ada ® in Practice Christine N. Ausnit, Norman H. Cohen, John B. Goodenough, R. Sterling Eanes. xv, 192 pages. 79 figures. 1985. ISBN 0-387-96182-8 The World of Programming Languages Michael Marcotty, Henry Ledgard. xvi, 360 pages. 30 figures. 1986. ISBN 0-387-96440-1 The World of Programming Languages Michael Marcotty Henry ledgard With 30 Illustrations Springer-Verlag New York Berlin Heidelberg London Paris Tokyo Michael Marcotty Computer Science Department General Motors Research Laboratories Wanen, MI 48090-9055 U.S.A. Henry Ledgard Drummer Hill Road, RFD Amhersl, MA 01002 U.S.A. Series Editor Henry Ledgard Library of Congress Cataloging-in-Publiclllion Data MarcoUy, Michael The world of programming languages. (Springer books on professional computing) Bibliography: p. Includes index. I. Programming language~ (Electronic computers) I. Ledgard, Henry F. II. Title. Ill. Series. QA76.7.M346 1986 005.13 86-26083 ,,,,,,,,,,,,/(',,,('111"';',,,. Original te;<tbook edition, PrQgr(ll"",i"x Lt"'~""Ke Umd.• c "pt'; S),,,uu. Se",,,"';c. •. published by Science Research Associates. Inc ..C hicago, Illinois. Copyright C 1986. 1981. C 1987 by Science Research Associates. Inc. All rights reserved. Trade edition reprinted by permission. This work may nOi be translated orcopicd in whole or in pari without the wrinen permission of the CQPyright Il<:lIder (Science Research Associates. Inc .. 155 Nonh Wacker Drive. Chicago. Illinois 60606, U.S.A.). ellcept for brief excerpts in con nection wilh reviews or scholarly analysis. Use in connection wilh any fonn of information slor.;!ge and retrieval. electronic adaptation. computer software. or by similar ()r dissimilar methodology now kllOwn or hereafter developed is forbidden. The use of general descriptive names. trade names. trademarks. etc. in this publication. even if the former are not especially identified. is not to be taken as a sign that such names, as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used freely by anyone. 9 8 7 6.') 4 3 2 I ISBN -13: 978·0-387-96440-9 e-ISBN-13:978-I- 4612-4692-3 DOl: 10.1007/978-1-4612-4692-3 Preface The earth, viewed through the window of an airplane, shows a regularity and reptition of features, for example, hills, valleys, rivers, lakes, and forests. Nevertheless, there is great local variation; Vermont does not look like Utah. Similarly, if we rise above the details of a few programming languages, we can discern features that are common to many languages. This is the programming language landscape; the main features include variables, types, control structures, and input/output. Again, there is local variation; Pascal does not look like Basic. This work is a broad and comprehensive discussion of the principal features of the major programming languages. A Study of Concepts The text surveys the landscape of programming languages and its features. Each chapter concentrates on a single language concept. A simple model of the feature, expressed as a mini-language, is presented. This allows us to study an issue in depth and relative isolation. Each chapter concludes with a discussion of the way in which the concept is incorporated into some well-known languages. This permits a reasonably complete coverage of language issues. A Study of Human Aspects Throughout the book, we view a programming language as a notation. The user of a notation must make a mental investment in learning before gaining any advantage. If the investment is greater than the return in mental leverage for the production of correct programs, the language fails. The mental leverage results from an increased ability by the programmer to master the details of a computation. For the investment to be low, there must be an inherent simplicity in the language with uniformly applied rules and few special cases. vi Preface For good mental leverage, the language must provide powerful abstraction tools that allow the user to subdue the clamor of complexity and master a large program. It is essential to remember that the most important function of a programming language is to communicate the algorithm to other programmers. It is constructed for the convenience of humans, not of machines. Basis on Existing Languages The reader is expected to have experience with one (perhaps more) high-level languages. The concepts discussed in this book are drawn mainly from Ada®, Algol 60, Algol 68, I Cobol, Fortran, Lisp, Pascal, and PLiI. Between these languages, almost any programming language principle, good or bad, can be found. Other languages referenced include APL, Basic, Bliss, Modula-2, Simula 67, and Smalltalk®.2 Obviously, there can be no attempt to present a description of all these languages. However, the reader, even if unfamiliar with these languages, should have no difficulty in understanding this text. The chapters that describe particular language features each contain a section-"Where to Look"-that describes the realization of the feature in one or two real languages. Reference material for all languages mentioned is cited in the bibliography under the name of the language. Suggestions for further reading are contained at the end of each chapter. Levels of Understanding Although each chapter in this book is self-contained, we treat programming languages in four successively deeper levels. The first level, Chapters I and 2, introduces the area of programming language, discusses a number of broad issues, such as the formal de scription of languages and compilers, and generally sets the landscape for the remainder of the book. The second level, Chapters 3 through 8, discusses six dominant features of most contemporary languages: assignment, control structures, data types, input/output. pro cedures, and nesting. The third level. Chapters 9 through 13, treats more advanced topics. data definition. dynamically varying data structures, applicative languages, exception handling. concUlTent processing, and modularization. The last chapter, Chapter 14, presents some views on the complexity of programming languages. The implementation of languages is not discussed specifically. However, where ap propriate, the method of implementation is described. For example, in the chapter on block structure, the dynamic allocation of variables is discussed. The reader is thus able to see how language design, translation processes, and execution environment interact. Acknowledgements One of the first uses of mini-languages as a vehicle for describing language principles was in Ledgard's paper, "Ten Mini-Languages: A Study for Topical Issues in Programming I Ada is a Registered Trademark of the Ada Joint Program Office-U.S. Government. Smalltalk is a Trademark of the Xerox Corporation. Preface vii Languages" [Ledgard 1971]. The book has gained much of its breadth from the work done by Ledgard on the design of Ada. The syntax of many of the mini-languages has also benefitted from this work. This work is derived from a longer text edition published by Science Research As sociates (S.R.A.) of Chicago. Michael Marcotty Henry Ledgard Contents Preface ...................................................................... v Acknowledgements ......................................................... xiii 1. Introduction ......................................................... . 1.1 The Building of the Tower of Babel ................................... I 1.2 What Is a Programming Language? .................................... 5 1.3 Why Study Programming Languages? ................................. 10 1.4 What Should We Look for in a Language? ............................. 10 1.5 Language Design Issues .............................................. 12 1.6 The Study of Programming Languages ................................. 15 Further Reading .......................................................... 17 2. Elements of a Programming Language ................................. 19 2.1 Informal Description of Mini-language Core ........................... 20 2.2 Informal Semantics of Mini-language Core ............................. 24 2.3 Language Design and the Programming Process ......................... 30 2.4 The Definition of Programming Languages... .. ...... .. .. ...... .... .. .. 34 2.5 The Formal Description of Syntax ..................................... 40 2.6 Other Context-free Syntax Definitions ................................. 47 Further Reading .......................................................... 5 1 3. Names, Locations, and Values ......................................... 53 3.1 Mini-language Ref ................................................... 54 3.2 Declarations ......................................................... 60 3.3 Assignment .......................................................... 63 3.4 Statements as Expressions ............................................ 67 3.5 Where to Look: Algol 68, Smalltalk ............................... 69 Further Reading .......................................................... 71 x Contents 4. Control Structures .................................................... 73 4. I Mini-language Control ............................................... 73 4.2 Basic Control Structures and Flowgraphs .............................. 79 4.3 Theorems on Control Structures ....................................... 87 4.4 Other One-in, One-out Control Structures .............................. 91 4.5 The Goto Statement and Label Values ................................. 95 4.6 Conclusions ......................................................... 97 4.7 Where to Look: Pasoal, Ada ........................................ 104 Further Reading 106 s. Data Types ........................................................... 109 5. I Mini-language Type.................................................. 109 5.2 The Meaning of Type ................................................ 114 5.3 Primitive Types ...................................................... 116 5.4 Array Types ......................................................... 123 5.5 Record Types ........................................................ 126 5.6 Type Checking ...................................................... 132 5.7 Where to Look: Fortran, PLII ...................................... 133 Further Reading .......................................................... 134 6. Input and Output ..................................................... 137 6.1 Mini-language Format ................................................ 138 6.2 Varieties of Input-output Specifications ................................ 142 6.3 Communication with the Outside World ............................... 154 6.4 Where to Look: Fortran, Cobol, Ada ............................... 157 Further Reading .......................................................... 159 7. Procedures and Parameters 161 7. I Mini-language Procedures 162 7.2 Procedures as Abstractions 170 7.3 Arguments and Parameters ............................................ 172 7.4 Value-returning Procedures ........................................... 178 7.5 Coroutines .......................................................... 179 7.6 Where to Look: PLII, Ada ........................................... 183 Further Reading .......................................................... 184 8. Nesting and Scope ..................................................... 187 8.1 Mini-language Scope ................................................. 187 8.2 The Idea of Scope ................................................... 194 8.3 Storage Allocation ................................................... 198 8.4 Dynamic Scope ...................................................... 204 8.5 Where to Look: Pascal, PLlI, APL .................................. 206 Further Reading .......................................................... 209 9. Definition of New Data Types ......................................... 21 I 9. I Mini-language Typedef ............................................... 212 9.2 Type Definitions ..................................................... 221 9.3 Definition of Structured Types ........................................ 224 9.4 User Defined Operators .............................................. 228 9.5 Where to Look: Pascal, Algol68 ................................... 230 Further Reading .......................................................... 233 Contents xi 10. Dynamically Varying Structures ....................................... 235 10.1 Mini-language Structures ............................................ 235 10.2 Dynamically Varying Data Structures ................................. 243 10.3 Pointers ............................................................ 245 10.4 Design Criteria for Data Structures ................................... 246 10.5 Dynamic Allocation of Storage ...................................... 249 10.6 Where to Look: PLII, Pascal, Ada ................................. 251 Further Reading .......................................................... 253 11. Exception Handling ................................................... 255 11.1 Mini-language Exceptions ........................ , . . . . . . . . . . . . . . . . .. 256 11.2 Exceptions ......................................................... 260 11.3 Issues in Exception Handling ........................................ 262 11.4 Where to Look: PL/I, Ada ......................................... 266 Further Reading .......................................................... 269 12. Parallel Processing .................................................... 271 12.1 Mini-language Parallel .............................................. 272 12.2 A First Solution to the Decoding Problem ............................ 277 12.3 Putting a Buffer in a Task ........................................... 279 12.4 Interrupting ........................................................ 281 12.5 Issues in Parallel Processing ......................................... 284 12.6 Where to Look: Concurrent Pascal, Modula-2, Ada .............. 289 Further Reading .......................................................... 291 13. Separately Compiled Modules ......................................... 293 13.1 Mini-language Modules ............................................. 295 13.2 Packaging Subprograms ............................................. 299 13.3 Abstract Data Types ................................................ 303 13.4 Where to Look: PLlI, Modula 2, Ada, Smalltalk ................. 305 Further Reading .......................................................... 31 I 14. The Swamp of Complexity ............................................ 313 14.1 The Forms of Complexity ........................................... 313 14.2 Escaping from the Swamp ........................................... 319 References 323 Index 347