Semantics - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Semantics

Description:

In most languages, assignment is a statement; cannot appear in an ... Ostrich algorithm. Ignore the problem. OK for small personal programs not commercial code ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 30
Provided by: sam73
Category:

less

Transcript and Presenter's Notes

Title: Semantics


1
Semantics
Ishmael Surely all this is not without
meaning. Herman Melville, Moby Dick
2
Outline
  • Motivation
  • Expression Semantics
  • Program State
  • Assignment Semantics
  • Control Flow Semantics
  • I/O Semantics
  • Exceptions Handling Semantics

3
Assignment Semantics
  • Abstract Syntax
  • Multiple assignment
  • Assignment statement vs. expression
  • Copy vs. reference semantics
  • Abstract Syntax
  • Assignment Variable target Expression source

4
Multiple Assignment
  • Example
  • a b c 0
  • Sets all 3 variables to zero.

5
Assignment 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

6
Copy 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.

7
Control 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
8
Sequence
  • 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

9
Conditional
  • 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.

11
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.

12
GoTo
  • 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

13
GoTo
  • Example from Fortran
  • Sums non-negative numbers in an array

14
GoTo
  • 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

15
Input/Output Semantics
  • Relatively straight forward
  • Reading Assignment

16
Exceptions
  • 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

17
Error 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

18
Exceptions
  • 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

19
Exceptions
  • 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

20
Exception 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.

21
Exception 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

22
Exception 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

23
Exception HandlingWhy?
  • Should not be used for program control
  • Not optimized
  • May have a negative impact on the performance

24
Exception 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

25
Exception 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

26
Exception 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

27
Exceptions
  • PL/1 exceptions did not have a type
  • Ada Type exception
  • Java Classes
  • Hierarchy of exceptions

28
Exception 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

29
Announcement
  • No Class Monday (9/18)
  • Midterm Exam Wednesday (9/20)
  • Chapters 1-7

30
Exception 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

31
Exception HandlingJava
  • Each subroutine has a separate exception handling
    table
  • Each stack frame contains a pointer to the table

32
Exception Handling Figure 6.1
33
Java Exception Type Hierarchy Figure 6.2
34
Creating a New Exception Class Figure 6.3
35
Missing Argument Exception Figure 6.4
36
Invalid Input Exception Figure 6.5
37
StackUnderflowException Class Figure 6.6
38
Throwing an Exception Figure 6.7
39
AssertException Class Figure 6.8
40
Assert Class Figure 6.9
41
Using Asserts Figure 6.10
Write a Comment
User Comments (0)
About PowerShow.com