Sie sind auf Seite 1von 51

Chapter 1

Preliminaries
Our texts

Concepts of Programming Languages


(10th Edition) January 16, 2012
by Robert W. Sebesta
Chapter 1 Topics

Motivation
Programming Domains
Language Evaluation Criteria
Influences on Language Design
Language Categories
Language Design Trade-Offs
Implementation Methods
Programming Environments

3
Where we are?

4
Motivation: Why Study
Programming Languages?
Increased ability to express ideas
Improved background for choosing appropriate
languages
C vs. Modula-3 vs. C++ for systems
programming
Fortran vs. APL vs. Ada for numerical
computations
Ada vs. Modula-2 for embedded systems

Common Lisp vs. Scheme vs. ML for symbolic


data manipulation
Java vs. C/CORBA for networked PC programs 5
Motivation: Why Study
Programming Languages?
Greater ability to learn new languages
concepts have even more similarity; if you

think in terms of iteration, recursion,


abstraction (for example), you will find it easier
to assimilate the syntax and semantic details of
a new language than if you try to pick it up in a
vacuum. Think of an analogy to human
languages: good grasp of grammar makes it
easier to pick up new languages (at least Indo-
European). 6
Motivation: Why Study
Programming Languages?
Understand significance of implementation
Ability to design new languages
Overall advancement of computing

7
Programming Domains
Scientific applications
Large numbers of floating point computations; use of arrays

Fortran

Business applications
Produce reports, use decimal numbers and characters

COBOL

Artificial intelligence
Symbols rather than numbers manipulated; use of linked lists

LISP

Systems programming
Need efficiency because of continuous use

Web Software
Eclectic collection of languages: markup (e.g., XHTML), scripting
(e.g., PHP), general-purpose (e.g., Java)
Special-purpose languages 8
Language Evaluation Criteria
Readability
The most important criterium
Factors:
Overall simplicity
Too many features is bad
Multiplicity of features is bad
Orthogonality
Makes the language easy to learn and read
Meaning is context independent
A relatively small set of primitive constructs can be
combined in a relatively small number of ways
Every possible combination is legal
Lack of orthogonality leads to exceptions to rules 9
Language Evaluation Criteria

Readability factors (continued)


Control statements
Defining data types and structures

Syntax considerations
Identifier forms
Special words
Form and meaning

10
Language Evaluation Criteria
Writability
Factors:
Simplicity and orthogonality
Support for abstraction

Expressivity

Reliability
Factors:
Type checking
Exception handling

Aliasing

Readability and writability 11


12
Language Evaluation Criteria
Cost
Categories
Training programmers to use language
Writing programs

Compiling programs

Executing programs

Language implementation system

Reliability

Maintaining programs

Others: portability, generality, well-


definedness 13
Influences on Language Design

Computer Architecture
Languages are developed around the prevalent computer
architecture, known as the von Neumann architecture
Programming Methodologies
New software development methodologies (e.g., object-
oriented software development) led to new programming
paradigms and by extension, new programming
languages

14
Influences on Language Design

Computer architecture: Von Neumann


We use imperative languages, at least in part,
because we use von Neumann machines
Data and programs stored in same memory
Memory is separate from CPU
Instructions and data are piped from memory to CPU
Basis for imperative languages
Variables model memory cells
Assignment statements model piping
Iteration is efficient

15
Von Neumann Architecture

16
Influences on Language Design

Programming methodologies
1950s and early 1960s: Simple applications; worry about
machine efficiency
Late 1960s: People efficiency became important;
readability, better control structures
Structured programming
Top-down design and step-wise refinement
Late 1970s: Process-oriented to data-oriented
data abstraction
Middle 1980s: Object-oriented programming

17
Language Categories

Imperative
Central features are variables, assignment
statements, and iteration
C, Pascal
Functional
Main means of making computations is by
applying functions to given parameters
LISP, Scheme

18
Language Categories

Logic
Rule-based
Rules are specified in no special order
Prolog
Object-oriented
Encapsulate data objects with processing
Inheritance and dynamic type binding
Grew out of imperative languages
C++, Java

19
Language Design Trade-Offs

Reliability vs. cost of execution


Readability vs. writability
Flexibility vs. safety

20
Layered View of Computer

21
Compilation
Translate high-level program (source language) into
machine code (machine language)
Slow translation, fast execution
Compilation process has several phases:
lexical analysis: converts characters in the source program
into lexical units
syntax analysis: transforms lexical units into parse trees
which represent the syntactic structure of program
Semantics analysis: generate intermediate code
code generation: machine code is generated
22
Compilation
Process

23
Additional Compilation
Terminologies

Load module (executable image): the user


and system code together
Linking and loading: the process of
collecting system program and linking them
to user program

24
Preprocessors
Preprocessor macros (instructions) are
commonly used to specify that code from
another file is to be included
A preprocessor processes a program
immediately before the program is compiled
to expand embedded preprocessor macros
A well-known example: C preprocessor
expands #include, #define, and similar
macros
25
Preprocessors

Removes comments and white space


Groups characters into tokens (keywords,
identifiers, numbers, symbols)
Expands abbreviations in the style of a macro
assembler
Identifies higher-level syntactic structures
(loops, subroutines)

26
Preprocessor VS Compiler

Compilation entails semantic understanding of what is


being processed; pre-processing does not
A pre-processor will often let errors through. A
compiler hides further steps; a pre-processor does not.
Example: #define T(x) printf(%d,x);
What if T(s) ?
#define F( x ,y) x*y+x
What happen for F(2+3,4) ?

27
Pure Interpretation
No translation
Easier implementation of programs (run-time
errors can easily and immediately displayed)
Slower execution (10 to 100 times slower than
compiled programs)
Often requires more space
Becoming rare on high-level languages
Significant comeback with some Web
scripting languages (e.g., JavaScript)
28
Pure Interpretation

29
Hybrid Implementation
Systems
A compromise between compilers and pure
interpreters
A high-level language program is translated to an
intermediate language that allows easy interpretation
Faster than pure interpretation
Examples
Perl programs are partially compiled to detect errors before
interpretation
Initial implementations of Java were hybrid; the intermediate form,
byte code, provides portability to any machine that has a byte code
interpreter and a run-time system (together, these are called Java
Virtual Machine)

30
Hybrid Implementation Process

31
Compilation vs. Interpretation

Interpretation:
Greater flexibility
Better diagnostics (error messages)

Compilation
Better performance

Compilation vs. interpretation


not opposites
not a clear-cut distinction

32
Compilation vs. Interpretation

Note that compilation does NOT have to


produce machine language for some sort of
hardware
Compilation is translation from one language
into another, with full analysis of the meaning
of the input

33
Compilation vs. Interpretation

Implementation strategies:
Library of Routines and Linking
Compiler uses a linker program to merge the appropriate
library of subroutines (e.g., math functions such as sin,
cos, log, etc.) into the final program:

34
Compilation vs. Interpretation

Implementation strategies:
Post-compilation Assembly
Facilitates debugging (assembly language easier for
people to read)
Isolates the compiler from changes in the format of
machine language files (only assembler must be
changed, is shared by many compilers)

35
Compilation vs. Interpretation

Implementation strategies:
The C Preprocessor (conditional compilation)
Preprocessor deletes portions of code, which allows
several versions of a program to be built from the
same source

Eg. In C, #define, #ifdef,


36
Compilation vs. Interpretation

Implementation strategies:
Source-to-Source Translation (C++)
C++ implementations based on the early AT&T compiler
generated an intermediate program in C, instead of an assembly
language:

37
Compilation vs. Interpretation

Implementation strategies:
Bootstrapping

38
Compilation vs. Interpretation

Implementation strategies:
Compilation of Interpreted Languages
The compiler generates code that makes assumptions
about decisions that wont be finalized until runtime.
If these assumptions are valid, the code runs very fast.
If not, a dynamic check will revert to the interpreter.

Hybrid system

39
Compilation vs. Interpretation

Implementation strategies:
Dynamic and Just-in-Time Compilation
In some cases a programming system may deliberately
delay compilation until the last possible moment.
Lisp or Prolog invoke the compiler on the fly, to translate
newly created source into machine language, or to optimize the
code for a particular input set.
The Java language definition defines a machine-independent
intermediate form known as byte code. Byte code is the
standard format for distribution of Java programs.
The main C# compiler produces .NET Common Intermediate
Language (CIL), which is then translated into machine code
immediately prior to execution. 40
Compilation vs. Interpretation

Compilers exist for some interpreted languages, but


they aren't pure:
selective compilation of compilable pieces and extra-
sophisticated pre-processing of remaining source.
Interpretation of parts of code, at least, is still necessary for
reasons above.
Unconventional compilers
text formatters
silicon compilers
query language processors

41
An Overview of Compilation

Lexical and Syntax Analysis


GCD Program (Pascal)

42
An Overview of Compilation

Lexical and Syntax Analysis


GCD Program Tokens
Scanning (lexical analysis) and parsing recognize the
structure of the program, groups characters into
tokens, the smallest meaningful units of the program

43
An Overview of Compilation

Lexical and Syntax Analysis


Context-Free Grammar and Parsing
Parsing organizes tokens into a parse tree that
represents higher-level constructs in terms of their
constituents
Potentially recursive rules known as context-free
grammar define the ways in which these constituents
combine

44
An Overview of Compilation

Context-Free Grammar and Parsing


Example (Pascal program)

45
An Overview of Compilation

Context-Free Grammar and Parsing


GCD Program Parse Tree

Next slide

46
An Overview of Compilation

Context-Free Grammar and Parsing


GCD Program Parse Tree (continued)

47
An Overview of Compilation

Syntax Tree
GCD Program Parse Tree

48
Programming Environments
The collection of tools used in software
development
UNIX
An older operating system and tool collection
Borland JBuilder
An integrated development environment for Java
Microsoft Visual Studio.NET
A large, complex visual environment
Used to program in C#, Visual BASIC.NET, Jscript, J#,
or C++
Eclipse, Dev C, NetBean, XCode,
49
Genealogy of Common
Languages

50
51

Das könnte Ihnen auch gefallen