Download presentation
Presentation is loading. Please wait.
Published bySarah Smith Modified over 9 years ago
1
1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill
2
2 Programming Languages What is a programming language?What is a programming language? Programming Language
3
3 Programming Languages A language used to express instructions to a computer.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: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
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. 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
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''). 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
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. 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
7 Programming Languages What is a programming language?What is a programming language? –Abstraction of virtual machine int sum(int[] x) { int sum = 0; int sum = 0; n = 0; n = 0; while (n < x.length) { while (n < x.length) { sum += x[n]; sum += x[n]; } return sum; return sum;}00101010101010101010111110101110101010111000101010101010...
8
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
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 leaveret
10
10 Machine Code Characteristics: Explicit registers for values and intermediate results.Explicit registers for values and intermediate results. Low-level machine instructions to implement operations.Low-level machine instructions to implement operations. Control flow based on labels and conditional branches.Control flow based on labels and conditional branches. Explicit memory management (e.g., stack management for procedures).Explicit memory management (e.g., stack management for procedures).
11
11 General Characteristics of HLLs: Complex Expressions (Arithmetic, Logical,...)Complex Expressions (Arithmetic, Logical,...) Structured Control Operators (Loops, Conditionals, Cases)Structured Control Operators (Loops, Conditionals, Cases) Composite Types (Arrays, Records, etc.)Composite Types (Arrays, Records, etc.) Type Declarations and Type CheckingType Declarations and Type Checking Multiple storage classes (global/local/heap)Multiple storage classes (global/local/heap) Procedures/Functions, with private scope, maybe first-classProcedures/Functions, with private scope, maybe first-class Maybe abstract data types, modules, objects, etc.Maybe abstract data types, modules, objects, etc. Maybe high-level control mechanisms (Exceptions, Back- tracking, etc.)Maybe high-level control mechanisms (Exceptions, Back- tracking, etc.)
12
12 Programming Languages What is a programming language?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; int sum = 0; n = 0; n = 0; while (n < x.length) { while (n < x.length) { sum += x[n]; sum += x[n]; } return sum; return sum;}00101010101010101010111110101110101010111000101010101010...
13
13 The Number of Programming Languages How many programming languages do you know?How many programming languages do you know? –This is a sample list… »http://dmoz.org/Computers/Programming/Languages http://dmoz.org/Computers/Programming/Languages Why is the number of programming languages so large?Why is the number of programming languages so large? –Evolution –Special Purpose –Personal Preference
14
14 The Number of Programming Languages How many programming languages do you know?How many programming languages do you know? –This is a sample list… This is a sample list…This is a sample list… »http://dmoz.org/Computers/Programming/Languages/ Why is the number of programming languages so large?Why is the number of programming languages so large? –Evolution –Special Purpose –Personal Preference
15
15 Evolution: Genealogy From Sebesta’s Concepts of ProgrammingLanguagesPrologSWI-Prolog Scheme Java
16
16 The Number of Programming Languages How many programming languages do you know?How many programming languages do you know? –This is a sample list… This is a sample list…This is a sample list… »http://dmoz.org/Computers/Programming/Languages/ Why is the number of programming languages so large?Why is the number of programming languages so large? –Evolution –Special Purpose –Personal Preference
17
17 The Number of Programming Languages How many programming languages do you know?How many programming languages do you know? –This is a sample list… This is a sample list…This is a sample list… »http://dmoz.org/Computers/Programming/Languages/ Why is the number of programming languages so large?Why is the number of programming languages so large? –Evolution –Special Purpose –Personal Preference
18
18 The Number of Programming Languages How many programming languages do you know?How many programming languages do you know? –This is a sample list… This is a sample list…This is a sample list… »http://dmoz.org/Computers/Programming/Languages/ Why is the number of programming languages so large?Why is the number of programming languages so large? –Evolution –Special Purpose –Personal Preference A programming language is a way of thinkingA programming language is a way of thinking –Different people think in a different way
19
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
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
21 Successful Programming Languages Are all languages equally successful?Are all languages equally successful? –No! What makes a language successful?What makes a language successful? –Expressive power –Ease of use for the novice –Ease of implementation –Excellent compilers –Economics, patronage, and inertia
22
22 Why study programming languages? Use the most appropriate programming language for your taskUse 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 languagesMake it easier to learn new languages –Evolution => Similarities Make better use of language featuresMake better use of language features –Obscure features –Cost of features –Simulate useful features
23
23 Classification of Programming Languages Imperative languagesImperative languages –Algorithms+data structures+assignment Von Neumann languagesVon Neumann languages »E.g. Fortran, Basic, C Object-oriented languagesObject-oriented languages »E.g. C++, Java
24
24 Classification of Programming Languages Declarative languagesDeclarative languages –No assignment (well sorta) Functional languagesFunctional languages –Functions+lists –E.g. Lisp, ML, and Haskell Dataflow languagesDataflow languages –Concurrent –E.g. Id and Val Logic or constraint-based languagesLogic or constraint-based languages –Propositions+predicates+logical deduction –E.g. Prolog
25
25 Summary Programming languages:Programming languages: –Set of abstractions => virtual machine –A way of thinking
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.