Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction (Sections )

Similar presentations


Presentation on theme: "Introduction (Sections )"— Presentation transcript:

1 Introduction (Sections 1.1-1.3)
CSCI 431 Programming Languages Fall 2003 Introduction (Sections ) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill

2 Programming Languages
What is a programming language? 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 x[i] to x[i] + 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
x[i] = x[i] + 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 x[i] 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
x[i] = x[i] + 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 int sum(int[] x) { int sum = 0; n = 0; while (n < x.length) { sum += x[n]; } return sum; ...

8 A simple algorithm for testing primality in Java:
public static boolean isprime (int n) { int d; for (d = 2; d < 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 >= 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 leave ret

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 < x.length) { sum += x[n]; } return sum; ...

13 The Number of Programming Languages
How many programming languages do you know? This is a sample list… Why is the number of programming languages so large? Evolution Special Purpose Personal Preference * Give me some examples * How many? (more than…)

14 The Number of Programming Languages
How many programming languages do you know? This is a sample list… Why is the number of programming languages so large? Evolution Special Purpose Personal Preference

15 Evolution: Genealogy Prolog Scheme Python 91 (from Modula-3 and ABC 87?). Python 2 influenced by Perl and functional programming. From Sebesta’s Concepts of Programming Languages Java

16 The Number of Programming Languages
How many programming languages do you know? This is a sample list… 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… 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… 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 < 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 (n:int) : bool = let fun no_divisor (d:int) : bool = (d >= n) orelse ((n mod d <> 0) andalso (no_divisor (d+1))) 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 Algorithms+data structures+assignment 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 Functions+lists E.g. Lisp, ML, and Haskell Dataflow languages Concurrent E.g. Id and Val Logic or constraint-based languages Propositions+predicates+logical 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 => Similarities Make better use of language features Obscure features Cost of features Simulate useful features

25 Summary Programming languages:
Set of abstractions => virtual machine A way of thinking


Download ppt "Introduction (Sections )"

Similar presentations


Ads by Google