overview. copyright © 2006 the mcgraw-hill companies, inc. chapter 1 overview a good programming...

37
CS 424/524 PROGRAMMING LANGUAGES Overview

Upload: georgiana-gibson

Post on 25-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

CS 424/524PROGRAMMING LANGUAGES

Overview

Copyright © 2006 The McGraw-Hill Companies, Inc.

Chapter 1Overview

A good programming language is a conceptual universe for thinking about programming.

A. Perlis

Programming Languages2nd edition

Tucker and Noonan

Copyright © 2006 The McGraw-Hill Companies, Inc.

1.1 Principles1.2 Paradigms1.3 Special Topics1.4 A Brief History1.5 On Language Design

1.5.1 Design Constraints1.5.2 Outcomes and Goals

1.6 Compilers, Interpreters, and Virtual Machines

Contents

Copyright © 2006 The McGraw-Hill Companies, Inc.

Programming languages have several properties, including◦ Syntax◦ Names & types◦ Semantics

Language designers define these properties

Language users must understand them

1.1 Principles

Copyright © 2006 The McGraw-Hill Companies, Inc.

Syntax describes◦ Legal statements ◦ Legal programs

Syntax defines the form of a correct program – doesn’t say anything about program meaning

Who needs to know PL syntax?◦ Programmers: how to write a correct program◦ Translators: how to recognize a correct program

Syntax

Copyright © 2006 The McGraw-Hill Companies, Inc.

Various kinds of entities in a program have names:variables, functions, parameters, classes, objects, …

Named entities are bound in a running program to:◦ Scope◦ Visibility◦ Type◦ Lifetime

Names

Copyright © 2006 The McGraw-Hill Companies, Inc.

A type is a collection of values and a collection of operations on those values.◦ Simple types: numbers, characters, booleans,

…◦ Structured types: Strings, lists, arrays, hash

tables, …

A language’s type system ◦ Defines legal operations ◦ Defines type errors

Types

Copyright © 2006 The McGraw-Hill Companies, Inc.

A program’s meaning is defined by its semantics.

In studying semantics, we ask questions like:◦ When a program is running, what happens to the

values of the variables?◦ What does each statement mean?◦ What underlying model governs the run-time

behavior of a function call?◦ …

Semantics

Copyright © 2006 The McGraw-Hill Companies, Inc.

A programming paradigm is a pattern or framework for problem-solving

Languages typically identify primarily with one paradigm, but may have aspects of several

Common paradigms can be grouped together according to whether they have imperative characteristics or declarative characteristics.

1.2 Paradigms

Copyright © 2006 The McGraw-Hill Companies, Inc.

Imperative languages:◦ Pure Imperative (von Neumann languages); e.g.,

C, Pascal, early versions of Fortran & Ada, …◦ Object-oriented; e.g., C++, Java, Smalltalk◦ Most of what we’ll study focuses on these

languages Declarative languages

◦ Functional Lisp, Scheme, ML, Haskell, …

◦ Logic Prolog, SQL, …

1.2 Paradigms

Copyright © 2006 The McGraw-Hill Companies, Inc.

Follows the classic von Neumann-Eckert model of computation:◦ Program and data are indistinguishable in

memory◦ Program = a sequence of commands ◦ Programs work by changing values of memory

variables Large programs use procedural abstraction as

a way to organize individual imperative statements.

Pure Imperative Paradigm

Copyright © 2006 The McGraw-Hill Companies, Inc.

The von Neumann-Eckert Model

Copyright © 2006 The McGraw-Hill Companies, Inc.

OO languages share many characteristics with the pure imperative languages

Programs consist of a collection of objects that communicate via “messages” that modify object state◦ Messages are imperative

OO languages are characterized by◦ Encapsulation◦ Inheritance◦ Polymorphism

Object-oriented (OO) Paradigm

Copyright © 2006 The McGraw-Hill Companies, Inc.

Functional programming models a computation as a function that maps inputs to outputs. ◦ Input = domain◦ Output = range

Functional languages are characterized by:◦ Functional composition◦ Recursion

Example functional languages:◦ Lisp, Scheme, ML, Haskell, …

Declarative - Functional Paradigm

Copyright © 2006 The McGraw-Hill Companies, Inc.

Logic programming declares what outcome the program should accomplish, rather than how it should be accomplished.

Based on predicate logicRule-basedWhen studying logic programming we see:

◦ Programs as sets of constraints on a problem◦ Find values that satisfy all constraints

Example logic programming languages: Prolog

Logic Paradigm

Copyright © 2006 The McGraw-Hill Companies, Inc.

Event handling◦ E.g., GUIs, home security systems, monitoring

systems of various kinds Concurrency

◦ e.g., Client-server programs, rendering (in computer graphics)

Correctness◦ Can we prove that a program does what it is

supposed to do under all circumstances?

1.3 Special Topics

Copyright © 2006 The McGraw-Hill Companies, Inc.

How and when did programming languages evolve?

What communities have developed and used them?◦ Artificial Intelligence◦ Computer Science Education◦ Science and Engineering◦ Information Systems◦ Systems and Networks◦ World Wide Web

1.4 A Brief History

Copyright © 2006 The McGraw-Hill Companies, Inc.

Copyright © 2006 The McGraw-Hill Companies, Inc.

1.5.1 – Design Constraints◦ Computer architecture◦ Technical setting (domain of applications)◦ Standards◦ Legacy systems

1.5 On Language Design

Copyright © 2006 The McGraw-Hill Companies, Inc.

Key characteristics:◦ Simplicity, readability and writability◦ Clarity about binding◦ Reliability◦ Support◦ Abstraction◦ Orthogonality◦ Efficient implementation

What Makes A Successful Language?

Copyright © 2006 The McGraw-Hill Companies, Inc.

Early languages focused on efficiency◦ Time◦ Space

Today, programs that are easy to read and easy to write are most likely to be successful

Promoted by simplicity of the language.

Readability & Writability

Copyright © 2006 The McGraw-Hill Companies, Inc.

Easy to learn◦ Don’t include too many features◦ Don’t make it too complex

Simple conceptual model of semantics. Uniformity: Similar syntax => similar

semantics. Lexical and syntactic conventions:

◦ descriptive identifier names, blocking of compound statements

Simplicity

Copyright © 2006 The McGraw-Hill Companies, Inc.

A language element is bound to a property at the time that property is defined for it.

So a binding is the association between an object and a property of that object; the binding time should be clearly understood.

Examples:

◦ variable and type: program writing time or execution time?

◦ variable and value: program writing time and execution time

◦ language operator to machine language instruction: ??

Clarity about Binding

Copyright © 2006 The McGraw-Hill Companies, Inc.

Major binding times◦ Language definition time

◦ Language implementation time

◦ Program writing time

◦ Compile time

◦ Load time

◦ Execution time In general,

◦ Early binding takes place at or before compile-time

◦ Late binding takes place at load time or run time Early v late: efficiency versus flexibility

Major Binding Times

Copyright © 2006 The McGraw-Hill Companies, Inc.

A language is reliable if:◦ Program behavior is the same on different

platforms E.g., early versions of Fortran weren’t

◦ Type errors are detected E.g., C vs Haskell or ML

◦ Good exception-handling facilities E.g., C vs Java

◦ Memory leaks are prevented E.g., C/C++ vs Java

Reliability

Copyright © 2006 The McGraw-Hill Companies, Inc.

Good texts and tutorials Wide community of users Integrated with development

environments (IDEs) Accessible compiler/interpreters (public

domain)

Language Support

Copyright © 2006 The McGraw-Hill Companies, Inc.

Data◦ Programmer-defined types/classes◦ Class libraries

Procedural◦ Programmer-defined functions◦ Standard function libraries

Supports code reuse; simplifies the programming process.

Abstraction in Programming

Copyright © 2006 The McGraw-Hill Companies, Inc.

A language is orthogonal if it is built on a small, mutually independent set of primitive operations.◦ You can use one feature without worrying about

how it affects others ◦ Rules don’t have exceptions;◦ Context doesn’t affect the behavior of a language

feature (e.g., a reserved word doesn’t have different meanings based on where it’s used).

◦ Things that are syntactically similar have similar meaning.

Orthogonality

Copyright © 2006 The McGraw-Hill Companies, Inc.

Rule exceptions: ◦ C functions can return a struct, but not an array.

Orthogonality => return value can be any type. Syntactic exceptions

◦ In Fortran, an identifier can indicate the type of the variable; this could be considered non-orthogonal

If A = B; and A = (B); acceptable, should (A) = B; be allowed?

Examples of Non-Orthogonality

Copyright © 2006 The McGraw-Hill Companies, Inc.

Embedded systems◦ Real-time responsiveness (e.g., navigation)◦ Failures of early Ada implementations – real time?

Corporate database applications◦ Efficient search and updating

Ability to implement the language efficiently◦ Characteristics of Lisp didn’t match the

architectures of early computers◦ Characteristics of Algol made it difficult to build

efficient translators.

Efficient Implementation

Copyright © 2006 The McGraw-Hill Companies, Inc.

Compiler – produces machine codeInterpreter – executes instructions directly Example compiled languages:

◦ Fortran, Cobol, C, C++ Example interpreted languages:

◦ Scheme, Haskell, Python Hybrid compilation/interpretation: Java

◦ The Java Virtual Machine (JVM) Just-in-time (JIT) compilation: C#, some Java

◦ Compiled to intermediate code, machine code produced just before execution

1.6 Compilers and Virtual Machines

Copyright © 2006 The McGraw-Hill Companies, Inc.

Translation/Execution – Compilerse

Copyright © 2006 The McGraw-Hill Companies, Inc.

Translate source code program into object code (machine language).

Object code is linked to libraries, other modules, to generate an executable object code module

Object code can be executed repeatedly without repeating the compilation process.

Compilers

Copyright © 2006 The McGraw-Hill Companies, Inc.

Translation/Execution – Interpreter

Copyright © 2006 The McGraw-Hill Companies, Inc.

No object code file generated. Interpretive routines

◦ Examine the program. When an action is recognized, do it (by calling one of the routines).

Slower execution (than with compilation): lexical, syntax, type/semantic analysis must be performed every time it runs

Better interactive development environment.

Interpreters

Copyright © 2006 The McGraw-Hill Companies, Inc.

Some languages (e.g., Java) are compiled to a machine code (byte code) that runs on a virtual machine (the JVM).

To run Java programs install a JVM for your machine that interprets the byte code.

Benefit: “compile once, run anywhere” – as long as “anywhere” has a JVM. Compiled languages need to be compiled for a specific platform

Disdadvantage: slow Solution: JIT compilers

Virtual Machines & Interpreters

Copyright © 2006 The McGraw-Hill Companies, Inc.

Programming language principles◦ Grammars, syntax, semantics

What makes a language successful? Reliable, readable, writeable Supported by simplicity, orthogonality, efficiency,

support, … Paradigms

◦ Imperative, object-oriented, functional, logic Language implementation

◦ Compilers & interpreters & hybrid systems

Summary