Introduction (Sections 1.1-1.3) - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction (Sections 1.1-1.3)

Description:

(vector-set! x i ( (vector-ref x i) 1) ... Programming is the art of telling another human being what one wants the computer to do ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 26
Provided by: csU50
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction (Sections 1.1-1.3)


1
Introduction(Sections 1.1-1.3)
  • CSCI 431 Programming Languages
  • Fall 2003

A modification of slides developed by Felix
Hernandez-Campos at UNC Chapel Hill
2
Programming Languages
  • What is a programming language?

3
Programming Languages
  • A language used to express instructions to a
    computer.
  • Definition of legal Programs (Syntax)
  • Meaning of a Program (Semantics)
  • Style of Programming (Pragmatics)
  • Example, Consider the following statement
  • set xi to xi 1
  • This is clearly intended to denote the increment
    of an array element. How would we translate this
    statement to a variety of different languages,
    and what would it mean?

4
In C (circa 1970), we would write this as
  • xi xi 1
  • This performs a hardware lookup for the address
    of x and adds i to it. The addition is a hardware
    operation, so it is dependent upon the hardware
    in question. This resulting address is then
    referenced (if it's legal - which it might not
    be), 1 is added to the bit-string stored there
    (again, as a hardware addition, which can
    overflow), and the result is stored back to that
    location. However, no attempt has been made to
    determine that x is even a vector and that xi
    is a number.

5
In Scheme (1975), this would be transcribed as
  • (vector-set! x i ( (vector-ref x i) 1))
  • This does all the things the corresponding C
    operation does, but in addition it also (a)
    checks that the object named x is indeed an
    array, (b) makes sure it is within the bounds of
    the array, (c) ensures the dereferenced location
    contains a number, and (d) performs abstract
    arithmetic (so there will be no overflow'').

6
Finally, in Java (circa 1991), one might write
  • xi xi 1
  • which looks identical to the C code. However,
    the actions performed are those performed by the
    Scheme code, with one major difference the
    arithmetic is not as abstract. It is defined to
    be done as if the machine were a 32-bit machine,
    which means we can always determine the result of
    an operation, no matter which machine we execute
    the program on, but we cannot have our numbers
    grow arbitrarily large.

7
Programming Languages
  • What is a programming language?
  • Abstraction of virtual machine

8
A simple algorithm for testing primality in Java
  • public static boolean isprime (int n)
  • int d
  • for (d 2 d lt n d)
  • if (n d 0)
  • return false
  • return true

9
In Intel X86 Assembler
  • .globl isprime
  • isprime
  • pushl ebp set up procedure entry
  • movl esp,ebp
  • pushl esi pushl ebx
  • movl 8(ebp),ebx fetch arg n from stack
  • movl 2,esi set divisor d 2
  • cmpl ebx,esi compare n,d
  • jge true jump if d gt n
  • loop
  • movl ebx,eax set n into ....
  • cltd ... dividend register
  • idivl esi divide by d
  • testl edx,edx remainder 0?
  • done
  • leal -8(ebp),esp clean up and exit
  • popl ebx
  • popl esi

10
Machine Code Characteristics
  • Explicit registers for values and intermediate
    results.
  • Low-level machine instructions to implement
    operations.
  • Control flow based on labels and conditional
    branches.
  • Explicit memory management (e.g., stack
    management for procedures).

11
General Characteristics of HLLs
  • Complex Expressions (Arithmetic, Logical, ...)
  • Structured Control Operators (Loops,
    Conditionals, Cases)
  • Composite Types (Arrays, Records, etc.)
  • Type Declarations and Type Checking
  • Multiple storage classes (global/local/heap)
  • Procedures/Functions, with private scope, maybe
    first-class
  • Maybe abstract data types, modules, objects, etc.
  • Maybe high-level control mechanisms (Exceptions,
    Back-tracking, etc.)

12
Programming Languages
  • What is a programming language?
  • Donald Knuth
  • Programming is the art of telling another human
    being what one wants the computer to do

int sum(int x) int sum 0 n 0
while (n lt x.length) sum xn
return sum
00101010101010 10101011111010 11101010101110 00101
010101010 ...
13
The Number of Programming Languages
  • How many programming languages do you know?
  • This is a sample list
  • http//dmoz.org/Computers/Programming/Languages
  • Why is the number of programming languages so
    large?
  • Evolution
  • Special Purpose
  • Personal Preference

14
The Number of Programming Languages
  • How many programming languages do you know?
  • This is a sample list
  • http//dmoz.org/Computers/Programming/Languages/
  • Why is the number of programming languages so
    large?
  • Evolution
  • Special Purpose
  • Personal Preference

15
Evolution Genealogy
Prolog
Scheme
From Sebestas Concepts of Programming Languages
Java
16
The Number of Programming Languages
  • How many programming languages do you know?
  • This is a sample list
  • http//dmoz.org/Computers/Programming/Languages/
  • Why is the number of programming languages so
    large?
  • Evolution
  • Special Purpose
  • Personal Preference

17
The Number of Programming Languages
  • How many programming languages do you know?
  • This is a sample list
  • http//dmoz.org/Computers/Programming/Languages/
  • Why is the number of programming languages so
    large?
  • Evolution
  • Special Purpose
  • Personal Preference

18
The Number of Programming Languages
  • How many programming languages do you know?
  • This is a sample list
  • http//dmoz.org/Computers/Programming/Languages/
  • Why is the number of programming languages so
    large?
  • Evolution
  • Special Purpose
  • Personal Preference
  • A programming language is a way of thinking
  • Different people think in a different way

19
A simple algorithm for testing primality
  • In Java
  • public static boolean isprime (int n)
  • int d
  • for (d 2 d lt n d)
  • if (n d 0)
  • return false
  • return true

20
A simple algorithm for testing primality
  • In Standard ML (using a recursive function)
  • fun isprime (nint) bool
  • let fun no_divisor (dint) bool
  • (d gt n) orelse ((n mod d ltgt 0) andalso
    (no_divisor (d1)))
  • in no_divisor 2 end

21
Successful Programming Languages
  • Are all languages equally successful?
  • No!
  • What makes a language successful?
  • Expressive power
  • Ease of use for the novice
  • Ease of implementation
  • Excellent compilers
  • Economics, patronage, and inertia

22
Classification of Programming Languages
  • Imperative languages
  • Algorithmsdata structuresassignment
  • Von Neumann languages
  • E.g. Fortran, Basic, C
  • Object-oriented languages
  • E.g. C, Java

23
Classification of Programming Languages
  • Declarative languages
  • No assignment (well sorta)
  • Functional languages
  • Functionslists
  • E.g. Lisp, ML, and Haskell
  • Dataflow languages
  • Concurrent
  • E.g. Id and Val
  • Logic or constraint-based languages
  • Propositionspredicateslogical deduction
  • E.g. Prolog

24
Why study programming languages?
  • Use the most appropriate programming language for
    your task
  • E.g. Java is great for writing applications
  • E.g. C is great for systems programming
  • Make it easier to learn new languages
  • Evolution gt Similarities
  • Make better use of language features
  • Obscure features
  • Cost of features
  • Simulate useful features

25
Summary
  • Programming languages
  • Set of abstractions gt virtual machine
  • A way of thinking
Write a Comment
User Comments (0)
About PowerShow.com