Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you.

Similar presentations


Presentation on theme: "Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you."— Presentation transcript:

1 Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you need to know, but we have allowed you admittance to the “show”. You know how to build a project. You can run hello world. You understand key differences. You know where the documentation is. What you do with your admission ticket is up to you! Note – for resumes, “exposure” is likely the correct terminology

2 © Kenneth C. Louden, 2003 Adapted by Vicki Allan 2010 2 Chapter 1 - Introduction Programming Languages: Principles and Practice, 2nd Ed.

3 Chapter 1Louden, Programming Languages3 Course Motivation Why are programming languages the way they are? How are particular language features implemented/supported? Terminology for communication

4 Chapter 1Louden, Programming Languages4 Course Motivation cont… understand the underlying ideas of the main programming paradigms know more about the huge variety of programming languages understand how the syntax and semantics of languages can be defined precisely. have a deeper understanding of the history and rationale behind languages like C++.

5 Chapter 1Louden, Programming Languages5 Relationship between thought and language. The Sapir-Whorf hypothesis in linguistics states that the structure of one's native-tongue influences the way one's mind perceives the world. It has found at best very limited experimental support, at least in its strong form. One study has shown that subjects in memory tests are more likely to remember a given color if their nativelanguage includes a word for that color. Example – if you had no identified concept of recursion, how would that affect the ability to reason about it? Array accessing – what is concept? First programming language affects ability to think in Object Oriented terms

6 Chapter 1Louden, Programming Languages6 Why study programming languages? Understanding of terminology Increased capacity to express ideas improved background for choosing language increased ability to learn and retain new languages - something to hang on to Better understanding of significance of implementation. Efficiency is key – not just ease of programming. Ability to design new languages - or user interface

7 Chapter 1Louden, Programming Languages7 Example Beginning students – always wanted to know specific answers:Can I do X? What happens if I do Y? Often hadn’t tried the specific test, but could reason about it from general knowledge of implementation. Ex: What happens if I try to return a reference to a local variable? Is this a compile time or run time issue? How will the system respond?

8 Chapter 1Louden, Programming Languages8 Programming languages bridge the human- computer semantic gap Human: Interested in modeling the real world More interested in what computer should do than how Computer: Only data it can manipulate is sequences of zeros and ones. Understands low-level “how” instructions.

9 Chapter 1Louden, Programming Languages9 What are programming languages… High-level languages bridge the human- computer semantic gap by providing a higher level notation that can still be executed by computer Definition: A programming language is a notational system for describing computation in machine-readable and human-readable form.

10 Chapter 1Louden, Programming Languages10 Computation: Described by a Turing Machine - a very simple computer that can carry out all known computations (albeit not very efficiently). A programming language is Turing complete if it can be used to describe any computation performed by a Turing Machine.

11 Chapter 1Louden, Programming Languages11 Turing Machine – 1936 Alan Turing "If your state is 42 and the symbol you see is a '0' then replace this with a '1', move one symbol to the right, and assume state 17 as your new state.“ a Turing machine consists of: –A tape which is divided into cells. Each cell contains a symbol from some finite alphabet. The alphabet contains a special blank symbol and one or more other symbols. The tape is assumed to be arbitrarily extendible to the left and to the right, –A head that can read and write symbols on the tape and move left and right. –A state register stores the state of the Turing machine. –An action table (or transition function) that tells the machine what symbol to write, how to move the head ('L' for one step left, and 'R' for one step right) and what its new state will be, given the symbol it has just read on the tape and the state it is currently in.

12 Chapter 1Louden, Programming Languages12 What is needed for Turing completeness? integer variables arithmetic sequentially execution of statements, which include assignment, selection (if) and loop (while) statements.

13 Chapter 1Louden, Programming Languages13 Machine-readability: Basically, the existence of a (more or less) linear-time translation algorithm. –Usually boils down to: The syntax must be given by a context- free grammar. We will discuss context-free grammars. Basically, the language must have rules which aid parsing.

14 Chapter 1Louden, Programming Languages14 Human-readability: This is the real issue! Virtually all the complex details of a programming language are there to (supposedly) enhance human readability. Still not very well understood. Is strongly dependent on good choice of abstractions. –Abstraction is the result of generalization by reducing the information content of a concept,typically to retain only information which is relevant for a particular purpose. For example, abstracting a leather soccer ball to a ball retains only the information on general ball attributes and behavior.

15 Chapter 1Louden, Programming Languages15 What about human “writability??” Aren’t programming languages there to promote the writing of programs, not the reading of them? Nonsense! Writability is a hacker’s goal. Readability is the real goal: many people are going to have to read your program after you have written it. –What is the relationship between readability and writability?

16 Chapter 1Louden, Programming Languages16 Abstractions:

17 Chapter 1Louden, Programming Languages17 Computational Paradigms Programming languages began by imitating the operations of a computer. It is not surprising that the kind of computer for which they were written had significant effect on their design. –variables representing memory –assignment to change values –sequential execution of statements

18 Chapter 1Louden, Programming Languages18 Language Paradigms: Imperative (procedural): traditional sequential programming (passive data, active control). Characterized by variables, assignment, and loops. Object-oriented: data-centric, data controls its own use. Action by request to data objects. Characterized by messages, instance variables, and protection. Extension of imperative paradigm. Functional: passive data, but no sequential control; all action by function evaluation (“call”), particularly recursion. No local variables! Similar to mathematics. Ex. Haskell

19 Chapter 1Louden, Programming Languages19 Language Paradigms: Example Haskell (functional style): fact n = if n == 0 then 1 else n * fact (n-1) square x = x * x squarelist lis = if (null lis) then lis else square (head lis): squarelist (tail lis)

20 Chapter 1Louden, Programming Languages20 Language Paradigms (cont.): Logic: Assertions are the basic data; logic inference the basic control. Again, no sequential operation. Similar to mathematics. Ex Prolog ex: I am your sister if I am female and we have common parents. Parallel: well, maybe not really a paradigm, but some think so. Again, no sequential operation. “Declarative”: Logic and functional paradigms share this property: state “what” needs computing, not “how” (sequence). Ex. Prolog

21 Chapter 1Louden, Programming Languages21 Languages and paradigms Imperative: C, Pascal, core Ada, FORTRAN Functional: Lisp (Scheme), ML, Haskell Object-oriented: C++, Java, Smalltalk, Ruby Logic: Prolog Parallel: Java (threads), Ada (tasks)

22 Chapter 1Louden, Programming Languages22 Paradigm use is rarely “pure”: The C program (in text) defined gcd function in a purely functional style, even though C is mainly imperative. The Java program used some imperative code to compute the gcd, and was not completely object- oriented (integers aren’t objects). The Scheme code used sequencing to do I/O, an imperative feature.

23 Chapter 1Louden, Programming Languages23 Examples of languages that are pure (mostly): Imperative: (old) FORTRAN Functional: Haskell Object-oriented: Ruby

24 Chapter 1Louden, Programming Languages24 Language definition Syntax: the structure of a program. Usually given a formal (i.e., mathematical) definition using a context-free language. (Lexical structure - the structure of the words or tokens - uses regular expressions.) Semantics: the actual result of execution. Usually described in English, but can be done mathematically. Semantics can have a static (compile time) component: type checking, definition checking, other consistency checks prior to execution. What are dynamic (run time) components of semantics?

25 Chapter 1Louden, Programming Languages25 Language translation Compiler: two-step process that 1. translates source code into executable code; 2. the user executes the executable code. compiler run sourceexecutable inputs outputs

26 Chapter 1Louden, Programming Languages26 Language translation Interpreter: one-step process in which the source code is executed directly. Hybrids are also possible (Java). run source inputs outputs Java compiler sourcebytecode inputs outputs machine dependent interpreter Bytecode is composed of instructions that have been brought to the lowest level possible without making them machine dependent.

27 Chapter 1Louden, Programming Languages27 Compilation, Interpretation, and Hybrid systems Consider this piece of code: public class Test { public static void main(String args[]) { int i; i = 2; i = i + 7; } }

28 Chapter 1Louden, Programming Languages28 If we were to compile it, we would create a file of machine instructions that would only work for one architecture. If we were to interpret it, our interpreter would have to be able to understand the high level code AND would repeatedly parse it (if the code was in a loop or called multiple times). Parsing is the process of analyzing a text, made of a sequence of tokens (for example, words), to determine its grammatical structure with respect to a given grammar. tokens When we use the hybrid approach of Java, we produce the file Test.class, which is a binary file that's not readable by most humans. We can convert the file to a readable form with the javap tool as shown here:

29 Chapter 1Louden, Programming Languages29 C:\ > javap -c Test Method void main(java.lang.String[]) 0 iconst_2 // Put integer 2 on stack 1 istore_1 // Store the top stack value at location 1 2 iload_1 // Put the value at location 1 on stack 3 bipush 7 // Put the value 7 on the stack 5 iadd // Add two top stack values together 6 istore_1 // Sum, on top of stack, stored 7 return // Finish processing

30 Chapter 1Louden, Programming Languages30 Although the bytecode cannot access registers or directly reference memory locations and must obey various other restrictions, the actual JVM (java virtual machine) program can use whatever techniques are convenient to use for a particular platform. As long as the Java bytecode sees only a JVM specification compliant system, the JVM programmer has broad discretion for its implementationJVM specification

31 Chapter 1Louden, Programming Languages31 Language Implementation Steps Compilation: lexical analysis: characters grouped into logical chunks (keywords, constants, etc) syntax analysis: figure out what it means - usually use parse trees (grammars to define). Like diagraming sentences. I have a picture of John at my home – what is meant? What does “at my home” modify? optimization - to make smaller or faster linking: supplying missing addresses to system code load module: user code augmented with system code

32 Chapter 1Louden, Programming Languages32 Language Implementation Steps (cont) Pure Interpretation: no translation phase - fetch, decode, and execute the source code (not machine code) Advantages/Disadvantages 1. easy to write debugger - as source lines are unchanged 2. execution is 10-100 times slower; statement decoding is bottleneck 3. better for language with simple structure - as not so slow to decode 4. natural for some kinds of features - like dynamic binding of type. Ex: c = c+b If c may be integer, string, or a set, how can we know what code to generate? 5. Nice for executing user produced code at runtime Ex. boolean expression is easy to evaluate if known at compile time, but NOT if produced at run time.

33 Chapter 1Louden, Programming Languages33 What is meant by dynamic binding? Girls choice dance: –Will you go with Sofie? (early binding) –Will you go with Sofie/Ann/Betty (whoever shows up at your door)? (delayed binding) –No specific partner assigned, but will change throughout the night. (changing binding) Lots of the interesting issues involve binding times.

34 Chapter 1Louden, Programming Languages34 Class binding (compiled language) Figure out the class to be invoked Determine which method signature to use If there is more than one matching signature, the one that is most specific is chosen. Example: doit(Object o) or doit(ColoredPoint p) or doit (Point p) A method is applicable if –the number of parameters matches –the type of each actual argument can be converted to the type of the corresponding parameter. A method is accessible - we know about the method

35 Chapter 1Louden, Programming Languages35 Error classification Lexical: character-level error, such as illegal character (hard to distinguish from syntax error). Syntax: error in structure (e.g., missing semicolon or keyword). Static semantic: non-syntax error prior to execution (e.g., undefined vars, type errors). Dynamic semantic: non-syntax error during execution (e.g., division by 0). Logic: programmer error, program not at fault.

36 36 Rectangle Triangle Polygon class Polygon{ int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); String display(String s) { return “POLYGON” + s; float area(); }; class Rectangle: public Polygon{ public: float area(); String display(String s) { return “RECTANGLE” + s … ; }; class Triangle: public Polygon{ public: float area() {…} String display(String s) { return “TRIANGE” + s …; }; Inheritance Concept

37 Which display do you want? Can you tell at compile time? Could there be several possible matches? Chapter 1Louden, Programming Languages37

38 Chapter 1Louden, Programming Languages38 Notes on error reporting A compiler will report lexical, syntax, and static semantic errors. It cannot report dynamic semantic errors; job of runtime system. An interpreter will often only report lexical and syntax errors when loading the program. Static semantic errors may not be reported until just prior to execution. Indeed, most interpreted languages (e.g. Lisp, Smalltalk) do not define any static semantic errors. No translator can report a logic error.

39 Chapter 1Louden, Programming Languages39 Find examples of syntax, semantic and logic errors. public int gcd ( int v# ) { int z = value y = v; while ( y >= 0 ) { int t = y; y = z % y; z = t; } return y; }

40 Chapter 1Louden, Programming Languages40 Sample Errors (Java): public int gcd ( int v# ) // lexical bad # { int z = value // syntax - missing ; y = v; // static semantic - y undefined while ( y >= 0 ) { int t = y; y = z % y; z = t; // dynamic semantic - division by zero } return y; // logic - should return z }


Download ppt "Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you."

Similar presentations


Ads by Google