Top Banner
Programming Languages The Beginning
40

Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Dec 25, 2015

Download

Documents

Marilyn Greene
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Programming Languages

The Beginning

Page 2: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

In the beginning...

• Computers were very expensive; programmers were cheap

• Programming was by plugboards or binary numbers (on-off switches)

• Memories were maybe 4K and up

• Cycle times were in milliseconds

• No compromises for programmer's ease

Page 3: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Early compromises

• Assembly language– reduced human error– programs were just as efficient

• FORTRAN– the compiler generated very efficient code– easier to read, understand, debug– need to compile was still extra overhead– the idea was: compile once, run many times

Page 4: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

The Automation Principle

• Automate mechanical, tedious, or error-prone activities.– Higher-level languages are an example– Assembly language automates writing binary

code– FORTRAN automates writing assembly

language or binary code

Textbook, p. 10Textbook, p. 10

Page 5: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

FORTRAN

• Efficiency was everything

• Card oriented, with information in fixed columns

• First language to catch on in a big way

• Because it was first, FORTRAN has many, many "mistakes"

• Algol 60 was a great leap forward

Page 6: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Expressions

• In FORTRAN, an expression...– In a WRITE statement, could be c (constant), v

(variable), or v+c, or v-c– As a parameter, could be c or v– As an array subscript, could be c, v, c*v, v+c, v-c,

c*v+c, or c*v-c– On the RHS of an assignment, could be anything

• In Algol 60, an expression is an expression is an expression!

Page 7: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

The Regularity Principle

• Regular rules, without exceptions, are easier to learn, use, describe, and implement.– Arithmetic expressions in FORTRAN vs. Algol

are an example– BNF is a great aid to imposing regularity

Textbook, p. 11Textbook, p. 11

Page 8: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Lexical Conventions

• Reserved words, used in most modern languages (C, Pascal, Java)– Easier for experts, somewhat harder for novices

• Keywords, unambiguously marked– Hard to type and often hard to read

• Keywords in context (FORTRAN, PL/1)– If it makes sense as a keyword, it's a keyword,

otherwise it's something else

Page 9: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

The Reserved Word Controversy

• FORTRAN had no reserved words

• IF (I) = 1 was an array assignment• Advantages of reserved words

– Easier for the compiler writer– Helps avoid ambiguities in the language

• Disadvantages of reserved words– Programmer has to know them all– Few reserved words imply less language power?

Page 10: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Other FORTRAN "Mistakes"

• The FORTRAN compiler ignored blanks– DO 50 I=1,10 became DO50I=1,10

• Did not require variable declarations– Misspellings were automatically new variables

• Earliest versions did not have subprograms– But they did have callable library routines

• DO , IF, and GO TO were the only control structures

Page 11: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

The Impossible Error Principle

• Making errors impossible to commit is preferable to detecting them after their commission.– In FORTRAN, variables were declared simply

by appearing in a program

– DO 50 I = 1. 10 is an assignment to DO50I– In general, the earlier an error can be detected,

the better

Textbook, p. 12Textbook, p. 12

Page 12: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Algol 60

• Introduced the idea of a virtual machine

• Used BNF to define syntax formally

• Introduced nested scopes

• Introduced free-format programs

• Introduced recursion

• Required declarations of all variables

• Introduced if-then-else and flexible loops

Page 13: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Algol 60 was three+ languages

• The language definition was given in the reference language

• Algorithms were published using the publication language, in which keywords were boldface

• Programs were in a hardware representation– Often looked like: 'IF' X < Y 'THEN' X := X + 1– Difficult to type, difficult to read

Page 14: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Algol 60 Shortcomings

• Algol 60 had no input/output!– I/O was considered to be too hardware-specific– Most implementations “borrowed” FORTRAN's I/O

• Used “call by name” semantics– powerful, but hard to implement– sometimes hard to understand

• Few but very flexible control structures were considered to be “baroque”

Page 15: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Metalanguages

• A metalanguage is a language used to talk about a language (usually a different one)

• We can use English as its own metalanguage (e.g. describing English grammar in English)

• It is essential to distinguish between the metalanguage terms and the object language terms

Page 16: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF

• BNF stands for either Backus Naur Form or Backus Normal Form

• BNF is a metalanguage used to describe the grammar of a programming language

• BNF is formal and precise

• BNF is essential in compiler construction

• There are many dialects of BNF in use, but…

• …the differences are almost always minor

Page 17: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF

• < > indicate a nonterminal that needs to be further expanded, e.g. <variable>

• Symbols not enclosed in < > are terminals; they represent themselves, e.g. if, while, (

• The symbol ::= means is defined as

• The symbol | means or; it separates alternatives, e.g. <addop> ::= + | -

Page 18: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF uses recursion

• <integer> ::= <digit> | <integer> <digit> or<integer> ::= <digit> | <digit> <integer>

• Many people find recursion confusing

• "Extended BNF" allows repetition as well as recursion

• Repetition is often more efficient when using BNF to construct a compiler

Page 19: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF Examples I

• <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

• <if statement> ::= if ( <condition> ) <statement> | if ( <condition> ) <statement> else <statement>

Page 20: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF Examples II

• <unsigned integer> ::= <digit> | <unsigned integer> <digit>

• <integer> ::= <unsigned integer> | + <unsigned integer> | - <unsigned integer>

Page 21: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF Examples III

• <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>

• <block> ::= { <statement list> }• <statement list> ::=

<statement> | <statement list> <statement>

Page 22: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

BNF Examples IV

• <statement> ::= <block> | <assignment statement> | <break statement> | <continue statement> | <do statement> | <for loop> | <goto statement> | <if statement> | . . .

Page 23: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Extended BNF

• The following are pretty standard:– [ ] enclose an optional part of the rule

– { } mean the enclosed can be repeated any number of times (including zero)

• The textbook uses a different notation:– x* means repeat x zero or more times– x+ means repeat x one or more times

– { } enclose alternatives, usually listed vertically

Page 24: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Limitations of BNF

• No easy way to impose length limitations, such as maximum length of variable names

• No way to impose distributed requirements, such as, a variable must be declared before it is used

• Describes only syntax, not semantics

• Nothing clearly better has been devised

Page 25: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Functions and Procedures

• In mathematics, a function:– returns a value– has no side effects (except maybe I/O)– does not alter its parameters

• A procedure (a.k.a. subroutine):– does not return a value– is called precisely for its side effects– may (probably does) alter its parameters

Page 26: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

C Has No Procedures

• Functions may return void (no value)

• Functions really can’t alter their parameters

• This is inadequate for real programs

• There are workarounds, such as passing pointers to values

• Hence, “functions” in C are seriously distorted

Page 27: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Predicates

• A predicate is a binary (two-valued) function

• In some languages, a predicate returns “true” or “false”

• In other languages (Snobol IV, Prolog, Icon), a predicate “succeeds” or “fails”

Page 28: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Methods

• A procedure or function is called directly

• In an O-O language, an object has methods

• A message is sent to an object

• The object decides what to do about the message

• Typically, the object chooses a method to execute

Page 29: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Scope Rules

• The scope of a variable is the part of a program in which it is defined and accessible

• FORTRAN: scope is the enclosing subprogram

• Prolog: scope is the (one) enclosing clause

• Java: scope is from point of definition to }• Algol, Pascal: scopes are nested

Page 30: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Nested Scopes

beginbegin int x, y; int x, y; --int x and int y are defined here --int x and int y are defined here begin begin float x, z; float x, z; -- int y, float x, float z are defined -- int y, float x, float z are defined herehere -- this is a “hole” in the scope of int x -- this is a “hole” in the scope of int x end end -- int x, int y are defined here -- int x, int y are defined hereendend

Page 31: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Actual and Formal Parameters

• Parameters are passed to subprograms in a variety of ways

• Actual parameters are the values used in a call to the subprogram

• Formal parameters are the names used for those values in the subprogram

Page 32: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Parameter Transmission

• Call by reference (FORTRAN, Pascal, Java)

• Call by value (C, Pascal, Java)

• Call by name (Algol 60)

• Call by value-result (Ada)

• Call by unification (Prolog)

Page 33: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Call by Reference

• Every value (data item) is stored at some particular machine address

• The subprogram is given that address

• The subprogram directly manipulates the original data item

• This is the most efficient way to pass parameters

• In FORTRAN, could alter “constants” this way

Page 34: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Example of Call by Reference

procedure Aprocedure A int X int X X = 5 X = 5

call B (X)call B (X)endend

procedure B (Y)procedure B (Y) Y = Y + 1 Y = Y + 1

endend

X is stored in only X is stored in only one place (that one place (that place is in A), and place is in A), and B is made to refer B is made to refer to that place.to that place.

Page 35: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Call by Value

• Subprograms are given a copy of the formal parameter

• Subprograms are free to change their copy

• The changed value is not copied back

• Safe, but not efficient or flexible

• C uses call-by-value exclusively– workaround: pass a pointer to the data item

Page 36: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Example of Call by Value

procedure Aprocedure A int X int X X = 5 X = 5

call B (X)call B (X)endend

procedure B (Y)procedure B (Y) Y = Y + 1 Y = Y + 1

endend

Value is Value is copied down copied down when B is when B is called, and called, and never copied never copied back upback up

Page 37: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Call by Name

• Used in Algol 60, hardly anywhere else

• Uses the copy rule: the subprogram acts as if it had a textual copy of the formal parameter

• Mathematically, this is very neat

• Doesn’t play well with scope rules

• Violates information hiding (names matter)

• Difficult to implement and usually inefficient

Page 38: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Example of Call by Name

int a, r;int a, r;

function fiddle (int x) {function fiddle (int x) { int a = 3, b = 5; int a = 3, b = 5; return a * x + b; // means a * (a+1) + return a * x + b; // means a * (a+1) + bb}}

a = 9;a = 9;r = fiddle (a+1); // should return 17r = fiddle (a+1); // should return 17print (r);print (r);

Page 39: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

Algol Supported Recursion

• Four rules for recursion:– Always check for base cases first– Recur only with a simpler case– Avoid global variables– Don’t look down

Page 40: Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.

The End