We all have witnessed the rapid development of computer science and its applications in many domains, particularly in Web-based computing (Web 2.0), cloud computing, and mobile computing. However, as a science discipline, the fundamentals of computer science, including programming language principles and the classic programming languages, are stable and do not change with the technological innovations. C, C++, Scheme, and Prolog belong to the classic programming languages that were developed many decades ago and are still the most significant programming languages today, both in theory and in practice, which has allowed the second edition of this text that covers these languages to survive and remain relevant for six years. However, the technologies that surround these languages have been changed and improved, and we feel that it is time to update the text. The main changes are on the programming environments that provide hands-on learning experiences of these languages.

In the third edition, chapter 5 is completely rewritten. The previous editions used Arity Prolog. The development environment of Arity Prolog is no longer available. When we teach, we have been using the GNU Prolog programming environment to execute the Arity Prolog programs. Although most examples in the book worked in the GNU Prolog environment, there are certain incompatibility issues. In this edition, we have moved from Arity Prolog to GNU Prolog. We had the choice of using GNU Prolog or SWI-Prolog. As running programs in a Unix environment is a part of the course objectives, we decided for GNU Prolog. However, we have tested all the examples in the book in SWI-Prolog. There are no incompatibility issues; and thus, instructors can choose to use the SWI-Prolog environment when teaching from this book.

In chapter 5, we also discuss the applications of Prolog in the semantic Web (Web 3.0) and its relationship with the currently used Web semantic languages RDF (Resource Description Framework) and OWL (Web Ontology Language). Semantic Web is considered the next generation of Web that allows the Web to be browsed and explored by both humans and computer programs. In the third edition revised print, this chapter is further expanded with the latest computing technologies in cloud computing and big data processing.

Since the publication of the second edition in 2006, the programming environment for chapter 4, on Scheme, has been changed from DrScheme to DrRocket. The change does not affect the examples in the text. They all work in the new DrRocket environment, except certain notational issues. We have updated chapter 4 to match the changes made in DrRocket.

As parallel computing and multithreading are becoming more and more important in software development, this edition adds a new section, section 3.6 parallel computing and multithreading in C++, in chapter 3. A MapReduce example is used as a case study for explaining multithreading in C++. The parallel computing concept is also emphasized in chapter 4, where the eager evaluation and higher functions Map and Reduce are linked to parallel computing.

The programming environments for C/C++ in chapters 2 and 3 are also updated from Visual Studio 2005 to Visual Studio 2010.

Various updates, improvements, and corrections have been made throughout the book in the third edition, in the main text, in appendices, and in homework and programming exercises, thanks to feedback from the instructors who adopted the text and students who used the text in the past six years.

In the second edition, the major change made was the inclusion of programming in Service-Oriented Architecture (SOA). Since the publication of the first edition in 2003, SOA programming has emerged as a new programming paradigm, which has demonstrated its strength to become a dominating programming paradigm. All major computing companies, including HP, IBM, Intel, Microsoft, Oracle, SAP, and Sun Microsystems, have moved into this new paradigm and are using the new paradigm to produce software and even hardware systems. The need for skill in SOA programming increases as the deployment of SOA applications increases. This new SOA paradigm is not only important in the practice of programming, but it also contributes to the concepts and principles of programming theory. In fact, SOA programming applies a higher level of abstraction, which requires fewer technical details for building large software applications. We, the authors of this book, are leading researchers and educators in SOA programming. The inclusion of the new chapter on C# and SOA programming makes the text unique, which allows teaching of the most contemporary programming concepts and practice. The new chapter also gives professors a new component to choose from, which adds flexibility for them to select different contents for different courses. As SOA has developed significantly in the past six years, this chapter is also updated for the third edition; for example, an introduction to cloud computing is included in this edition.

This edition of the text consists of six chapters. A half-semester course (25 to 30 lecture hours) can teach from two to three chapters, and a full semester course can teach four to five chapters of choice from the text. Chapter 3 (C++) is closely related to chapter 2. If chapter 3 is selected as a component of a course, chapter 2, or a part of chapter 2, should be selected as well. Other chapters are relatively independent of each other and can be selected freely to compose a course.

For a half-semester course, sensible course compositions could include: (chapters 1, 2, 3); (chapters 2, 3); (chapters 1, 2, 6); (chapters 2, 3, 6); (chapters 1, 4, 5); and (chapters 4, 5). For a full semester course, sensible course compositions could include: (chapters 1, 2, 3, 4, 5); (chapters 1, 2, 3, 4, 6); (chapters 1, 2, 3, 5, 6); and (chapters 2, 3, 4, 5, 6).

The authors wish to thank particularly our colleagues Calvin Cheng and Ruben Acuna, who taught the parallel sections of CSE 240 and have contributed to the teaching materials added into the third edition.

Please contact the authors for instructional support at “text.cse240@asu.edu” or “yinong@asu.edu”. Available materials include lecture slides in PowerPoint format, sample tests, assignments, and the solutions to the tests and assignments.

 

Yinong Chen and Wei-Tek Tsai

April 2014

 

Table of Contents

Preface (This Edition) xi

Preface (First Edition) xiii

Chapter 1      Basic Principles of Programming Languages. 1

1.1      Introduction. 2

1.1.1     Programming concepts and paradigms  2

1.1.2     Program performance and features of programming languages  3

1.1.3     Development of programming languages  4

1.2      Structures of programming languages. 7

1.2.1     Lexical structure  7

1.2.2     Syntactic structure  8

1.2.3     Contextual structure  8

1.2.4     Semantic structure  8

1.2.5     BNF notation  8

1.2.6     Syntax graph  10

1.3      Data types and type checking. 11

1.3.1     Data types and type equivalence  11

1.3.2     Type checking and type conversion  12

1.3.3     Orthogonality  13

1.4      Program processing and preprocessing. 15

1.4.1     Interpretation and compilation  15

1.4.2     Preprocessing: macro and inlining  16

*1.5   Program development 20

1.5.1     Program development process  20

1.5.2     Program testing  21

1.5.3     Correctness proof  24

1.6      Summary. 26

1.7      Homework and programming exercises. 27

Chapter 2      The Imperative Programming Languages, C/C++. 33

2.1      Getting started with C/C++. 34

2.1.1     Write your first C/C++ program   34

2.1.2     Basic input and output functions  35

2.1.3     Formatted input and output functions  35

2.2      Control structures in C/C++. 37

2.2.1     Operators and the order of evaluation  38

2.2.2     Basic selection structures (if-then-else and the conditional expression) 38

2.2.3     Multiple selection structure (switch) 39

2.2.4     Iteration structures (while, do-while, and for) 42

2.3      Data and basic data types in C/C++. 44

2.3.1     Declaration of variables and functions  44

2.3.2     Scope rule  45

2.3.3     Basic data types  46

2.4      Complex types. 49

2.4.1     Array  49

2.4.2     Pointer 51

2.4.3     String  54

2.4.4     Constants  61

2.4.5     Enumeration type  62

2.5      Compound data types. 65

2.5.1     Structure types  65

2.5.2     Union  66

2.5.3     Array of structures using static memory allocation  67

2.5.4     Linked list using dynamic memory allocation  70

2.5.5     Stack  74

2.5.6     Files, standard input/output files, and file operations  76

2.6      Functions and parameter passing. 84

2.7      Recursive structures. 88

2.7.1     Loop structures versus recursive structures  88

2.7.2     The fantastic-four abstract approach of writing recursive functions  89

2.7.3     Hanoi Towers  90

2.7.4     A simple recursive sorting algorithm   93

2.7.5     Merge sort algorithm   95

2.7.6     Quick sort algorithm   96

2.8      Modular design. 97

2.9      Summary. 99

2.10   Homework, programming exercises, and projects. 101

Chapter 3      The Object-Oriented Programming Language, C++. 121

3.1      A long program example: a queue and a priority queue written in C++. 122

3.2      Class definition and composition. 125

3.2.1     Class definition  125

3.2.2     Scope resolution operator 126

3.2.3     Objects from a class  127

3.2.4     Definition of constructor and destructor 127

3.3      Memory management and garbage collection. 128

3.3.1     Static: global variables and static local variables  129

3.3.2     Runtime stack for local variables  130

3.3.3     Heap: dynamic memory allocation  133

3.3.4     Scope and garbage collection  133

3.4      Inheritance. 138

3.4.1     Class containment and inheritance  138

3.4.2     Inheritance and hierarchy  141

3.4.3     Inheritance and polymorphism   155

3.4.4     Polymorphism and type checking  157

3.4.5     Polymorphism and late binding  158

3.5      Exception Handling. 158

3.6      Parallel computing and multithreading. 161

3.6.1     Basic concepts in parallel computing and multithreading  161

3.6.2     Generic features in C++  162

3.6.3     Case Study: Implementing multithreading in C++  163

3.7      Summary. 167

3.8      Homework, programming exercises, and projects. 169

Chapter 4      The Functional Programming Language, Scheme. 177

4.1      From imperative programming to functional programming. 178

4.2      Prefix notation. 180

4.3      Basic Scheme terminology. 182

4.4      Basic Scheme data types and functions. 185

4.4.1     Number types  185

4.4.2     Boolean  186

4.4.3     Character 187

4.4.4     String  188

4.4.5     Symbol 188

4.4.6     Pair 189

4.4.7     List 190

4.4.8     Application of Quotes  190

4.4.9     Definition of procedure and procedure type  192

4.4.10  Input/output and nonfunctional features  193

*4.5   Lambda-calculus. 195

4.5.1     Lambda-expressions  196

4.5.2     l-procedure and parameter scope  196

4.5.3     Reduction rules  197

4.6      Define your Scheme procedures and macros. 198

4.6.1     Unnamed procedures  198

4.6.2     Named procedures  198

4.6.3     Scopes of variables and procedures  199

4.6.4     Let-form and unnamed procedures  201

4.6.5     Macros  202

4.6.6     Compare and contrast imperative and functional programming paradigms  202

4.7      Recursive procedures. 205

4.8      Define recursive procedures on data types. 207

4.8.1     Number manipulations  207

4.8.2     Character and string manipulations  210

4.8.3     List manipulations  211

4.9      Higher-order functions. 212

4.9.1     Mapping  212

4.9.2     Reduction  214

4.9.3     Filtering  215

4.10   Summary. 217

4.11   Homework, programming exercises, and projects. 219

Chapter 5      The Logic Programming Language, Prolog. 229

5.1      Basic concepts of logic programming in Prolog. 229

5.1.1     Prolog basics  229

5.1.2     Structures of Prolog facts, rules, and goals  231

5.2      The Prolog execution model 233

5.2.1     Unification of a goal 233

5.2.2     Example of searching through a database  235

5.3      Arithmetic operations and database queries. 236

5.3.1     Arithmetic operations and built-in functions  236

5.3.2     Combining database queries with arithmetic operations  238

5.4      Prolog functions and recursive rules. 240

5.4.1     Parameter passing in Prolog  240

5.4.2     Factorial example  240

5.4.3     Fibonacci numbers example  241

5.4.4     Hanoi Towers  241

5.4.5     Graph model and processing  242

5.4.6     Map representation and coloring  244

5.5      List and list manipulation. 245

5.5.1     Definition of pairs and lists  245

5.5.2     Pair simplification rules  246

5.5.3     List membership and operations  247

5.5.4     Knapsack problem   250

5.5.5     Quick sort 251

5.6      Flow control structures. 253

5.6.1     Cut 253

5.6.2     Fail 256

5.6.3     Repeat 257

5.7      Prolog application in semantic Web. 259

5.8      Summary. 260

5.9      Homework, programming exercises, and projects. 263

Chapter 6      Fundamentals of the Service-Oriented Computing Paradigm.. 271

6.1      Introduction to C#. 271

6.1.1     Getting started with C# and Visual Studio  271

6.1.2     Comparison between C++ and C#  273

6.1.3     Namespaces and the using directives  273

6.1.4     The queue example in C#  275

6.1.5     Class and object in C#  276

6.1.6     Parameters: passing by reference with ref & out  279

6.1.7     Base classes and constructors  280

6.1.8     Constructor, destructor, and garbage collection  280

6.1.9     Pointers in C#  281

6.1.10  C# unified type system   282

6.1.11  Further topics in C#  283

6.2      Service-oriented computing paradigm.. 283

6.2.1     Basic concepts and terminologies  284

6.2.2     Web services development 285

6.2.3     Service-oriented system engineering  286

6.2.4     Web services and enabling technologies  287

6.3      *Service providers: programming Web services in C#. 289

6.3.1     Creating a Web service project 290

6.3.2     Writing the service class  290

6.3.3     Launch and access your Web services  291

6.3.4     Automatically generating a WSDL file  294

6.4      Publishing and searching Web services using UDDI 295

6.4.1     UDDI file  295

6.4.2     ebXML  297

6.4.3     Ad hoc registry lists  298

6.5      Building applications using ASP.Net 299

6.5.1     Creating your own Web browser 299

6.5.2     Creating a Windows application project in ASP.Net 300

6.5.3     Developing a Web site application to consume Web services  305

6.6      Cloud computing and big data processing. 308

6.6.1     Cloud computing  308

6.6.2     Big data  311

6.7      Summary. 313

6.8      Homework, programming exercises, and projects. 315

Appendix A   Basic Computer Architectures and Assembly Language Programming. 321

A.1     Basic computer components and computer architectures. 321

A.2     Computer architectures and assembly programming. 323

A.3     Subroutines and local variables on stack. 327

Appendix B   Programming Environments Supporting C, C++, Scheme, and Prolog. 329

B.1     Introduction to Unix commands. 329

B.2     Programming environments supporting C, C++, and C# programming. 332

B.2.1    Getting started with GNU GCC under the Unix operating system   332

B.2.2    Getting started with Visual Studio under the MS Windows operating system   333

B.2.3    Download C, C++, and C# programming development tools and environments  334

B.3     Programming environments supporting Scheme programming. 334

B.3.1    Getting started with DrRocket 334

B.3.2    Download DrRocket programming environment 335

B.4     Programming environments supporting Prolog programming. 335

B.4.1    Getting started with the GNU Prolog environment 335

B.4.2    Getting started with Prolog programming  337

B.4.3    Download Prolog programming development tools  338

Appendix C   ASCII Character Table. 341

Bibliography    345

Index    349