Exam 3

10/23/14
CSCI 2325
Principles of Programming Languages
Semantics
Reading: Ch 7 (Tucker & Noonan)
Exam 3
u 
Tuesday, November 4 (beginning of class)
u 
Topics:
u 
Names, types, semantics, memory management,
and function (up to Thursday’s lecture)
u 
Must read book chapters 4, 5, 7, 11, and 9 (9.7
only)
1
10/23/14
Sample questions
u 
Given a program, what will be the output if static scoping
is used? what will be its output if dynamic scoping is
used? (Ch 4)
u 
Difference between lifetime and scope. Give example.
(Ch 4)
u 
Static vs. dynamic typing – what is the difference? (Ch 5)
u 
How are arrays implemented? Extend 1-dimensional array
shown in class to 2-dimensional array. (Ch 5)
u 
What mechanisms allow functions to be types? (Ch 5)
u 
Given a statement, give a verbal description of its
semantics. (Ch 7)
u 
Simulate the three memory management algorithms on a
given example. (Ch 11)
u 
Given a recursive function, draw the activities in the runtime stack when the function is called. (Ch 9.7)
Semantics
Precisely describes the meaning of all language
constructs for:
1. 
Programmers
2. 
Compiler writers
3. 
Standards developers
2
10/23/14
Expression Semantics
Meaning of
u  Infix
(C, Java): a + b - c * d!
u  Polish
Prefix (Ambi): - + a b * c d!
u  Polish
!
Postfix (Postscript, calculator): !a b + c d * -!
u  Cambridge
!
Polish (LISP, Scheme): !(- (+ a b) (* c d))!
Associativity of Operators
Language
+-*/
Unary -
**
== != < ...
C-like
L
R
Ada
L
non
non
non
Fortran
L
R
R
L
u 
L
Semantics of: a < b < c in C
3
10/23/14
Precedence of Operators
Operators
C-like
Ada
Fortran
Unary -
7
3
3
5
5
**
*/
6
4
4
+-
5
3
3
== !=
4
2
2
< <= ...
3
2
2
not
7
2
2
Short Circuit Evaluation
u 
a and b evaluated as:
u 
u 
if a then b else false
a or b evaluated as:
u 
if a then true else b
4
10/23/14
Example
Node p = head;
while (p != null && p.info != key)
p = p.next;
if (p == null) // not in list
...
else // found it
...
!
Denotational Semantics
u 
Program state
u 
collection of all active objects and their current
values
5
10/23/14
// compute the factorial of n
1
2
void main ( ) {
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
!
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
undef undef undef
3
undef undef
6
10/23/14
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
3
3
undef undef
1
undef
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
3
3
1
1
undef
1
7
10/23/14
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
3
3
1
1
1
1
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
3
3
1
2
1
1
8
10/23/14
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
3
3
2
2
1
2
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
…
…
9
10/23/14
// compute the factorial
of n
1 void main ( ) {
2
int n, i, f;
3
n = 3;
4
i = 1;
5
f = 1;
6
while (i < n) {
7
i = i + 1;
8
f = f * i;
9
}
10 }
n
i
f
3
3
6
Exception Handling
Hardware vs. Programming Lang.
10