Exam 2

11/1/15
CSCI 2325
Principles of Programming Languages
Semantics
Reading: Ch 7 (Tucker & Noonan)
Exam 2
u 
Wednesday, November 4
u 
Topics:
u 
Names, types, semantics, memory management,
and function (up to Monday’s lecture)
u 
Must read book chapters 4, 5, 7, 11, and 9 and go
over class notes and slides
1
11/1/15
Sample questions
u 
u 
u 
u 
u 
u 
u 
u 
u 
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)
Difference between lifetime and scope. Give example. (Ch
4)
Static vs. dynamic typing – what is the difference? (Ch 5)
What does a strongly typed language mean? (Ch 5)
How are arrays implemented? Extend 1-dimensional array
shown in class to 2-dimensional array. (Ch 5)
Are functions types? Give examples. (Ch 5)
Given a statement, give a verbal description of its
semantics. (Ch 7)
Simulate the three memory management algorithms on a
given example. (Ch 11)
Given a recursive function, draw the activities in the runtime stack when the function is called. (Ch 9.7)
Other questions from the topics covered during the classes.
Semantics
Precisely describes the meaning of all language
constructs for:
1. 
Programmers
2. 
Compiler writers
3. 
Standards developers
2
11/1/15
How to define semantics?
1. 
Operational semantics
u 
2. 
Axiomatic semantics
u 
3. 
u 
The meaning attached by compiling using compiler
C and executing using machine M. Ex: Fortran on
IBM 709.
Use mathematical logic
Denotational semantics
u 
Use mathematical functions
u 
These functions transform the “state” of the
program
We’ll use the concept of denotational
semantics informally
Expression Semantics
Meaning of
u  Infix
(C, Java): a + b - c * d
u  Ambiguous
u  Polish
without rules of prec. & assoc.
Prefix (Ambi): - + a b * c d
u  Unambiguous!
u  But
cannot use – as both unary and binary
u  Polish
Postfix (Postscript, calculator): ab+cd*-
u  Cambridge
Polish (LISP, Scheme): (- (+ a b) (* c d))
3
11/1/15
Associativity of Operators
Language
+-*/
Unary -
**
== != < ...
C-like
L
R
Ada
L
non
non
non
Fortran
L
R
R
L
u 
Semantics of: a < b < c in C/C++?
u 
u 
L
if (a < b) return 1 < c; else return 0 < c;
Why did Ada make relational operators nonassociative?
u 
To make a < b < c illegal
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
4
11/1/15
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
Example
Node p = head;
while (p != null && p.info != key)
p = p.next;
if (p == null) // not in list
...
else // found it
...
5
11/1/15
Question
u 
Is the meaning of
(a + b) + c the same as a + (b + c)?
u 
No!
u 
There are considerations beyond just
operators, operands, and types
u 
Hardware representation of numbers
Question
u 
What is the value of a below?
i = 2; b = 2; c = 5;
a = b * i++ + c * i;
u 
Here, the semantics of the RHS expression
above is undefined in C!
6
11/1/15
Assignment semantics
u 
u 
Allow multiple assignments?
u 
a=b=c=0
u 
OK by C/C++/Java/Python
Is assignment an expression?
u 
Why do I ask? If it’s an expression, you can use
assignment as a conditional expression!
Examples
u 
u 
C
u 
//Kernighan and Ritchie’s strcpy function
while (*p++ = *q++) ;
u 
while (ch = getc(fp)) ... // ???
u 
while (p = p->next) ... // ???
Java
boolean x = false;
if (x = true)
System.out.println("x is true!"); //this will be printed!
u 
Python
u 
These are illegal
7
11/1/15
Assignment:
copy vs. reference semantics
u 
x=y
Will the (R-)value of y be copied to x
or
will x and y point to the same data?
Denotational Semantics
u 
Program state
u 
collection of all active objects and their current
values (binding!)
8
11/1/15
// compute 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 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
9
11/1/15
// compute 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 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
10
11/1/15
// compute 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 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
11
11/1/15
// compute 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 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
…
…
12
11/1/15
// compute 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
Control flow semantics
u 
To be complete, an imperative language
needs:
• 
Statement sequencing
• 
Conditional statement
• 
Looping statement
13
11/1/15
Sequence of statements
u 
s1 s2
u 
Semantics:
u 
First execute s1
u 
Then execute s2
u 
Output state of s1 is the input state of s2
Conditional
u 
IfStatement → if ( Expresion ) Statement
[ else Statement ]
u 
u 
Example:
u 
if (a > b)
u 
u 
u 
u 
z = a;
else
If the test expression is
true (here, no change
of state)
u 
z = b;
u 
then the output state
of the conditional is
the output state of the
then branch,
else the output state of
the conditional is the
output state of the else
branch.
14
11/1/15
While loops
u 
WhileStatement → while (Expr) Statement
u 
The expression is evaluated (state does not
change)
u 
If it is true, first the statement is executed,
u 
and then the loop is executed again.
u 
Otherwise the loop terminates.
Exception Handling
Why?
15
11/1/15
Hardware vs. Programming Lang.
u 
u 
Hardware– Interrupt
u 
Your program: division by 0/illegal memory addr à
Interrupt handler routine in HW à
Back to the next line of your program
u 
Only one interrupt handler routine for div by 0
u 
Resumption model
Programming Lang. – Exception
u 
Your program: array index out of bound/access
object using NULL pointer à
Corresponding exception handler à
Back to a specific part of your program
u 
You can have multiple exception handlers for the
same exception
u 
Termination model
History of exception handling
u 
COBOL à PL/I à Ada à C++/Java…
u 
No exception handling: Fortran/Pascal/C
u 
Programmer’s responsibility
16
11/1/15
Resumption (HW) vs.
termination (PL)
Exception in C++
try
{
//throw 20;
//throw “Some error”;
}
catch(int a)
{
//This kicks in if an int is thrown.
}
catch(char* st)
{
//This kicks in if a string is thrown.
}
17
11/1/15
Exception in Java
Code by Jakob Jenkov
try {
// execute code that may throw
// 1 of the 3 exceptions below.
}
catch(SQLException e) {
logger.log(e);
}
catch(IOException e) {
logger.log(e);
}
catch(Exception e) {
logger.severe(e);
}
u 
Java’s exception hierarchy
18
11/1/15
Python’s
exception
hierarchy
19