Skip to document

Pl12sols - Solution manual

Solution manual
Academic year: 2018/2019
Uploaded by:
2Uploads
137upvotes

Comments

Please sign in or register to post comments.
  • Student
    thank you
  • MH
    very helpful for concept of programming language.

Preview text

Instructor’s Solutions Manual

to

Concepts of Programming Languages

Twelfth Edition R. Sebesta

Preface

Changes for the Twelfth Edition

he goals, overall structure, and approach of this twelfth edition of Concepts of Programming Languages remain the same as those of the eleven earlier editions. The principal goals are to introduce the fundamental constructs of contemporary programming languages and to provide the reader with the tools necessary for the critical evaluation of existing and future programming languages. A secondary goal is to prepare the reader for the study of compiler design, by providing an in-depth discussion of programming language structures, presenting a formal method of describing syntax, and introducing approaches to lexical and syntactic analysis.

T

The twelfth edition evolved from the eleventh through several different kinds of changes. To maintain the currency of the material, nearly all of the discussion of some programming languages, specifically Lua and Objective-C, has been removed. Material on the newer language, Swift, was added to several chapters. In addition, a new section on optional types was added to Chapter 6. Material was added to Section 8.3 to describe iterators in Python. In numerous places in the manuscript small changes were made to correct and/or clarify the discussion.

The Vision

This book describes the fundamental concepts of programming languages by discussing the design issues of the various language constructs, examining the design choices for these constructs in some of the most common languages, and critically comparing design alternatives. Any serious study of programming languages requires an examination of some related topics, among which are formal methods of describing the syntax and semantics of programming languages, which are covered in Chapter 3. Also, implementation techniques for various language constructs must be considered: Lexical and syntax analysis are discussed in Chapter 4, and implementation of

discussion of language features that support object-oriented programming (inheritance and dynamic method binding), Chapter 13 discusses concurrent program units, and Chapter 14 is about exception handling, along with a brief discussion of event handling. Chapters 15 and 16 describe two of the most important alternative programming paradigms: functional programming and logic programming. However, some of the data structures and control constructs of functional programming languages are discussed in Chapters 6 and 8. Chapter 15 presents an introduction to Scheme, including descriptions of some of its primitive functions, special forms, and functional forms, as well as some examples of simple functions written in Scheme. Brief introductions to ML, Haskell, and F# are given to illustrate some different directions in functional language design. Chapter 16 introduces logic programming and the logic programming language, Prolog.

To the Instructor

Chapters 1 and 3 are typically covered in detail, and though students find it interesting and beneficial reading, Chapter 2 receives little lecture time due to the lack of hard technical content. Because no material in subsequent chapters depends on Chapter 2, it can be skipped entirely. If a course in compiler design is required, Chapter 4 is not covered. Chapters 5 through 9 should be relatively easy for students with extensive programming experience in C++, Java, or C#. Chapters 10 through 14 are more challenging and require more detailed lectures. Chapters 15 and 16 are entirely new to most students at the junior level. Ideally, language processors for Scheme and Prolog should be available for students required to learn the material in these chapters. Sufficient material is included to allow students to dabble with some simple programs. Undergraduate courses will probably not be able to cover all of the material in the last two chapters. Graduate courses, however, should be able to completely discuss the material in those chapters by skipping over some parts of the early chapters on imperative languages.

Supplemental Materials

The following supplements are available to all readers of this book at pearsonhighered/cs-resources/. - A set of lecture note slides. PowerPoint slides are available for each chapter in the book. - All of the figures from the book. A companion Web site to the book is available at pearson/cs- resources/. This site contains mini-manuals (approximately 100-page tutorials) on a handful of languages. Solutions to many of the problem sets are available to qualified instructors in our Instructor Resource Center at pearson. Please contact your school’s Pearson Education representative or visit pearson to register.

Language Processor Availability Processors for and information about some of the programming languages discussed in this book can be found at the following Web sites:

C, C++, Fortran, and Ada gcc.gnu

C# and F# microsoft

Java java.sun

Haskell haskell

Scheme plt-scheme/software/drscheme

Perl perl

Python python

Ruby ruby-lang

JavaScript is included in virtually all browsers; PHP is included in virtually all Web servers. All this information is also included on the companion Web site.

Matthew Michael Burke

Michael Prentice SUNY Buffalo Nancy Tinkham Rowan University Neelam Soundarajan Ohio State University

Nigel Gwee Southern University–Baton Rouge

Pamela Cutter Kalamazoo College

Paul M. Jackowitz University of Scranton

Paul Tymann Rochester Institute of Technology

Richard M. Osborne University of Colorado–Denver

Richard Min University of Texas at Dallas

Robert McCloskey University of Scranton

Ryan Stansifer Florida Institute of Technology

Salih Yurttas Texas A&M University

Saverio Perugini University of Dayton

Serita Nelesen Calvin College

Simon H. Lin California State University–Northridge

Stephen Edwards Virginia Tech Stuart C. Shapiro SUNY Buffalo Sumanth Yenduri University of Southern Mississippi

Teresa Cole Boise State University

Thomas Turner University of Central Oklahoma

Tim R. Norton University of Colorado–Colorado Springs

Timothy Henry University of Rhode Island

Walter Pharr College of Charleston

Xiangyan Zeng Fort Valley State University

Numerous other people provided input for the previous editions of Concepts of Programming Languages at various stages of its development. All of their comments were useful and greatly appreciated. In alphabetical order, they are:

Vicki Allan, Henry Bauer, Carter Bays, Manuel E. Bermudez, Peter Brouwer, Margaret Burnett, Paosheng Chang, Liang Cheng, John Crenshaw, Charles Dana, Barbara Ann Griem, Mary Lou Haag, John V. Harrison, Eileen Head, Ralph C. Hilzer, Eric Joanis, Leon Jololian, Hikyoo Koh, Jiang B. Liu, Meiliu Lu, Jon Mauney, Robert McCoard, Dennis L. Mumaugh, Michael G. Murphy, Andrew Oldroyd, Young Park, Rebecca Parsons, Steve J. Phelps, Jeffery Popyack, Steven Rapkin, Hamilton Richard, Tom Sager, Raghvinder Sangwan, Joseph Schell, Sibylle Schupp, Mary Louise Soffa, Neelam Soundarajan, Ryan Stansifer, Steve Stevenson, Virginia Teller, Yang Wang, John M. Weiss, Franck Xia, and Salih Yurnas. Matt Goldstein, Portfolio Management Specialist; Meghan Jacoby, Portfolio Management Assistant; Managing Content Producer, Scott Disanno; and Prathiba Rajagopal, all deserve my gratitude for their efforts to produce the twelfth edition both quickly and carefully.

About the Author

Robert Sebesta is an Associate Professor Emeritus in the Computer Science Department at the University of Colorado–Colorado Springs. Professor Sebesta received a BS in applied mathematics from the University of Colorado in Boulder and MS and PhD degrees in computer science from Pennsylvania State University. He taught computer science for more than 40 years..

Summary • Bibliographic Notes • Review Questions • Problem Set •Programming

  • 2 Two Early Dynamic Languages: APL and SNOBOL.............................
  • 2 The Beginnings of Data Abstraction: SIMULA 67.................................
  • 2 Orthogonal Design: ALGOL 68..............................................................
  • 2 Some Early Descendants of the ALGOLs...............................................
  • 2 Programming Based on Logic: Prolog.....................................................
  • 2 History’s Largest Design Effort: Ada......................................................
  • 2 Object-Oriented Programming: Smalltalk...............................................
  • 2 Combining Imperative and Object-Oriented Features: C++..................
  • 2 An Imperative-Based Object-Oriented Language: Java..........................
  • 2 Scripting Languages.................................................................................
  • 2 The Flagship .NET Language: C#...........................................................
  • 2 Markup-Programming Hybrid Languages.............................................
    • Exercises............................................................................................................
  • Chapter 3 Describing Syntax and Semantics
    • 3 Introduction............................................................................................
    • 3 The General Problem of Describing Syntax..........................................
    • 3 Formal Methods of Describing Syntax..................................................
    • 3 Attribute Grammars............................................................................... - History Note................................................................................................
    • 3 Describing the Meanings of Programs: Dynamic Semantics................. - History Note................................................................................................
      • Summary • Bibliographic Notes • Review Questions • Problem Set.................
  • Chapter 4 Lexical and Syntax Analysis
    • 4 Introduction............................................................................................
    • 4 Lexical Analysis.....................................................................................
    • 4 The Parsing Problem..............................................................................
    • 4 Recursive-Descent Parsing....................................................................
    • 4 Bottom-Up Parsing................................................................................
      • Summary • Review Questions • Problem Set • Programming Exercises...........
  • Chapter 5 Names, Bindings, and Scopes
    • 5 Introduction............................................................................................
    • 5 Names..................................................................................................... - History Note................................................................................................
    • 5 Variables................................................................................................
    • 5 The Concept of Binding.........................................................................
    • 5 Scope......................................................................................................
    • 5 Scope and Lifetime................................................................................
    • 5 Referencing Environments.....................................................................
    • 5 Named Constants...................................................................................
      • Summary • Review Questions • Problem Set •Programming Exercises............
  • Chapter 6 Data Types
    • 6 Introduction............................................................................................
    • 6 Primitive Data Types.............................................................................
    • 6 Character String Types........................................................................... - History Note................................................................................................
    • 6 Enumeration Types................................................................................
    • 6 Array Types............................................................................................ - History Note................................................................................................ - History Note................................................................................................
    • 6 Associative Arrays.................................................................................
  • Chapter 8 Statement-Level Control Structures
    • 8 Introduction............................................................................................
    • 8 Selection Statements..............................................................................
    • 8 Iterative Statements................................................................................
    • 8 Unconditional Branching....................................................................... - History Note................................................................................................
    • 8 Guarded Commands...............................................................................
    • 8 Conclusions............................................................................................
      • Summary • Review Questions • Problem Set • Programming Exercises.............
  • Chapter 9 Subprograms
    • 9 Introduction............................................................................................
    • 9 Fundamentals of Subprograms...............................................................
    • 9 Design Issues for Subprograms..............................................................
    • 9 Local Referencing Environments...........................................................
    • 9 Parameter-Passing Methods................................................................... - History Note................................................................................................ - History Note................................................................................................
    • 9 Parameters That Are Subprograms........................................................ - History Note................................................................................................
    • 9 Calling Subprograms Indirectly.............................................................
    • 9 Design Issues for Functions...................................................................
    • 9 Overloaded Subprograms.......................................................................
    • 9 Generic Subprograms.............................................................................
    • 9 User-Defined Overloaded Operators.....................................................
    • 9 Closures..................................................................................................
    • 9 Coroutines..............................................................................................
      • Summary • Review Questions • Problem Set • Programming Exercises.............
  • Chapter 10 Implementing Subprograms
    • 10 The General Semantics of Calls and Returns.........................................
    • 10 Implementing “Simple” Subprograms...................................................
    • 10 Implementing Subprograms with Stack-Dynamic Local Variables.......
    • 10 Nested Subprograms..............................................................................
    • 10 Blocks.....................................................................................................
    • 10 Implementing Dynamic Scoping...........................................................
      • Summary • Review Questions • Problem Set • Programming Exercises.............
  • Chapter 11 Abstract Data Types and Encapsulation Constructs
    • 11 The Concept of Abstraction...................................................................
    • 11 Introduction to Data Abstraction............................................................
    • 11 Design Issues for Abstract Data Types..................................................
    • 11 Language Examples............................................................................... - Its Ubiquitousness, and Common Criticisms................................................ Interview: BJARNE STROUSTRUP—C++: Its Birth,
    • 11 Parameterized Abstract Data Types.......................................................
    • 11 Encapsulation Constructs.......................................................................
    • 11 Naming Encapsulations..........................................................................
      • Summary • Review Questions • Problem Set • Programming Exercises.............
  • Chapter 12 Support for Object-Oriented Programming
    • 12 Introduction............................................................................................
    • 12 Object-Oriented Programming...............................................................
    • 12 Design Issues for Object-Oriented Languages.......................................
    • 12 Support for Object-Oriented Programming in Specific Languages ......
    • 14 Event Handling in C#.............................................................................
      • Exercises............................................................................................................. Summary • Bibliographic Notes • Review Questions • Problem Set • Programming
  • Chapter 15 Functional Programming Languages
    • 15 Introduction............................................................................................
    • 15 Mathematical Functions.........................................................................
    • 15 Fundamentals of Functional Programming Languages..........................
    • 15 The First Functional Programming Language: Lisp..............................
    • 15 An Introduction to Scheme....................................................................
    • 15 Common Lisp.........................................................................................
    • 15 ML..........................................................................................................
    • 15 Haskell...................................................................................................
    • 15 F#........................................................................................................... - ................................................................................................................ 15 Support for Functional Programming in Primarily Imperative Languages
    • 15 A Comparison of Functional and Imperative Languages......................
      • Exercises............................................................................................................. Summary • Bibliographic Notes • Review Questions • Problem Set • Programming
  • Chapter 16 Logic Programming Languages
    • 16 Introduction............................................................................................
    • 16 A Brief Introduction to Predicate Calculus............................................
    • 16 Predicate Calculus and Proving Theorems............................................
    • 16 An Overview of Logic Programming....................................................
    • 16 The Origins of Prolog............................................................................
    • 16 The Basic Elements of Prolog................................................................
    • 16 Deficiencies of Prolog............................................................................

16 Applications of Logic Programming...................................................... Summary • Bibliographic Notes • Review Questions • Problem Set • Programming

Exercises............................................................................................................

Bibliography......................................................................................... Index.....................................................................................................

Answers to Selected Problems

Chapter 1

Problem Set:

  1. Some arguments for having a single language for all programming domains are: It would dramatically cut the costs of programming training and compiler purchase and maintenance; it would simplify programmer recruiting and justify the development of numerous language dependent software development aids.

  2. Some arguments against having a single language for all programming domains are: The language would necessarily be huge and complex; compilers would be expensive and costly to maintain; the language would probably not be very good for any programming domain, either in compiler efficiency or in the efficiency of the code it generated. More importantly, it would not be easy to use, because regardless of the application area, the language would include many unnecessary and confusing features and constructs (those meant for other application areas). Different users would learn different subsets, making maintenance difficult.

  3. One possibility is wordiness. In some languages, a great deal of text is required for even simple complete programs. For example, COBOL is a very wordy language. In Ada, programs require a lot of duplication of declarations. Wordiness is usually considered a disadvantage, because it slows program creation, takes more file space for the source programs, and can cause programs to be more difficult to read.

  4. The argument for using the right brace to close all compounds is simplicity—a right brace always terminates a compound. The argument against it is that when you see a right brace in a program, the location of its matching left brace is not always obvious, in part because all multiple-statement control constructs end with a right brace.

  5. The reasons why a language would distinguish between uppercase and lowercase in its identifiers are: (1) So that variable identifiers may look different than identifiers that are names for constants, such as the convention of using uppercase for constant names and using lowercase for variable names in C, and (2) so that catenated words as names can have their first letter distinguished, as in TotalWords. (Some think it is better to include a connector, such as underscore.) The primary reason why a language would not

  6. The argument for typeless languages is their great flexibility for the programmer. Literally any storage location can be used to store any type value. This is useful for very low-level languages used for systems programming. The drawback is that type checking is impossible, so that it is entirely the programmer's responsibility to insure that expressions and assignments are correct.

  7. A good deal of restraint must be used in revising programming languages. The greatest danger is that the revision process will continually add new features, so that the language grows more and more complex. Compounding the problem is the reluctance, because of existing software, to remove obsolete features.

  8. One situation in which pure interpretation is acceptable for scripting languages is when the amount of computation is small, for which the processing time will be negligible. Another situation is when the amount of computation is relatively small and it is done in an interactive environment, where the processor is often idle because of the slow speed of human interactions.

  9. New scripting languages may appear more frequently than new compiled languages because they are often smaller and simpler and focused on more narrow applications, which means their libraries need not be nearly as large.

Chapter 3

Instructor's Note:

In the program proof on page 160, there is a statement that may not be clear to all, specifically, (n + 1)* ... * n = 1. The justification of this statement is as follows:

Consider the following expression:

(count + 1) * (count + 2) * ... * n

The former expression states that when count is equal to n, the value of the later expression is 1. Multiply the later expression by the quotient:

(1 * 2 * ... * count) / (1 * 2 * ... * count)

whose value is 1 , to get

(1 * 2 * ... * count * (count + 1) * (count + 2) * ... * n) /

(1 * 2 * ... * count)

The numerator of this expressions is n!. The denominator is count!. If count is equal to n, the value of the quotient is

n! / n!

or 1 , which is what we were trying to show.

Problem Set:

2a. <class_head>  {<modifier>} class <id> [extends class_name]

[implements <interface_name> {, <interface_name>}]

<modifier>  public | abstract | final

2c. <switch_stmt>  switch ( <expr> ) {case <literal> : <stmt_list>

{case <literal> : <stmt_list> } [default : <stmt_list>] }

  1. <assign>  <id> = <expr>

<id>  A | B | C

<expr>  <expr> * <term>

| <term>

<term>  <factor> + <term>

| <factor>

<factor>  ( <expr> )

| <id>

(a) <assign> => <id> = <expr>

=> A = <expr> => A = <id> * <expr> => A = A * <expr> => A = A * ( <expr> ) => A = A * ( <id> + <expr> ) => A = A * ( B + <expr> ) => A = A * ( B + ( <expr> ) ) => A = A * ( B + ( <id> * <expr> ) ) => A = A * ( B + ( C * <expr> ) ) => A = A * ( B + ( C * <id> ) )

Was this document helpful?

Pl12sols - Solution manual

Was this document helpful?
Instructor’s Solutions Manual
to
Concepts of Programming Languages
Twelfth Edition
R.W. Sebesta