Principles of Programming Languages
Total Page:16
File Type:pdf, Size:1020Kb
Principles of Programming Languages Lecture 02 Criteria for Language Design C SC 520 Principles of Programming Languages ↓ cut ↓ ↓ cut ↓ hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 1 | | + + Criteria for Language Design 1. Simplicity — mnemonic — clear easily mastered semantics — as few basic concepts as possible — feature/concepts limited enough to master entire language (discourages "dialecting") — effects of feature combinations easily predictable — simple rules of combination ex: (1) PL/I: default coercion rules among fixed bin, fixed dec, float bin, float dec, when modified by scale attributes, become very complex. Each rule is reasonable by itself—the combination yields strange results. + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 2 | | + + dcl M fixed dec(10,5), N fixed bin(5,4); N=0; M=N+.1; expr attr. repn. val. .1 dec(1,1) 0.1 (dec) 1/10 N+.1 bin(5,4) 0.0001|100110011 .. 1/16 (binary conversion, then truncation) M dec(10,5) 00000.06250 (dec) 1/16 (2) ALGOL 60: — own static — array dynamic size (known at block entry) ex: own boolean array B[M:N]; — created on first entry to block — retained between entries to block (with values at block exit) — seemingly could vary in size — conflicts with stack implementation — meaning? + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 3 | | + + (3) PASCAL: fairly simple (4) ADA: Entia non sint multiplicanda praeter necessitatem —William of Ockham — procedure calls: keyword or positional for actual/formal correspondence — but positional parameters must occur first, and once a keyword is used, rest of the call must use keyword parms REORDER_KEYS(NUM_OF_ITEMS, KEY_ARRAY :=: RESULT_TABLE); + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 4 | | + + 2. Well-defined Syntactic/Semantic Description g syntax — not a problem to specify once designed g semantics — big problem; still often informal — ALGOL 68 Report (1968) — Revised Report on ALGOL 68 (1975) — Informal Introduction to ALGOL 68, Lindsey & Van der Meulen (1977) g Chapter 0: Very Informal Introduction to ALGOL 68 g Techniques — Interpretive (operational): PL/I (VDL, Wegner, 1972) — Axiomatic: PASCAL (Hoare-Wirth, 1971) — Denotational: Scheme (Steele & Sussman, 1978) + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 5 | | + + ex: (1) ALGOL 60: Lack of complete syntax and semantics (especially I/O) harmed adoption (2) PASCAL: forward left out of grammar; demands definition before use but makes exceptions for recursive types (3) PASCAL: When are types equivalent? The Report is ambiguous type t = array[1..100] of real; var a, b: array[1..100] of real; c:array[1..2] of t; d:array[1..2] of array[1..100] of real; e:t; f:array[1..100] of real; g:t; — structural equivalence: equivalent if have same type structure g {a, b, e, f , g} : array[1..100] of real; g {c, d} : array[1..2] of array[1..100] of real; + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 6 | | + + — declaration equivalence: variables type compatible ⇔ have same type name (built-in or user-defined) or appear in the same declaration (ex: PASCAL) g {a, b} (same declaration) g { f } (distinct declaration) g {e, g} (both of type t) g {c}, {d} (distinct declaration) — name equivalence: variables type compatible ⇔ have same type name (built-in or user-defined) (ex: ADA) g only {e, g} (both of type t) g all others inequivalent (4) DO loops in early FORTRAN: is the index value available after transfer out? + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 7 | | + + (5) ALGOL 60: Semantics of assignments From the Algol 60 Report: ‘‘4.2.3. Semantics [of assignment statement] Assignment statements serve for assigning the value of an expression to one or several variables ... The process will in the general case be understood to take place in three steps as follows: 4.2.3.1. Any subscript expressions occurring in the left part variables are evaluated in sequence from left to right. 4.2.3.2. The expression of the statement is evaluated. 4.2.3.3. The value of the expression is assigned to all the left part variables, with any subscript expressions having values as evaluated in step 4.2.3.1.’’ begin integer a; integer procedure f(x, y); value y,x; integer y,x; a:=f:=x+1; integer procedure g(x); integer x; x:=g:=a+2; a := 0; outreal(1, a + (f(a,g(a)) / g(a)) ) end Many possible evaluations of: a + ( f(a,g(a)) / g(a) ) + + | | a + ( f(a,g(a)) / g(a) ) + a / g(x) by name: x:=g:=a+2 fg f(x,y) by value: a g a a:=f:=x+1 a main: a:=0; print expr C SC 520 Principles of Programming Languages (a) Eval denom. 1st, then numer. L-R 31 g(x) by name: 34+= 22 + x:=g:=a+2 8 f(x,y) by value: f 3 = a=3 g 2 a:=f:=x+1 a / 7 6 main: f:=3 a:=0; a:=3 g:=2 print expr fg1 a:=2 5 g:=4 a:=4 a=2 a=0 2a g 4 a 0 a=2 3a C SC 520 Principles of Programming Languages (b) Eval standard topological sort (L-R) 11 g(x) by name: 0 += 33 + x:=g:=a+2 8 f 1 f(x,y) by value: = a=0 g 3 a:=f:=x+1 a / 0 7 main: f:=1 a:=0; a:=1 g:=3 print expr fg6 a:=3 4 g:=2 a:=2 a=0 a=1 1a g 3 a 5 a=0 2a (c) Other possible results (11 total) 3354 3 1 3 1 1 33537 5223 5 3 5 2 2 C SC 520 Principles of Programming Languages Another Example: • Bad: A[ a + B[f(a)] + g(a) ] := C[a] := 0; • Algol examples from D.E. Knuth, ``The Remaining Trouble Spots in ALGOL 60’’, CACM 1967. C SC 520 Principles of Programming Languages hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 12 | | + + (6) C, C++: Semantics of expressions & assignments a = f(i); a += g(i); a = f(i) + g(i); NOT equivalent. Result of one is known; result of the other is unknown. (7) C++: There are 6 different kinds of scope: — block (local) scope: names declared in a block & formal parameters defined in any enclosed block/function (except if redeclared) — function scope: labels defined throughout function in which declared — function prototype scope: names in the parameter list extend to the end of prototype definition — file (global) scope: name declared outside all blocks/classes is defined throughout the translation unit + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 13 | | + + — class scope: class member name n is local to its class C and can be used only (1) as n in another member function of class C (2) as c.n where c is an object of class C (or a derived class of C) (3) as pc->n where pc is a pointer to an object of class C (or a derived class of C) (4) as X::n (scope resolution operator) where X is C (or a derived class of C) — namespace scope: names defined only inside the namespace (a named collection of classes) or ofoutside using scope resolution operator :: (namespaces support multivendor libraries with potential name conflicts) namespace std { #include <iostream.h> #include <math.h> } namespace SOFTinc { class stack ( ... }; class queue { ... }; } // here use std::<< using namespace std; // here use << for std::<< using namespace SOFTinc; // here use stack for SOFTinc::stack + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 14 | | + + 3. Reliability/Safety — Reliability: syntactic errors not easy to introduce and discouraged (related to readability) — Safety: semantic errors detectable, preferably at compile time ex: (1) ALGOL 60: missing ; after comment absorbs next statement ⇒ strange errors (2) PL/I: FOO: PROC OPTIONS(MAIN); DCL ... ON ENDFILE(SYSIN) BEGIN; . END; (forgot ). DO I=1 BY 1; . (forgot END;) END FOO; Last END creates end for FOO and any other enclosed structures with forgotton ENDs—with no warning! + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 15 | | + + (3) PASCAL: ; is a separator, not a terminator as in PL/I, ADA PASCAL (separator bad): if C then S 1 else S 2 ; S 3 ; {error without begin S 2 ;S3 end ; } ADA (terminator good): if C then S 1 else S 2 ; S 3 ; (* ok *) end if ; g [Ripley & Druseikis, Computer Languages 3 (1978) 227-240] [Gannon, CACM 20 (1977) 584-595] — 20% of all PASCAL syntax errors are ; terminator errors — 7.5% are missing begin , end + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 16 | | + + (4) Use unique delimiters for different constructs ADA: if end if ; for end loop; case end case; ALGOL 68: do od if fi case esac ?!: comment tnemmoc + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 17 | | + + (5) PASCAL variant records: type compatibility cannot be checked at compile time type scale = (large, huge) ; measurement = record unit : string ; case size : scale of large : (exact : integer) ; huge : (approx : real) end ; var dist : measurement ; _______ unit→ |_______| size→ |_______|(tag field) exact, approx→ |_______| (∗) dist.exact is meaningful ⇔ dist.size = large dist.approx is meaningful ⇔ dist.size = huge — Programmer can alter dist.size — Cannot enforce (∗) at compile time — To enforce at runtime every reference to a variant field must check the tag field value (large or huge). Never done in practice. + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 18 | | + + (6) C, C++: == vs. = if (n = 0) exception(); else norm(); (7) C, C++: Implicit pointer conversions char* mem; void* gen_ptr; gen_ptr = mem; // C and C++ mem = gen_ptr; // assignt. compatible in C // illegal in C++ (8) Comments in C++ // FILE: rat.h // ... class Rat { public: // CONSTRUCTORS Rat(int n = 0, int d = 1); // default constructor: Rat r // Rat(5) ==> 5/1 Rat(const Rat& r); // copy constructor Rat(double d); // init by a double value // DESTRUCTOR ˜Rat(){ // nothing to do } + + | | hhhhhhh hhhhhhh 1/7/106 AT&T FOIL 19 | | + + // SELECTORS & CONST MEMBER FUNCTIONS int numer() const { return num; } double decimal() const { return double(num)/double(den); } int denom() const { return den; } int sign() const { if (num < 0) return -1; else return 1 // ..