CS450 Classnotes (PDFDrive)
CS450 Classnotes (PDFDrive)
Raphael Finkel
1 Intro
Lecture 1, 1/10/2019
1. Handout 1 — My names
2. Plagiarism — read aloud
3. E-mail list: [email protected]
4. Assignments on web. First assignment — Fortran
5. Accounts in MultiLab
6. Text (Sebesta, 10th edition) — we will follow somewhat
2 Software tools
Use (client) Programmer
Spec Language
Implementation Compiler
3 Fortran by examples
examples.f
1
CS450G Spring 2019 2
5 Java Puzzlers
4. cost
5. portability
6. generality (but beware of the Ada syndrome of over-complexity)
7. well-definedness (syntax is easy to specify, but semantics is harder)
8. But: a designer often has to trade one criterion for another.
7 MacLennan’s principles
A related set of of principles is given by MacLennan slide , with principles
such as
1. Labelling: Do not require the programmer to know the absolute po-
sition of an item in a list.
2. Structure: The static structure of the program should correspond in
a simple way to the dynamic structure of the corresponding compu-
tations.
1. Lecture 3, 1/17/2019
2. procedural: procedure calls with parameters, return values
(a) imperative (Fortran, Algol, Pascal, C): Variables hold values and
have scope. Control structures based on statements, including
sequences, assignments, compound statements, loops, proce-
dure calls, exception handling.
i. object-oriented (Java, C++, C#): imperative, with data and
associated procedures organized in hierarchical classes.
ii. visual (Visual BASIC, .NET languages): drag-and-drop gen-
eration of code, easy generation of GUIs.
iii. scripting (Perl, Python, Ruby): string manipulation, invok-
ing programs and manipulating results.
iv. web-oriented (JavaScript, PHP, JSP): creating and manipu-
lating document content.
(b) functional (Lisp, ML): There are no variables, but there are named
read-only parameters and possibly named constants. Control
structures are based on expressions, high-order functions, and
a heavy use of recursion.
3. declarative (or rule-based or logic) (Prolog, lparse, aspps, CP): rules
with conditions and consequences; predicates
4. text-oriented (HTML, XML, TeX, nroff): not programming languages,
but might have macros and nested structures.
CS450G Spring 2019 5
Program in slow
compiler byte code interpreter load−decode−execute
source language P−code
machine−independent
just−in−time
compiler
libraries
(a) syntax: line oriented: 3 lines per statement (one for types, one
for subscripts)
(b) data: bits, integer, floating-point, arrays, records (nested)
(c) control: for, multi-level break, if (without else)
(d) assertions
CS450G Spring 2019 6
(a) Sebesta thinks these languages did not contribute to the main
line of development of programming languages.
(b) syntax: one line per operation, with symbols instead of opcodes
and addresses + labelling
(c) macros (typically for subroutine linkage)
4. Lecture 4, 1/22/2019
5. Pseudocodes
9. Cobol 60
11 Syntax: Grammars
1. Grammars are a formal way to define the syntax of a programming
language, which means how a program is composed, and the forms
of its components, independent of their meaning.
2. Most syntax descriptions use BNF (Backus-Naur Form) or some vari-
ant; this formalism was introduced around 1960 for Algol-60.
CS450G Spring 2019 8
(d) Earley’s algorithm (Jay Earley, 1970) can parse in O(n3 ) for am-
biguous grammars and O(n2 ) for unambiguous grammars.
(e) Actual programming languages are more restrictive (in partic-
ular, they need very little lookahead), allowing O(n) parsers.
13 Pascal by examples
1. Lecture 6, 1/29/2019
14 Formal semantics
1. Lecture 7, 1/31/2019
2. The semantics of a programming language describes what programs
mean, that is, what they do when running, as opposed to how they
look.
3. Three ways of approaching semantics
15 Operational semantics
1. Basic idea: translate programs (or statements) into a simpler inter-
mediate language with its own interpreter.
2. Levels of use
(a) Natural: See the final result of executing the whole program.
(b) Structural: Inspect the translation of single components (such
as statements)
6. Lecture 8, 2/5/2019
7. Axiom of selection (if statements)
{B ∧ P} S1 {Q}, {¬B ∧ P} S2 {Q} `
{P} if B then S1 else S2 {Q}
8. Axiom of iteration (while statements)
{B ∧ I} S {I} `
{I} while B do S {¬B ∧ I}
but no guarantee of completion.
9. Extended example: factorial
1 {true}
2 {1 = 1!}
3 count := 1;
4 {1 = count!}
5 answer := 1;
6 {answer = count!}
7 while count != n do
8 {answer = count!}
9 count := count + 1;
10 {answer = (count-1)!}
11 answer := answer * count;
12 {answer = count!}
13 end;
14 {answer = count! ∧ count = n}
15 {answer = n!}
4. Evaluation
(a) The semantic domains onto which one maps programs are re-
cursively defined and therefore mathematically suspect.
(b) It is very awkward (much harder than Sebesta indicates) to cap-
ture indefinite iteration (while loops).
(c) Complete denotational descriptions cover all erroneous cases
(at a terrible cost to readability), specifying exactly what an er-
roneous program means.
(d) Denotational semantics is of little use to programmers.
(e) One can try to automatically convert a denotational description
of a language into a compiler.
CS450G Spring 2019 15
(a) Predefined names, like int in Pascal, are not reserved, but it is
foolish to redefine them.
(a) Attributes
i. address: (static, often as offset from start of a frame). Also
called the L-value of the variable. Can refer to multiple
adjacent addresses, which together we call a memory cell.
If two variables access the same address, they are aliases.
This situation is error-prone.
ii. value (dynamic): contents of the addressed cell. also called
the R-value of the variable.
iii. type (usually static): set of values that can be stored in the
address and how those values are interpreted.
iv. lifetime (dynamic)
v. scope (usually static)
20 Smalltalk by examples
Lecture 11, 2/14/2019
Lecture 12, 2/19/2019
2. Static variables
3. Stack-dynamic variables
(a) Usually stored on a single stack, which we call the central stack,
but there can be multiple stacks (for concurrency).
CS450G Spring 2019 19
22 Type checking
1. Types serve several purposes.
(a) The compiler can allocate the right amount of space + automation
(b) The compiler can generate correct code. + impossible error
(c) Programming errors can often be detected as type violations.
+ defense in depth However, not all type errors can be caught.
i. If we use integers to represent colors, we might multiply the
integers, although multiplying the colors is meaningless.
ii. If we store both distance and time in reals, division makes
sense (we get velocity), but not addition. My work on di-
mensions tries to remedy this problem.
(a) Every value has a type. Expressions have values, and proce-
dures and labels are also values, albeit second or third class (to
be defined later).
(b) Assignment and formal-actual bindings are restricted to com-
patible types, introducing type conversions if necessary.
(c) All type errors can be detected, typically statically.
CS450G Spring 2019 21
23 Type equivalence
1. The compiler must reject any assignment or parameter binding with
incompatible types.
2. Types are compatible if they are equivalent or if the language is will-
ing to coerce the R-value to a type equivalent to the L-value’s type.
3. When are types equivalent?
(a) Name equivalence (Pascal, Ada, Java): The types have the same
name, or can be traced back to the same name.
i. A type generator (type constructor) like array, record,
pointerTo, or derived creates a new internal type name.
ii. Strict (Ada): a declaration of multiple variables is a short-
hand for multiple declarations; any type generator in the
declaration is therefore expanded to multiple (different) types.
iii. Lax (declaration equivalence: Pascal): a declaration of mul-
tiple variables shares any type generator among the vari-
ables.
(b) Structural equivalence (Ada unconstrained arrays, Modula-3):
The types have the same memory layout.
i. Strict: arrays have the same bounds, same subscript type;
record fields have the same names, records are not flattened.
ii. Can be implemented inexpensively by a combination of compile-
time effort (compute canonical representation and hash it)
and run-time effort (compare actual hash with expected hash).
CS450G Spring 2019 22
Midterm, 2/28/2019
25 Scope
1. Lecture 15, 3/5/2019
2. The scope of an identifier is the collection of statements that can ac-
cess that identifier. An identifier is a name, which could refer to a
constant, type, procedure, label, or variable.
3. Static scope: The scope of an identifier is based on where the state-
ments are in the source program. Also called lexical scope.
(a) Very common, including Fortran and all Algol derivatives.
CS450G Spring 2019 23
2. real
3. complex
4. Boolean
5. character
28 Strings
1. Length restrictions
5. Storage organization
29 Enumeration types
1. (Pascal, C, Java) + labelling + impossible error
2. Comparable, discrete.
3. How to define I/O?
4. Convertible to integer?
5. Overloaded enumeration literals? Ada: yes, resolvable.
30 Subtypes
1. A subtype is a type with (more) constraints placed on its values.
2. Members of the subtype inherit all operations of the base type.
3. Examples
31 Arrays
An array is an indexed sequence of values.
1. Notation: the index is of the subscript type, and the values are of the
element type.
CS450G Spring 2019 28
2. Homogeneity