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 87 86 85 84 83 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 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 vB Preface 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