Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V.,...

22
1/15/14 1 Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A A g go oo od d p pr ro og gr ra am mm mi in ng g l la an ng gu ua ag ge e i is s a a c co on nc ce ep pt tu ua al l u un ni iv ve er rs se e f fo or r t th hi in nk ki in ng g a ab bo ou ut t p pr ro og gr ra am mm mi in ng g. . A A. . P Pe er rl li is s 1-1 Contents 1.1 Principles 1.2 Paradigms 1.3 Special Topics 1.4 A Brief History 1.5 On Language Design 1.5.1 Design Constraints 1.5.2 Outcomes and Goals 1.6 Compilers and Virtual Machines 1-2

Transcript of Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V.,...

Page 1: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

1

Copyright © 2006 The McGraw-Hill Companies, Inc.

Programming Languages 2nd edition

Tucker and Noonan"

"Chapter 1""Overview""""AA ggoooodd pprrooggrraammmmiinngg llaanngguuaaggee iiss aa ccoonncceeppttuuaall

uunniivveerrssee ffoorr tthhiinnkkiinngg aabboouutt pprrooggrraammmmiinngg.. """" "" "" "" "" "" "" "" "" "" "" ""AA.. PPeerrlliiss""

""

1-1

Copyright © 2006 The McGraw-Hill Companies, Inc.

Contents"

1.1 Principles"1.2 Paradigms"1.3 Special Topics"1.4 A Brief History"1.5 On Language Design"

"1.5.1 Design Constraints""1.5.2 Outcomes and Goals"

1.6 Compilers and Virtual Machines"

1-2

Page 2: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

2

Programming languages have four properties: –  Syntax

–  Names –  Types

–  Semantics

For any language: –  Its designers must define these properties

–  Its programmers must master these properties

1.1 Principles"

1-3 Copyright © 2006 The McGraw Hill Companies, Inc.

Syntax""

The syntax of a programming language is a precise description of all its grammatically correct programs.

When studying syntax, we ask questions like: –  What is the grammar for the language?

–  What is the basic vocabulary?

–  How are syntax errors detected?

1-4 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 3: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

3

Names""Various kinds of entities in a program have names:

variables, types, functions, parameters, classes, objects, …

Named entities are bound in a running program to: –  Scope

–  Visibility

–  Type –  Lifetime

1-5 Copyright © 2006 The McGraw Hill Companies, Inc.

Types""

A type is a collection of values and a collection of operations on those values.

•  Simple types –  numbers, characters, booleans, …

•  Structured types –  Strings, lists, trees, hash tables, …

•  A language’s type system can help to: –  Determine legal operations

–  Detect type errors

1-6 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 4: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

4

Semantics""The meaning of a program is called 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 run-time behavior, such as function call?

–  How are objects allocated to memory at run-time?

1-7 Copyright © 2006 The McGraw Hill Companies, Inc.

A programming paradigm is a pattern of problem-solving thought that underlies a particular genre of programs and languages.

There are four main programming paradigms: –  Imperative

–  Object-oriented –  Functional

–  Logic (declarative)

1.2 Paradigms"

1-8 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 5: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

5

Imperative Paradigm""

Follows the classic von Neumann-Eckert model: –  Program and data are indistinguishable in memory

–  Program = a sequence of commands –  State = values of all variables when program runs

–  Large programs use procedural abstraction

Example imperative languages: –  Cobol, Fortran, C, Ada, Perl, …

1-9 Copyright © 2006 The McGraw Hill Companies, Inc.

The von Neumann-Eckert Model""

1-10 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 6: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

6

1-11

The von Neumann Architecture •  Fetch-execute-cycle (on a von Neumann

architecture computer) initialize the program counter

repeat forever

fetch the instruction pointed by the counter

increment the counter

decode the instruction

execute the instruction

end repeat

Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

Object-oriented (OO) Paradigm""

An OO Program is a collection of objects that interact by passing messages that transform the state.

When studying OO, we learn about: –  Sending Messages

–  Inheritance

–  Polymorphism

Example OO languages:

Smalltalk, Java, C++, C#, and Python

1-12 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 7: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

7

Functional Paradigm""Functional programming models a computation as a

collection of mathematical functions. –  Input = domain –  Output = range

Functional languages are characterized by: –  Functional composition –  Recursion

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

1-13 Copyright © 2006 The McGraw Hill Companies, Inc.

Logic Paradigm""Logic programming declares what outcome the

program should accomplish, rather than how it should be accomplished.

When studying logic programming we see: –  Programs as sets of constraints on a problem

–  Programs that achieve all possible solutions –  Programs that are nondeterministic

Example logic programming languages: –  Prolog

1-14 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 8: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

8

•  Event handling –  E.g., GUIs, home security systems

•  Concurrency –  E.g., Client-server programs

•  Correctness –  How can we prove that a program does what it is

supposed to do under all circumstances?

–  Why is this important???

1.3 Special Topics"

1-15 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"

1-16 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 9: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

9

Evolution of Programming Languages Machine Language - 0's and 1's

Assembly Language - mnemonic form of Machine Language First Generation Languages - higher-level data and control constructions corresponding to Machine Language data and control (e.g., FORTRAN)

Second Generation Languages - higher-level data and control constructions, not always corresponding to, but still modeled after Machine Language data and control (e.g., ALGOL 60, COBOL)

Third Generation Languages - introduction of more abstract forms of data, including user-defined data types (e.g., Pascal, C)

1-17 Copyright © 2004 Barrett R. Bryant

Evolution of Programming Languages Object-Based Languages - support for objects and abstract data types (e.g., Ada)

Object-Oriented Languages - support for classes of objects organized as a class hierarchy (e.g., Smalltalk, C++, Java)

. . .

Natural Languages - humans communicate directly with the machine (e.g., English)

1-18 Copyright © 2004 Barrett R. Bryant

Page 10: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

10

1-19 Copyright © 2006 The McGraw Hill Companies, Inc.

Design Constraints –  Computer architecture

–  Technical setting –  Standards

–  Legacy systems

Design Outcomes and Goals

1.5 On Language Design"

1-20 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 11: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

11

1-21 Copyright © 2006 The McGraw Hill Companies, Inc.

What makes a successful language?""

Key characteristics: –  Simplicity and readability

–  Clarity about binding –  Reliability

–  Support

–  Abstraction –  Orthogonality

–  Efficient implementation

1-22 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 12: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

12

Simplicity and Readability""

•  Small instruction set –  E.g., Java vs Scheme

•  Simple syntax –  E.g., C/C++/Java vs Python

•  Benefits: –  Ease of learning –  Ease of programming

1-23 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 –  Examples:

•  a variable and its type

•  a variable and its value

–  Early binding takes place at compile-time

–  Late binding takes place at run time

Clarity about Binding""

1-24 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 13: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

13

Reliability""

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

•  E.g., early versions of Fortran

–  Type errors are detected •  E.g., C vs Haskell

–  Semantic errors are properly trapped •  E.g., C vs C++

–  Memory leaks are prevented •  E.g., C vs Java

1-25 Copyright © 2006 The McGraw Hill Companies, Inc.

Language Support""

•  Accessible (public domain) compilers/interpreters •  Good texts and tutorials

•  Wide community of users •  Integrated with development environments (IDEs)

1-26 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 14: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

14

Abstraction in Programming""

•  Data –  Programmer-defined types/classes

–  Class libraries

•  Procedural –  Programmer-defined functions

–  Standard function libraries

1-27 Copyright © 2006 The McGraw Hill Companies, Inc.

Orthogonality ""A language is orthogonal if its features are built upon

a small, mutually independent set of primitive operations.

•  Fewer exceptional rules = conceptual simplicity –  E.g., restricting types of arguments to a function

•  Tradeoffs with efficiency

1-28 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 15: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

15

Efficient implementation ""•  Embedded systems

–  Real-time responsiveness (e.g., navigation)

–  Failures of early Ada implementations

•  Web applications –  Responsiveness to users (e.g., Google search)

•  Corporate database applications –  Efficient search and updating

•  AI applications –  Modeling human behaviors

1-29 Copyright © 2006 The McGraw Hill Companies, Inc.

Compiler – produces machine code Interpreter – executes instructions on a virtual

machine •  Example compiled languages:

–  Fortran, Cobol, C, C++

•  Example interpreted languages: –  Scheme, Haskell, Python

•  Hybrid compilation/interpretation –  The Java Virtual Machine (JVM)

1.6 Compilers and Virtual Machines"

1-30 Copyright © 2006 The McGraw Hill Companies, Inc.

Page 16: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

16

Compilation

target program Hardware Interpreter

Compiler

1-31 Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D., Compilers: Principles, Techniques, and Tools, 2nd ed., Addison-Wesley, 2007.

1-20

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

1-32 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

Page 17: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

17

1-21

The Compilation Process

1-33 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

id1 := temp3

Front-End of a Compiler

1-34 Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D., Compilers: Principles, Techniques, and Tools, 2nd ed., Addison-Wesley, 2007.

Page 18: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

18

Back-End of a Compiler

1-35 Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D., Compilers: Principles, Techniques, and Tools, 2nd ed., Addison-Wesley, 2007.

1-24

Pure Interpretation

•  No translation

•  Easier implementation of programs (run-time errors can easily and immediately be displayed)

•  Slower execution (10 to 100 times slower than compiled programs)

•  Often requires more space

•  Now rare for traditional high-level languages

•  Significant comeback with some Web scripting languages (e.g., JavaScript, PHP)

1-36 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

Page 19: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

19

Pure Interpretation

Software Interpreter

1-37 Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D., Compilers: Principles, Techniques, and Tools, 2nd ed., Addison-Wesley, 2007.

Pure Interpretation

Software Interpreter

Hardware Interpreter

1-38 Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D., Compilers: Principles, Techniques, and Tools, 2nd ed., Addison-Wesley, 2007.

Page 20: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

20

1-25

Pure Interpretation Process

1-39 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

1-26

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)

1-40 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

Page 21: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

21

Hybrid Implementation

Compiler

1-41 Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D., Compilers: Principles, Techniques, and Tools, 2nd ed., Addison-Wesley, 2007.

1-27

Hybrid Implementation Process

1-42 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

Page 22: Programming Languages - CSE at UNT4430s001/introduction.pdf · Interpreter Compiler Aho, A. V., Lam, M. S., Sethi, R ... machine that has a byte code interpreter and a run-time ...

1/15/14

22

1-28

Just-in-Time Implementation Systems

•  Initially translate programs to an intermediate language

•  Then compile the intermediate language of the subprograms into machine code when they are called

•  Machine code version is kept for subsequent calls

•  JIT systems are widely used for Java programs

•  .NET languages are implemented with a JIT system

1-43 Sebesta, R. W., Concepts of Programming Languages, 10th ed., Addison Wesley Longman, 2012.

Execution of a Java Applet

1-44 Orfali, R. and Harkey, D., Client/Server Programming with Java and CORBA, 1st ed., Wiley, 1997.