programming languages meeting 4 september 16/17, 2014
DESCRIPTION
Short Exam (2) – Inferences from the grammar Precedence of operators Associativity of operators Ambiguity of sentences – Construct one of the various semantic domains in a specific situationTRANSCRIPT
![Page 1: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/1.jpg)
Programming Languages
Meeting 4September 16/17, 2014
![Page 2: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/2.jpg)
Planning Ahead
• Short exam next time– Construct a grammar given a description of strings
in the language– Parse a sentence in a grammar– Describe a language defined by a grammar
![Page 3: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/3.jpg)
Short Exam (2)
– Inferences from the grammar• Precedence of operators• Associativity of operators• Ambiguity of sentences
– Construct one of the various semantic domains in a specific situation
![Page 4: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/4.jpg)
REF Results
Declarations followed by 1. A := RRF + B;
Ans: 42. B := RRE;
Ans: 13. RC := RRF + RD; Ans: Type mismatch (see slide Semantics(6) from last week).
![Page 5: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/5.jpg)
REF Results (2)
4. RRF : = RRE;Ans: Location of RC
5. RRE : = RD;Ans: Location of RD
![Page 6: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/6.jpg)
Multiple Assignments
Let’s now investigate statements of the form
ref1,ref2,…,refm := exp1,exp2,…,expn
What conditions should we put on m and n?
![Page 7: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/7.jpg)
MA (2)
m = 1, n = 1The ordinary assignment statement
A := 0;
(ref1 is A and the location it is mapped to; exp1 has value 0)
![Page 8: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/8.jpg)
MA (3)
m > 1, n = 1A,B,C := 0;
should represent the sequenceA := 0;B := 0;C := 0;
or any of the other 5 arrangements of it.(Why 5?)
![Page 9: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/9.jpg)
MA (4)
m = 2, n = 2
A,B = 0,1;A,B = X,Y;A,B = B,A;
If initially A:=9; B:=23; X:=7; Y:=12; what is the state after each of the statements above, accumulating the changes?
![Page 10: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/10.jpg)
MA (5)
More complexity:Suppose A has been declared to be an array of
size 10 of integers, all initialized to 0. Also j has been declared to be an integer and currently has value 4. What is the result of executing
A[j-1], j, A[j+1] := j+1;
Work with your partner to find several plausible answers.
![Page 11: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/11.jpg)
MA(6)
Case 1:Evaluate exp to obtain a value vfor i=1 to m step 1
evaluate refi to obtain loci
assign v to loci
In our case j becomes 5 and A is [0,0,5,0,0,5,0,0,0,0]
![Page 12: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/12.jpg)
MA (7)
Case 2:Evaluate exp to obtain a value vfor i=m to 1 step −1
evaluate refi to obtain loci
assign v to loci
In our case j becomes 5 and A is [0,0,0,5,5,0,0,0,0,0]
![Page 13: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/13.jpg)
MA (8)
Case 3:for i=1 to m step 1
evaluate refi to obtain loci
evaluate exp to obtain value v
assign v to loci
In our case j becomes 5 and A is [0,0,5,0,0,6,0,0,0,0]
![Page 14: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/14.jpg)
MA (9)
Case 4:for i=m to 1 step −1
evaluate refi to obtain loci
evaluate exp to obtain value v
assign v to loci
In our case j becomes 5 and A is [0,0,0,6,5,0,0,0,0,0]
![Page 15: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/15.jpg)
MA (10)
Case 5:for i=1 to m step 1
evaluate refi to obtain loci
evaluate exp to obtain value vfor i=1 to m step 1
assign v to loci
In our case j becomes 5 and A is [0,0,5,0,5,0,0,0,0,0]
![Page 16: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/16.jpg)
Control Structures
Start with the three fundamental structures and assume the existence of an empty statement (NOP)• Sequence S1;S2
• If-then-else if P then S1 else S2
• While-do while P do S
![Page 17: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/17.jpg)
CS Semantics
Use program functions, defined as followsLet P be a program (or program fragment)Let a be an identifier declared in P
representing valuesLet Va be the set of legal values for a as
determined by its type. Let Va* = Va + {undefined}
![Page 18: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/18.jpg)
CS Semantics (2)
Question: What are the possible outcomes after executing a program fragment?1. 2. 3. 4. 5.
![Page 19: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/19.jpg)
CS Semantics (3)
The program function for P is
[P]: Va* × Vb* × … −> Va* × Vb* × … + {error,
nonterminating}
defined to show the state after executing P.
![Page 20: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/20.jpg)
Example 1
P = X := X+3; Y:=Y−1 where X,Y are type integer
![Page 21: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/21.jpg)
Example 2
P = while Y > 0 do begin X := X−1; Y := Y−1 endwhere X,Y are type 0..maxint
![Page 22: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/22.jpg)
Example 3
P = if X <= Y then X := Y – X else X := 0 where X,Y are type integer
![Page 23: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/23.jpg)
Constructing a PF
Given a program P1. Find the set of identifiers: X, Y, Z, …2. Find the set of values VX* , … associated with
each identifier, which includes {undefined}3. Construct the function [P] with
domain: VX* × VY* × … codomain: VX* × VY* × …
+ {error, nonterminating}
![Page 24: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/24.jpg)
Constructing (2)
Use the following notational conventions:1. If an identifier in P does not appear as the
target of an assignment statement, it can be suppressed in the domain and codomain
2. [P] (error) = error [P] (nonterminating) = nonterminating
so are not written
![Page 25: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/25.jpg)
Constructing (3)
3. undefined is denoted by 4. The identifier and its value are denoted by
the same letter5. Account for all possible values of the domain,
perhaps by partitioning with conditions, e.g. x<0, x≥0, x =
![Page 26: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/26.jpg)
Example 4Program Function Exercise II.1
P = while x <= b do x := x + a end
![Page 27: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/27.jpg)
If-Then Semantics
Let P = if C then S1 else S2
Define [P](v) = [S1](v) if [C](v) = true [S2](v) if [C](v) =
false error if [C](v) =
error
![Page 28: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/28.jpg)
If-Then-Else (2)
Theorem:
[if C then S] = [if C then S else Φ] where [Φ] = id
![Page 29: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/29.jpg)
While-Do Semantics
Let P = while C do S
Define [P](v) = [S]k(v) where nonterminating error
![Page 30: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/30.jpg)
Sequence Semantics
Let P = S1 ; S2
Define [P](v) = [S2] o [S1](v)
![Page 31: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/31.jpg)
Böhm – Jacopini Theorem
The simple assignment statement and the three control structures sequence, if-then-else, while-do are enough• to write any block structured program• to compute any computable function• to convert any flowchart specified program to
code
![Page 32: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/32.jpg)
Additional Control Structures
Useful for human understanding of programsAnother example of “syntactic sugar”
Definition:[repeat S until C] = [S ; while (not C) do S]
![Page 33: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/33.jpg)
Additional (2)
Let P = case F of F1:S1; F2:S2; … ; FN:SN endDefinition[P] = [if F=F1 then S1 else if F=F2 then S2 else if … else if F=FN then SN]Issues: Two selector values with same statement What if F does not equal any FI? Can some SI be empty?
![Page 34: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/34.jpg)
Additional (3)
Let P = switch(E) {case E1:S1;case E2:S2; … ; case EN:SN; default: SD}Definition[P] = [ if E=E1 then S1;S2;…;SN;SD else if E=E2 then S2;…;SN;SD
else if E=E3 then S3;…;SN;SD
else SD]
![Page 35: Programming Languages Meeting 4 September 16/17, 2014](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b687f8b9ab0599b2129/html5/thumbnails/35.jpg)
Assignment
• Due at beginning of next class meeting. Work by yourself. Ask questions only of the instructor.
1. Program Function Exercise I.52. Program Function Exercise II.5and under the assumptions of Section III3. Program Function Exercise I.44. Program Function Exercise II.4