Title: Semantics
1Semantics
Ishmael Surely all this is not without
meaning. Herman Melville, Moby Dick
2Outline
- Motivation
- Expression Semantics
- Program State
- Assignment Semantics
- Control Flow Semantics
- I/O Semantics
- Exceptions Handling Semantics
3Assignment Semantics
- Abstract Syntax
- Multiple assignment
- Assignment statement vs. expression
- Copy vs. reference semantics
- Abstract Syntax
- Assignment Variable target Expression source
4Multiple Assignment
- Example
- a b c 0
- Sets all 3 variables to zero.
5Assignment Statement vs. Expression
- In most languages, assignment is a statement
cannot appear in an expression. - In C-like languages, assignment is an expression.
- Examples
- if (a 0) ... // an error?
- while (p q) // strcpy
- while (ch getc(fp)) // read until EOF
- while (p p-gtnext) // traverse list
6Copy vs. Reference Semantics
- Copy a b
- a and b have same value.
- Changes to either have no effect on other.
- Used in imperative languages.
- Reference
- a and b point to the same object.
- A change in object state affects both
- Used by many object-oriented languages.
7Control Flow Semantics
- Turing Completeness A programming language is
Turing complete if its programs are capable of
computing any computable function. - A language is Turing complete if it has
- Assignment statement
- Statement sequencing
- Conditional statement
- Looping statement
Control structures
8Sequence
- 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
- Branching statements
- return
- break
- continue
- goto
9Conditional
- IfStatement ? if ( Expresion ) Statement else
Statement - Example
if (a gt b) z a else z b
10 Conditional
- 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.
11Loops
- 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.
12GoTo
- Widely used in early programming languages
- Controversy stated with ACM letter by Dijkstra
- Started the push for structured programming
- Structure of the program gives a clear guide to
what it does
13GoTo
- Example from Fortran
- Sums non-negative numbers in an array
14GoTo
- Most modern languages dont have goto
- Reduced reliance on flowchart as a program design
tool - Increased focus on structured programming
techniques - Many languages do have restricted form of goto
- return, break, continue
15Input/Output Semantics
- Relatively straight forward
- Reading Assignment
16Exceptions
- Unexpected or unusual condition that arises
during the program execution - Can not be resolved in the context of the
operation - Moves error checking out of the normal flow of
the program - Example
- Division by zero
- Causes a hardware interrupt
17Error Handling Techniques
- Ostrich algorithm
- Ignore the problem
- OK for small personal programs not commercial
code - Use assert
- Program terminates if the assertion is false
- Abort the program
- Graceful exit Appearance that program worked OK
- Not OK for mission critical applications
18Exceptions
- Throw
- Signal that an exception has occurred
- Catch
- Control has been passed to an exception handler
- Example (division by zero)
- Hardware interrupt routine
- Prints a message and halts the program
19Exceptions
- Occurs at many levels of abstraction
- Hardware Level
- Division by zero
- Floating Point overflow/underflow
- Programming Language Level
- Array index out of bounds
- Read values of wrong types
- Access to a null pointer
- Applications Level
- Pop an empty stack
20Exception HandlingÂ
- Mechanism that allows a program to capture and
handle the exceptions thrown in your program. - If exception handling does not exist, the program
will quit when an exception occurs.
21Exception HandlingWhy?
- Simplify programmers task of developing more
robust applications - Robustness
- Recovering from errors in a reasonable or
graceful way - Production quality software systems must to be
robust - Part of engineering systems
- Need to be fault tolerant
22Exception HandlingWhy?
- Catch errors before they occur
- Can deal with synchronous errors (e.g. division
by zero) - Can not deal with asynchnonous errors (disk I/O
falut) - Useful when program can not recover must
gracefully terminate - Improve fault-tolerance
- Write error processing code
- Specify the types of exceptions to be caught
23Exception HandlingWhy?
- Should not be used for program control
- Not optimized
- May have a negative impact on the performance
24Exception Handling
- Early languages did not provide for exception
handling - Pioneered in PL/1
- ON condition
- statement
- Statement executed only when condition occurs
- Most languages since Ada have adapted some
exception handling mechanisms
25Exception Handling
- Modern languages make the exception handler
lexically bound - PL/1 had dynamic binding of the handlers
- Exceptions are propagated
- If not handled by the current block it is moved
to a parent block - Using the dynamic link of the stack
26Exception Handling
- Java uses lexically scoped exception handlers
- try
- int anew int12
- a4
- catch (ArrayIntexOutofBoundsException e)
- System.out.println(exception
e.getMessage()) - e.printStackTrace()
-
- Dynamic chain is made available through
printStackTrace
27Exceptions
- PL/1 exceptions did not have a type
- Ada Type exception
- Java Classes
- Hierarchy of exceptions
28Exception Handling
- Unchecked Exceptions
- Any descendent of Error or RuntimeException
- Serious error programmer can do little to fix it
- Checked Exceptions
- Any other descendent of Throwable/Exception
- Error programmer knows how to deal with it
29Announcement
- No Class Monday (9/18)
- Midterm Exam Wednesday (9/20)
- Chapters 1-7
30Exception HandlingImplementation
- Linked Lists
- Dynamically bound handlers
- Maintained at run time
- Slow must be updated for each block of code
- Compile-time table of blocks and handlers
- Table of two fields
- Starting address of block and address of the
handler - Locate the handler using a binary search
- Logarithmic in the number of handlers
31Exception HandlingJava
- Each subroutine has a separate exception handling
table - Each stack frame contains a pointer to the table
32Exception Handling Figure 6.1
33Java Exception Type Hierarchy Figure 6.2
34Creating a New Exception Class Figure 6.3
35Missing Argument Exception Figure 6.4
36Invalid Input Exception Figure 6.5
37StackUnderflowException Class Figure 6.6
38Throwing an Exception Figure 6.7
39AssertException Class Figure 6.8
40Assert Class Figure 6.9
41Using Asserts Figure 6.10