Programming Languages Tucker and Noonan - PowerPoint PPT Presentation

About This Presentation
Title:

Programming Languages Tucker and Noonan

Description:

Title: Programming Languages Author: Allen Tucker Robert Noonan Last modified by: jhan Document presentation format: On-screen Show Company: Bowdoin College; College ... – PowerPoint PPT presentation

Number of Views:317
Avg rating:3.0/5.0
Slides: 45
Provided by: AllenTuck4
Learn more at: https://csc.csudh.edu
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages Tucker and Noonan


1
Programming LanguagesTucker and Noonan
  • Chapter 7 Semantics
  • 7.1 Motivation
  • 7.2 Expression Semantics
  • 7.3 Program State
  • 7.4 Assignment Semantics
  • 7.5 Control Flow Semantics
  • 7.6 Input/Output Semantics
  • 7.7 Exception Handling Semantics

2
Motivation
  • To provide an authoritative definition of the
    meaning of all language constructs for
  • Programmers
  • Compiler writers
  • Standards developers
  • A programming language is complete only when its
    syntax, type system, and semantics are
    well-defined.

3

Semantics
  • Semantics is a precise definition of the meaning
    of a syntactically and type-wise correct program.
  • Three approaches
  • Operational semantics
  • The meaning attached by compiling using compiler
    C and executing using machine M. Ex Fortran on
    IBM 709.
  • Direct and straightforward program meaning
  • Axiomatic semantics
  • Axiomatize the meaning of statements -- Chapter
    12
  • Exploration of formal properties of programs
  • Denotational semantics
  • Statements as state transforming functions
  • High-level mathematical precision of program
    meaning
  • This chapter uses an informal, operational model.

4
Expression Semantics
  • Notation Expression tree
  • Meaning
  • Mathematics (a b) - (c d)
  • Polish prefix notation
  • - a b c d
  • Preorder traversal
  • Polish postfix notation
  • a b c d -
  • Postorder traversal
  • Cambridge Polish
  • (- ( a b) ( c d))
  • Operator precedes operands
  • Parentheses

5
Infix Notation
  • Mathematics meaning
  • (a b) - (c d)
  • Uses associativity and precedence to disambiguate
  • Associativity of Operators
  • Language - / Unary -
    ! lt ...
  • C-like Left Right Left
  • Ada Left non non non
  • Fortran Left Right Right Left
  • Meaning of a lt x lt b in C-like
  • a lt x x lt b ?
  • If (a lt x) 1 lt b else 0 lt b

6
Precedence of Operators
  • Operators C-like Ada Fortran
  • Unary - 7 3 3
  • 5 5
  • / 6 4 4
  • - 5 3 3
  • ! 4 2 2
  • lt lt ... 3 2 2
  • Unary not 7 2 2
  • Logical and 2 1 1
  • Logical or 1 1 1

7
Short Circuit Evaluation
  • a and b evaluated as
  • if a then b else false
  • a or b evaluated as
  • if a then true else b
  • Benefits
  • Efficiency
  • Shorter code
  • Clearer code
  • Example in C-like
  • x lt Math.pow(y, 3) b bad?
  • xlt1 (yy gt 100 ygtx) good?

8
More Example
  • Node p head
  • while (p ! null p.info ! key) p p.next
  • while (p ! null ! found)
  • if (p.info key) break
  • else p p.next
  • // using break
  • boolean found false
  • while (p ! null ! found)
  • if (p.info key) found true
  • else p p.next
  • // avoiding break

9
Expression Meaning
  • Number precision
  • data type size
  • (ab)c a(bc) ?
  • Side effect
  • A change to any non-local variable or I/O.
  • What is the value of
  • i 2 b 2 c 5
  • x b i c i // 19
  • y c i b i // 14
  • Consider
  • y x
  • y x1

10
Program State
  • The state of a program is the collection of all
    active objects and their current values.
  • Two maps
  • Active objects to specific memory locations
  • Active memory locations to specific values.
  • State memory ? environment
  • 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.
  • For simplicity, ignore the memory location

11
Program State Transformation
Program compute the factorial of n
State Transformation
  • 1 void main ( )
  • 2 int n, i, f
  • 3 n 3
  • 4 i 1
  • 5 f 1
  • 6 while (i lt n)
  • 7 i i 1
  • 8 f f i
  • 9
  • 10
  • n i f
  • undef undef undef
  • 3 undef undef
  • 3 1 undef
  • 3, 3, 3 1, 2, 3 1, 2, 6
  • 3, 3 1, 2 1, 2
  • 3, 3 2, 3 1, 2
  • 3, 3 3, 3 2, 6
  • 3 3 6

12
Assignment Semantics
  • Issues
  • Multiple assignment
  • Example
  • a b c 0
  • Sets all 3 variables to zero.
  • Problems???
  • Assignment statement vs. expression
  • Copy vs. reference semantics

13
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) ... // an error
  • while (p q) // strcpy
  • while (ch getc(fp)) ... // ???
  • while (p p-gtnext) ... // ???

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

15
Control Flow Semantics
  • Turing complete
  • a programming language is Turing Complete if its
    program are capable of computing any computable
    function
  • To be complete, an imperative language needs
  • Statement sequencing
  • Conditional statement
  • Looping statement

16
Sequence
  • Format
  • 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

17
Conditional
  • Format
  • IfStatement ? if ( Expression ) Statement
    else Statement
  • Semantics
  • If the value of Expression is true, the meaning
    is the state resulting from evaluating Statement1
    in the current state. Otherwise the meaning is
    the state resulting from evaluating Statement2 in
    the current state
  • Example
  • if (a gt b)
  • z a
  • else
  • z b

18
Various Conditional Statements
  • if ltEgt then ltSgt if ltEgt is true then execute ltSgt
  • if ltEgt then ltSgt else ltS2gt if ltEgt is true then
    execute ltSgt, otherwise execute ltS2gt
  • Dangling-else Ambiguity. Ways to avoid ambiguity
  • Syntax the else-clause associated with the
    closest then e.g. C, Java
  • if ltE1gt then if ltE2gt then ltS1gt else ltS2gt
  • Use compound brackets
  • A if ltE1gt then if ltE2gt then ltS1gt else ltS2gt
  • B if ltE1gt then if ltE2gt then ltS1gt else ltS2gt
  • Avoid use of nesting in then parts
  • A if !ltE1gt then ltS2gt else if ltE2gt then ltS1gt
  • B if ltE1gt ltE2gt then ltS1gt else if ltE1gt then
    ltS2gt

19
Multiple Branches
  • Multiple branches case (selection) use the
    value of an expression to select one of several
    substatements for execution
  • Pascal, Modula-2 case x of
  • ltvalue1gt ltstatements1gt
  • ltvalue2gt ltstatements2gt
  • else ltstatementsgt
  • end
  • Ada case x is
  • when ltvalue1gt gt ltstatements1gt
  • when ltvalue2gt gt ltstatements2gt
  • others gt ltstatementsgt
  • C/C, Java Notice the use of break
  • switch (x)
  • case ltvalue1gt ltstatements1gt
  • break
  • case ltvalue2gt ltstatements2gt
  • break

20
Multiple Branches
  • Points
  • Cases can appear in any order, but
    else/others/default must be the last one
  • The case value of x is discrete, such as 1, 10,
    6, 9
  • The case values must be distinct
  • Some languages allow ranges of case values, such
    as in Modula-2, 0..9 represents the value
    either 0, or 1, , or 9 of x
  • Case can be replaced with ifthenelse statements
  • if x ltvalue1gt then ltstatements1gt
  • else if x ltvalue2gt then ltstatements2gt
  • else ltstatementsgt

21
Loops
  • Format
  • WhileStatement ? while ( Expression ) Statement
  • Semantics
  • The expression is evaluated.
  • If it is true, first the statement is executed,
  • and then the loop is executed again.
  • Otherwise the loop terminates.

22
General Loop Structure
  • General structure
  • Do S1
  • If C exit
  • Otherwise Do S2
  • Repeat

23
Loop Implementation
General loop
While loop
Repeat loop
24
For Loop Implementation
  • For statement definite iteration for each
    element do
  • General form
  • for (ltvargt ltinitialgt ltstepgt ltterminationgt)
    do statements
  • ltvargt varies from the initial value to the
    termination, each loop varies with a stepwise
  • Step can be omitted, default value is 1
  • Stepwise can be going either up from small
    initial to large termination, or down from large
    initial to small termination
  • Pascal
  • for x low to high do S
  • for x high downto low do S
  • Fortran/Algol
  • for x start step increment until end do S
  • increment may be negative to implement going
    down
  • Ada
  • for x in low..high loop S end loop
  • for x in reverse low..high loop S end loop
  • C/C/Java
  • for (e1 e2 e3) S

25
For Loop Implementation
  • Some languages allow the stepwise to be real or
    other data types
  • Fortran/Algol allow real steps such as 0.1, 0.2
  • Pascal/Modula-2 allow step type of enumeration
  • for x in d do S where d is a type of enumeration
  • Perl allows enumeration in a list
  • foreach x (_at_list) S
  • Java allows enumeration in a collection
  • Syntax
  • foreach (Type variable collection) statement
  • Example
  • String names
  • foreach (String name names)
  • do something with name

26
Jump Statements
  • Goto jump to a specific statement (labeled by a
    label). Recall jmp, jp in assembly languages
  • makes the program difficult to trace, hard to
    prove
  • violates the structured programming style
  • destroy the system if goto the kernel of the OS
  • Break (C/C, Java) limited use of Goto as an
    exit of a loop or selection
  • Used inside a loop exit the loop
  • Used inside a selection (case) exit the case
  • Exit in Ada and Modula-2
  • Continue(C/C, Java, Fortran) limited use of
    Goto
  • Begin the next loop, ignore the statements after
    it in the body

27
7.6 Input/Output Semantics
  • Binding open, close
  • Access sequential vs. random
  • Stream vs. fixed length records
  • Character vs. binary
  • Format
  • I/O Error handling

28
Standard Files
  • Unix stdin, stdout, stderr
  • C stdin, stdout, stderr
  • C cin, cout, cerr
  • Java System.in, System.out, System.err

29
Input/Output Streams
  • Fortran
  • integer i, a(8)
  • write(8,) Enter 8 integers
  • read(,) a
  • write(,) a
  • Java
  • file, pipe, memory, url
  • filter
  • reader, writer

30
Formats
  • C
  • Codes d, e, f, c, s (decimal. float, float,
    char, string)
  • Specifier opt-width code
  • Ex s 5d 20s 8.2f
  • Fortran
  • Codes i, f, a (integer, float, string)
  • Specifier op-repeat code width
  • Ex 8i4, f8.2, a20

31
Exception Handling Semantics
  • Exception
  • an error condition occurring from an operation
    that cannot be resolved by the operation itself
  • Purposes
  • Simplify programming
  • Make applications more robust continue to
    operate under all concevable error situations

32
Exception Handling Basic Structure
  • Exception events abnormal
  • event that the system can not deal with so that
    leave to the programmer, such as
  • division by zero,
  • get value from a null pointer
  • read a file that doesnt exist
  • Exception handlers
  • Deal with the exception events, such as
  • output error
  • use default value
  • stop the execution

33
Exception Handling ProgramStructure
  • Ada
  • begin
  • exception
  • when ltexception1gt gt exception1-handler
  • when ltexception2gt gt exception2-handler
  • .
  • end

C/Java try . catch (ltexception1gt)
exception1-handler catch (ltexception2gt)
exception2-handler finally .
34
Raise/Throw/Catch Exceptions
  • Raise/throw exception
  • Implicitly throw built-in method/function throw,
    e.g. division, file access
  • Explicitly throw
  • throw ltexceptiongt --- C/Java
  • raise ltexceptiongt --- Ada
  • Catch exception
  • If there exists a catch clause to catch a
    specific exception, the exception is handled by
    the corresponding handler. If no handler exists,
    the exception is raised/thrown to the caller. To
    implement this, the throws must be specified in
    the method prototype (signature). E.g. in Java
  • public void method_sample() throws ltexceptiongt
  • throw ltexceptiongt

35
Throwing an Exception Figure 7.13
36
Exception Post-process
  • Termination model
  • C/Java, Ada only enter the exception handler
    and execute the finally clause, if any.
  • Resumption model
  • Eiffel can use the retry statement to re-execute
    the procedure/function in which the exception
    occurred

37
Exception Handling Figure 7.9
38
Declare Exceptions
  • Predefined exceptions (built-in exception)
  • User-defined exception e.g. in Java
  • Public class MyException extends
    RuntimeException

Creating a New Exception Class
39
Java Exception Type Hierarchy Figure 7.10
40
Missing Argument Exception
41
Invalid Input Exception Figure 7.12
42
AssertException Class
43
Assert Class Figure 7.15
44
Using Asserts
Write a Comment
User Comments (0)
About PowerShow.com