CmSc 315 Programming languages
Chapter 7 . Semantics
7.3 Program State
The state of a program is the collection of all active objects and their current values.
Maps:
1.
The pairing of active objects with specific memory locations,
2.
and the pairing of active memory locations with
their current values.
The current statement (portion of an abstract syntax tree) to be executed in a program is interpreted relative to the current state.
The individual steps that occur during a program run can be viewed as a series of state transformations.
Here: use only a map from a variable to its value; like a debugger watch window, tied to a particular statement.
Example: tracing a program
7.4 Assignment Semantics
Issues
•
Multiple assignment
•
Assignment statement vs. expression
• Copy vs. reference semantics
Multiple Assignment
Example: a = b = c = 0;
Sets all 3 variables to zero.
Assignment Statement vs. Expression
•
In most languages, assignment is a statement; cannot appear in an expression.
• In C-like languages, assignment is an expression.
–
Example: if (a = 0) ...
1
Copy vs. Reference Semantics
•
Copy: a = b;
– a, b have same value.
– Changes to either have no effect on other.
–
Used in imperative languages.
•
Reference
– a, b point to the same object.
–
A change in object state affects both
–
Used by many object-oriented languages.
7.5 Control Flow Semantics
To be complete, an imperative language needs:
• Statement sequencing
•
Conditional statement
•
Looping statement
Forms of statement-level control
Composition
– Statements are executed in the order they appear in the source code.
Alternation – Two sequences form alternatives so one sequence or the other sequence is executed but not both. (conditionals)
Iteration
– A sequence of statements that are executed repeatedly.
Explicit Sequence Control goto X if Y goto X – transfer control to the statement labeled X if Y is true. break
Sequence - composition s1 s2
Semantics: in the absence of a branch:
• First execute s1
•
Then execute s2
•
Output state of s1 is the input state of s2
Conditional
IfStatement
if ( Expresion ) Statement
[ else Statement ]
2
Example: if (a > b) z = a; else z = b;
If the test expression is true, 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.
Loops
WhileStatement
while ( Expression ) Statement
The expression is evaluated.
If it is true, first the statement is executed, and then the loop is executed again.
Otherwise the loop terminates.
3
4
5
Structured programming design
(1) Hierarchical design of program structures
(2) Representation of hierarchical design directly in the program text using "structured" control statements.
(3) The textual sequence corresponds to the execution sequence
(4) Use of single-purpose groups of statements
4.3. Structured control statements
6
Problems with structured sequence control:
Multiple exit loops
Exceptional conditions
Do-while-do structure
Solutions vary with languages, e.g. in C++ - break statement, assert for exceptions.
Prime programs
Theory of prime programs - a consistent theory of control structures
Consider 3 classes of flowchart nodes:
Any flowchart is a graph of directed arcs and these 3 types of nodes
A proper program is a flowchart with:
•1 entry arc
•1 exit arc
•There is a path from entry arc to any node to exit arc
A prime program is a proper program which has no embedded proper subprogram of greater than 1 node. (i.e., cannot cut 2 arcs to extract a prime subprogram within it).
A composite program is a proper program that is not prime.
7
Every proper program can be decomposed into a hierarchical set of prime subprograms. This decomposition is unique (except for special case of linear sequences of function nodes).
All primes can be enumerated.
Question: Can any prime program be built out of structure control statements?
The answer is given by the structure theorem :
Any flowchart can be represented using only if statements, while statements and sequence control statements
8
Primes of up to 4 nodes
9