Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter...

33
CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE3302 CSE3302 Programming Languages Programming Languages Dr. Carter Tiernan

description

Ch Ch jcmt CSE 3302 Programming Languages Why is this interesting? Although it’s possible to write any program in any language, it’s not equally easy to do so. Languages are the tools of the central activity of computer science The structure of language defines the boundaries of thought Motivation for and use of modern language facilities

Transcript of Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter...

Page 1: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 1 jcmt

CSE3302CSE3302Programming LanguagesProgramming Languages

Dr. Carter Tiernan

Page 2: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 2 jcmt

programming languageprogramming languagea language that is intended

for the expression of computer programs and is capable of expressing anyany

computer program

A is

Page 3: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 3 jcmt

Why is this interesting?Why is this interesting?• Although it’s possible to write any

program in any language, it’s not equally easy to do so.

• Languages are the tools of the central activity of computer science

• The structure of language defines the boundaries of thought

• Motivation for and use of modern language facilities

Page 4: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 4 jcmt

Why is this useful?Why is this useful?• Learning language mechanisms

can allow you to simulate such things even in a language that does not provide them

• Presents the most important principles for the design, evaluation, and implementation of programming languages

Page 5: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 5 jcmt

PrinciplesPrinciples• Open the front cover of your

textbook.• Yes, right now.

Page 6: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 6 jcmt

Early computingEarly computing• Numeric programming with coding of

instructions • A “pseudo-code” was a primitive

language that implemented the machine code operations with different, and hopefully easier to use, codes– interpreted or– (eventually) compiled

Page 7: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 7 jcmt

ConstraintsConstraints• Very slow• Very small memory• Computer time cost more than

programmer time• For numeric programming -

– Significant floating point calculations– Requirement for indexing data

Page 8: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 8 jcmt

What must a “pseudo-code” (or a What must a “pseudo-code” (or a programming language) do?programming language) do?

Originally based on what the actual Originally based on what the actual machine could do:machine could do:

• Floating point arithmetic and comparisons

• Indexing• Transfer of control• I/O

Page 9: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 9 jcmt

Decisions to makeDecisions to make(OK, just a few of them)(OK, just a few of them)

• Syntax?• How large can addresses be?• How long can instructions be?• How should we code the operations?• and so on…

Page 10: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 10 jcmt

The Principles (again)The Principles (again)• Identify elements most important

to good programming language design

• Apply with flexibility• Balance among contradictory

• Modeled on Strunk and White

Page 11: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 11 jcmt

Arithmetic vs. ComparisonArithmetic vs. Comparison• Calculate

values• Use operands• Values created

can be used or stored

• Test values• Use operands• Test results can

be used or stored

• Test results may control program flow

Page 12: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 12 jcmt

Moving, Indexing & LoopingMoving, Indexing & Looping[ Addresses ][ Addresses ]

• Moving – Put a value into a memory address

• Indexing– Access a single element from a

multiple element structure - an array• Looping

– Change the flow of the program back to a previous location

Page 13: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 13 jcmt

Input and OutputInput and Output• Based on I/O devices and methods

of the time– Punched cards– Paper tape– Keyboards

• {Not much to say about this one}

Page 14: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 14 jcmt

Program StructureProgram Structure• Declarations• Instructions• Input data

InterpreterInterpreterExecutionExecution

1. Read instruction2. Decode3. Execute4. Repeat steps with

next instruction

Page 15: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 15 jcmt

Interpreter enhancementsInterpreter enhancements• Program tracing

– Breakpoints– Data trap

• Labels for statements and variables– Beginning of symbol table

• Translation

Page 16: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 16 jcmt

Symbolic Pseudo CodeSymbolic Pseudo Code• Syntax• Punch cards led to fixed format

fields• Key punches only had upper case• VAR format• Prefix notation

Page 17: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 17 jcmt

PhenomenologyPhenomenology• Ampliative and Reductive• Fascination and Fear• Direct vs. Mediated (transparency)• Focus and Action

Page 18: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 18 jcmt

Efficiency : FortranEfficiency : Fortran• Inclusion of floating point arithmetic

and indexing in hardware exposed the overhead of interpreters

• John Backus of IBM recognized that language adoption would be based on :– Use of conventional mathematical notation– Highly efficient (machine) code produced

Page 19: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 19 jcmt

Adoption and Use of FortranAdoption and Use of Fortran• Development of a usable Fortran took 2 years

(18 person/years) of effort (starting in 1955)• Within a year and a half, approximately half the

code being written for the IBM 704 machines was in Fortran

• Why?– Exceptionally clear documentation– Very sophisticated optimization

techniques• Many versions exist and are in use• Book focuses on ANS Fortran IV (’66)

Page 20: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 20 jcmt

Fortran structureFortran structure• Subprograms

– Parameters– COMMON blocks

• Declarative or Imperative (nonexecutable vs. executable)– Allocate, bind and initialize at declaration– Compute, control flow, or I/O

• Stages and phases of compilation

Page 21: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 21 jcmt

Imperative statementsImperative statements• Assignment (=) is most important and

most common computational statement• Control structures developed to direct

control to primitive statements– Structures were based on IBM 704

branches– GOTO used to transfer control with IF for

selection and iteration– DO loop is only higher level construct

• Counted loop with CONTINUE• Can be nested hierarchically

Page 22: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 22 jcmt

Fortran Control IssuesFortran Control Issues• Static and dynamic structure hard to

see and correlate• Confusion of GOTO plethora• Weak typing• DO-loop is optimized with all needed

info for execution stated at the top• Procedural abstraction

– SUBROUTINE name (input & output parameters)

– FUNCTION name (input parameters)

Page 23: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 23 jcmt

Subroutines in FortranSubroutines in Fortran• CALL passes parameters and control• RETURN passes control back to caller• Allows modularity and encourages

libraries• Parameters passed by reference for

efficiency but with side effects– Compare with pass by value-result

• Activation records save state

Page 24: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 24 jcmt

Fortran: Activation RecordFortran: Activation Record• Nonrecursive subprogram invocation• Contains all data needed to activate

or restart a subprogram– Parameters– Instruction pointer (resumption address)– Dynamic link– Temporary storage

Page 25: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 25 jcmt

Fortran: Data StructuresFortran: Data Structures• Scalars - integers, floating point

– Integers, Hollerith constants (strings)– Double precision, complex, logical (boolean)

• Representation– Word-based– Appropriate to operations on the type

• Mathematical operations– Representation-independent– Overloaded for each type

Page 26: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 26 jcmt

Fortran: Data StructuresFortran: Data Structures• Arrays• Contiguous memory allocation for

indexing– Column-major order

• Easy to optimize– Loop controls used array address to start– Subscript format was restricted– Index register increment easy to

determine

A(1)A(2)A(3)

A(4)

Page 27: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 27 jcmt

Fortran: Data IssuesFortran: Data Issues• Overloaded operators• Integer type used for integers and

character strings (Hollerith constants)• No facilities for character

manipulation• Arrays are static

– Dimensions must be constants– Limited to 3 dimensions

Page 28: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 28 jcmt

Fortran: Name structuresFortran: Name structures• Declaration does binding• DATA statement does initialization• Static allocation• Fortran would automatically declare

previously-unseen variables– “I through N” names assumed as integer– Major typo problems created

• Variable names are local scope• Subprogram names are global scope

Page 29: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 29 jcmt

COMMON blocksCOMMON blocks• Data sharing between subprograms• Subprogram explicitly calls out the

COMMON block to be used• Aliasing

EQUIVALENCEEQUIVALENCE• Share memory within a subprogram

Page 30: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 30 jcmt

Language DefinitionLanguage Definition• Lexics

– The way characters are combined to form words and symbols

• Syntax– The way words and symbols are combined

to form statements and expressions• Lexical analysis (scanning)• Syntactic analysis (parsing)

Page 31: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 31 jcmt

Fortran syntaxFortran syntax• Fixed-format columns

– Free-format statement in columns 7 - 72• Ignored blanks (ugh!)• No “reserved” words• Allowed (quasi) algebraic notation• Operator precedence• Linear instruction sequence

Page 32: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 32 jcmt

First Generation LanguagesFirst Generation Languages• Machine dependence

– Especially seen in control structures– And in primitive data types supported

• Non-nested and rely on GOTO• One parameter passing mode• Definite loops supported but not recursion• Arrays• Weak typing• Static allocation and disjoint scoping of names• Linear organization

Page 33: Ch. 0 - 2 Ch. 0 - 2 1 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.

CSE 3302 CSE@UTA Programming Languages Ch. 0 - 2Ch. 0 - 2 33 jcmt

Language Design IssuesLanguage Design Issues• Interaction of features• “Higher level” construct allows

programmer to state what they want rather than how to do it

• “Regular” flow of control• “Cost” of choices, e.g. Fortran

efficiency