chapter 1louden, programming languages1 for ruby (and most other languages we will cover), you are...

40
Chapter 1 Louden, Programming Languages 1 (and most other languages we will cover), you are given a tic possibly cover everything you need to know, but we have allow tance to the “show”. how to build a project. You can run hello world. You underst es. You know where the documentation is. What you do with y ticket is up to you! r resumes, “exposure” is likely the correct terminology

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

1Chapter 1 Louden, Programming Languages

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

Page 2: 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

2© Kenneth C. Louden, 2003 Adapted by Vicki

Allan 2010

Chapter 1 - Introduction

Programming Languages:Principles and Practice, 2nd Ed.

Page 3: 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

Louden, Programming Languages 3Chapter 1

Course Motivation

Why are programming languages the way they are?

How are particular language features implemented/supported?

Terminology for communication

Page 4: 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

Louden, Programming Languages 4Chapter 1

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++.

Page 5: 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

Louden, Programming Languages 5Chapter 1

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

Page 6: 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

Louden, Programming Languages 6Chapter 1

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

Page 7: 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

Louden, Programming Languages 7Chapter 1

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?

Page 8: 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

Louden, Programming Languages 8Chapter 1

Programming languages bridge the human-computer semantic gap

Human: Interested in modeling the real world More interested in what computer should do

than howComputer: Only data it can manipulate is sequences of

zeros and ones. Understands low-level “how” instructions.

Page 9: 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

Louden, Programming Languages 9Chapter 1

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.

Page 10: 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

Louden, Programming Languages 10Chapter 1

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.

Page 11: 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

Louden, Programming Languages 11Chapter 1

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.

Page 12: 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

Louden, Programming Languages 12Chapter 1

What is needed for Turing completeness?

• integer variables• arithmetic• sequentially execution of statements, which

include assignment, selection (if) and loop (while) statements.

Page 13: 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

Louden, Programming Languages 13Chapter 1

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.

Page 14: 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

Louden, Programming Languages 14Chapter 1

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.

Page 15: 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

Louden, Programming Languages 15Chapter 1

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?

Page 16: 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

Louden, Programming Languages 16Chapter 1

Abstractions:

Simple Structured Unit

Data int, char

class, struct

file, package, API, ADT

Control goto, =

if { } else { }, while { }, method

file, package, API, ADT

Page 17: 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

Louden, Programming Languages 17Chapter 1

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

Page 18: 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

Louden, Programming Languages 18Chapter 1

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

Page 19: 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

Louden, Programming Languages 19Chapter 1

Language Paradigms:Example Haskell (functional style):

fact n = if n == 0 then 1 else n * fact (n-1)square x = x * xsquarelist lis =if (null lis) then liselse square (head lis): squarelist (tail lis)

Page 20: 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

Louden, Programming Languages 20Chapter 1

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

Page 21: 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

Louden, Programming Languages 21Chapter 1

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)

Page 22: 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

Louden, Programming Languages 22Chapter 1

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.

Page 23: 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

Louden, Programming Languages 23Chapter 1

Examples of languages that are pure (mostly):

Imperative: (old) FORTRAN

Functional: Haskell

Object-oriented: Ruby

Page 24: 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

Louden, Programming Languages 24Chapter 1

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?

Page 25: 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

Louden, Programming Languages 25Chapter 1

Language translation

Compiler: two-step process that

1. translates source code into executable code;

2. the user executes the executable code.

compiler runsource executable

inputs

outputs

Page 26: 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

Louden, Programming Languages 26Chapter 1

Language translation

Interpreter: one-step process in which the source code is executed directly.

Hybrids are also possible (Java).

runsource

inputs

outputs

Javacompilersource bytecode

inputs

outputsmachine dependentinterpreter

Bytecode is composed of instructions that have been brought to the lowest level possible without making them machine dependent.

Page 27: 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

Louden, Programming Languages 27Chapter 1

Compilation, Interpretation, and Hybrid systemsConsider this piece of code:

public class Test {    public static void main(String args[])

   {       int i;      i = 2;      i = i + 7;    } }

Page 28: 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

Louden, Programming Languages 28Chapter 1

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.

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:

Page 29: 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

Louden, Programming Languages 29Chapter 1

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

Page 30: 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

Louden, Programming Languages 30Chapter 1

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 implementation

Page 31: 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

Louden, Programming Languages 31Chapter 1

Language Implementation StepsCompilation: 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

Page 32: 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

Louden, Programming Languages 32Chapter 1

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 runtimeEx. boolean expression is easy to evaluate if known

at compile time, but NOT if produced at run time.

Page 33: 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

Louden, Programming Languages 33Chapter 1

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.

Page 34: 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

Louden, Programming Languages 34Chapter 1

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

Page 35: 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

Louden, Programming Languages 35Chapter 1

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.

Page 36: 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

36

RectangleTriangle

Polygonclass 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

Page 37: 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

Louden, Programming Languages 37

Which display do you want?

Can you tell at compile time?Could there be several possible

matches?

Chapter 1

Page 38: 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

Louden, Programming Languages 38Chapter 1

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.

Page 39: 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

Louden, Programming Languages 39Chapter 1

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; }

Page 40: 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

Louden, Programming Languages 40Chapter 1

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}