1.5 Programming Environments. 24 ... 2.1 Specifying Syntax: Regular Expressions and Context-Free Grammars. 42 ... 2.4.3 Grammar and Language Classes.
Programming Language Pragmatics THIRD EDITION
Michael L. Scott Department of Computer Science University of Rochester
^ШШШШШ ,¥'-*i» ЩЛ< ^ ' m H jfce§f$fa
AMSTERDAM • BOSTON • HEIDELBERG • LONDON
ELSEVIER
Morgan Kaufmann Publishers is an imprint of Elsevier
NEW YORK «OXFORD «PARIS »SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
^ k ^ B ^ ^ ^ И
V\A® к.^
MORGAN KAUFMANN
PUBLISHERS
Contents
Foreword Preface
FOUNDATIONS 1
Introduction I. I The Art of Language Design
xxiii
з 5 7
1.2 The Programming Language Spectrum
10
1.3 Why Study Programming Languages?
14
1.4 Compilation and Interpretation
16
1.5 Programming Environments
24
1.6 An Overview of Compilation
25
1.6.1 Lexical and Syntax Analysis 1.6.2 Semantic Analysis and Intermediate Code Generation 1.6.3 Target Code Generation
27 29 33
1.6.4 Code Improvement
33
1.7 Summary and Concluding Remarks
35
1.8 Exercises
36
1.9 Explorations
37
1.10 Bibliographic Notes
2
xxi
Programming Language Syntax 2.1 Specifying Syntax: Regular Expressions and Context-Free Grammars 2.1.1 Tokens and Regular Expressions 2.1.2 Context-Free Grammars 2.1.3 Derivations and Parse Trees
3.3 Scope Rules 3.3.1 Static Scoping 3.3.2 Nested Subroutines 3.3.3 Declaration Order 3.3.4 Modules 3.3.5 Module Types and Classes 3.3.6 Dynamic Scoping 3.4 Implementing Scope 3.4.1 Symbol Tables 3.4.2 Association Lists and Central ReferenceTabl 3.5 The Meaning of Names within a Scope 3.5.1 Aliases
3.5.2 Overloading 3.5.3 Polymorphism and Related Concepts 3.6 The Binding of Referencing Environments 3.6.1 Subroutine Closures 3.6.2 Fi rst-Class Values and Unlimited Extent 3.6.3 Object Closures 3.7 Macro Expansion 3.8 Separate Compilation 3.8.1 Separate Compilation in С 3.8.2 Packages and Automatic Header Inference 3.8.3 Module Hierarchies 3.9 Summary and Concluding Remarks 3.10 Exercises 3.1 I Explorations 3.12 Bibliographic Notes
4
Semantic Analysis 4.1 The Role of the Semantic Analyzer 4.2 Attribute Grammars 4.3 Evaluating Attributes 4.4 Action Routines 4.5 Space Management for Attributes 4.5.1 Bottom-Up Evaluation 4.5.2 Тор-Down Evaluation 4.6 Decorating a Syntax Tree 4.7 Summary and Concluding Remarks 4.8 Exercises 4.9 Explorations 4.10 Bibliographic Notes
5 Target Machine Architecture 5.1 The Memory Hierarchy 5.2 Data Representation 5.2.1 Integer Arithmetic 5.2.2 Floating-Point Arithmetic
5.3 Instruction Set Architecture 5.3.1 Addressing Modes 5.3.2 Conditions and Branches 5.4 Architecture and Implementation 5.4.1 Microprogramming 5.4.2 Microprocessors 5.4.3 RISC 5.4.4 Multithreading and Multicore 5.4.5 Two Example Architectures: The x86 and MIPS 5.5 Compiling for Modern Processors 5.5.1 Keeping the Pipeline Full 5.5.2 Register Allocation 5.6 Summary and Concluding Remarks 5.7 Exercises 5.8 Explorations 5.9 Bibliographic Notes
si 6
CORE ISSUES IN LANGUAGE DESIGN Control Flow 6.1 Expression Evaluation . I Precedence and Associativity .2 Assignments .3 Initialization .4 Ordering within Expressions .5 Short-Circuit Evaluation 6.2 Structured and Unstructured Flow 6.2.1 Structured Alternatives to goto 6.2.2 Continuations 6.3 Sequencing 6.4 Selection 6.4.1 Short-Circuited Conditions 6.4.2 Case/Switch Statements 6.5 Iteration 6.5.1 Enumeration-Controlled Loops 6.5.2 Combination Loops
6.5.3 Iterators 6.5.4 Generators in Icon 6.5.5 Logically Controlled Loops 6.6 Recursion 6.6.1 Iteration and Recursion 6.6.2 Applicative- and Normal-Order Evaluation 6.7 Nondeterminacy 6.8 Summary and Concluding Remarks 6.9 Exercises 6.10 Explorations 6.1 I Bibliographic Notes
Data Types 7.1 Type Systems 7.1.1 Type Checking 7.1.2 Polymorphism 7.1.3 The Meaning of "Type 7.1.4 Classification of Types 7.1.5 Orthogonality 7.2 Type 7.2.1 7.2.2 7.2.3 7.2.4
Checking Type Equivalence Type Compatibility Type Inference The ML Type System
7.3 Records (Structures) and Variants (Unions) 7.3.1 Syntax and Operations 7.3.2 Memory Layout and Its Impact 7.3.3 With Statements 7.3.4 Variant Records (Unions) 7.4 Arrays 7.4.1 Syntax and Operations 7.4.2 Dimensions, Bounds, and Allocation 7.4.3 Memory Layout 7.5 Strings 7.6 Sets 7.7 Pointers and Recursive Types 7.7.1 Syntax and Operations
xiv
Contents
7.7.2 Dangling References
149 •• 356
357
7.7.3 Garbage Collection
364
7.8 Lists 7.9 Files and Input/Output 7.9.1 Interactive I/O 7.9.2 File-Based I/O 7.9.3 Text I/O
153 •• 367
153 154 156 368
7.10 Equality Testing and Assignment
8
7.1 I Summary and Concluding Remarks
371
7.12 Exercises
373
7.13 Explorations
379
7.14 Bibliographic Notes
380
Subroutines and Control Abstraction
383
8.1 Review of Stack Layout 8.2 Calling Sequences 8.2.1 Displays 8.2.2 Case Studies: С on the MIPS; Pascal on the x86 8.2.3 Register Windows 8.2.4 In-Line Expansion 8.3 Parameter Passing 8.3.1 Parameter Modes 8.3.2 Call-by-Name 8.3.3 Special-Purpose Parameters 8.3.4 Function Returns 8.4 Generic Subroutines and Modules 8.4.1 Implementation Options 8.4.2 Generic Parameter Constraints 8.4.3 Implicit Instantiation 8.4.4 Generics in C++, Java, and C#
11.2 Prolog I 1.2.1 Resolution and Unification I 1.2.2 Lists
547 549 550
Contents
I I I I I
1.2.3 1.2.4 1.2.5 1.2.6 1.2.7
Arithmetic Search/Execution Order Extended Example: Tic-Tac-Toe Imperative Control Flow Database Manipulation
I 1.3 Theoretical Foundations I 1.3.1 Clausal Form 11.3.2 Limitations 11.3.3 Skolemization 11.4 Logic Programming in Perspective I 1.4.1 Parts of Logic Not Covered I 1.4.2 Execution Order 11.4.3 Negation and the "Closed World" Assumption
12.1 Background and Motivation 12.1.1 The Case for Multithreaded Programs 12.1.2 Multiprocessor Architecture
576 579 581
12.2 Concurrent Programming Fundamentals 12.2.1 Communication and Synchronization 12.2.2 Languages and Libraries 12.2.3 Thread Creation Syntax 12.2.4 Implementation of Threads
13.1 What Is a Scripting Language? 13.1.1 Common Characteristics
650 652
13.2 Problem Domains 13.2.1 Shell (Command) Languages 13.2.2 Text Processing and Report Generation I 3.2.3 Mathematics and Statistics I 3.2.4 "Glue" Languages and General-Purpose Scripting 13.2.5 Extension Languages
655 655 663 667 668 676
13.3 Scripting the World Wide Web 13.3.1 CGI Scripts 13.3.2 Embedded Server-Side Scripts 13.3.3 Client-Side Scripts 13.3.4 Java Applets 13.3.5 XSLT
15.1 Virtual Machines 15.1.1 The Java Virtual Machine 15.1.2 The Common Language Infrastructure
764 766 775
15.2 Late Binding of Machine Code 15.2.1 Just-in-Time and Dynamic Compilation 15.2.2 Binary Translation
784 785 791
XX
Contents
15.2.3 Binary Rewriting 15.2.4 Mobile Code and Sandboxing 15.3 Inspection/Introspection 15.3.1 Reflection 15.3.2 Symbolic Debugging 15.3.3 Performance Analysis 15.4 Summary and Concluding Remarks 15.5 Exercises 15.6 Explorations 15.7 Bibliographic Notes
16 Code Improvement 16.1 Phases of Code Improvement 16.2 Peephole Optimization 16.3 Redundancy Elimination in Basic Blocks 16.3.1 A Running Example 16.3.2 Value Numbering 16.4 Global Redundancy and Data Flow Analysis 16.4.1 SSA Form and Global Value Numbering 16.4.2 Global Common Subexpression Elimination 16.5 Loop Improvement I 16.5.1 Loop Invariants 16.5.2 Induction Variables 16.6 Instruction Scheduling 16.7 Loop Improvement II 16.7.1 Loop Unrolling and Software Pipelining 16.7.2 Loop Reordering 16.8 Register Allocation 16.9 Summary and Concluding Remarks 16.10 Bibliographic Notes