UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Advertisements

Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
CSC321: Programming Languages7-1 Programming Languages Tucker and Noonan Chapter 7: Semantics 7.1 Motivation 7.2 Expression Semantics 7.3 Program State.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Lecture 1 The Basics (Review of Familiar Topics).
Chapter 8 . Sequence Control
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Ismael: “Surely all this is not.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Fundamentals of Python: From First Programs Through Data Structures
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Fundamentals of Python: First Programs
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Imperative Programming
High-Level Programming Languages: C++
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
Expressions and Assignment Statements
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Semantics (1).
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
By Mr. Muhammad Pervez Akhtar
Chapter Seven: Expressions and Assignment Statements Lesson 07.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Information and Computer Sciences University of Hawaii, Manoa
Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment
CS2403 Programming Languages Expressions and Assignment Statements
Representation, Syntax, Paradigms, Types
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Java Programming: From Problem Analysis to Program Design, 4e
Control Structures – Selection
Chapter 4: Control Structures I (Selection)
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Chapter 2: Basic Elements of Java
Programming Funamental slides
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Chapter 7 Expressions and Assignment Statements.
Representation, Syntax, Paradigms, Types
Chap 7. Advanced Control Statements in Java
C++ for Engineers and Scientists Second Edition
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco Valtorta Surely all this is not without meaning. Ishmael in Moby Dick by Herman Melville

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Contents 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

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 7.1 Motivation To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards developers A programming language is complete only when its syntax, type system, and semantics are well- defined.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Semantics is a precise definition of the meaning of a syntactically and type-wise correct program Ideas of meaning: –Operational Semantics: The meaning attached by compiling using compiler C and executing using machine M. Ex: Fortran on IBM 709; C using SimpleSem –Axiomatic Semantics: Chapter 18 –Denotational Semantics: Statements as state transforming functions This chapter uses an informal, operational model.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 7.2 Expression Semantics Infix notation: (a + b) - (c * d) Polish prefix notation: - + a b * c d –generated by preorder traversal of the expression tree Polish postfix notation: a b + c d * - –generated by postorder traversal of the expression tree –good for stacks Cambridge prefix notation: (- (+ a b) (* c d)) –LISP-like –allows use of the same operator with different arity Infix uses associativity and precedence to disambiguate: a + b – c * d

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Associativity of Operators Language+ - * / Unary- ** == != <... C-like L R not appl L Ada L non non non Fortran L R R L Meaning of: a < b < c in C: –if (a < x) 1 < b else 0 < b

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Precedence of Operators Operators C-like AdaFortran Unary ** not applicable 5 5 * / == != < <= Not Logical and Logical or 1 1 1

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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 Node p = head; while (p != null && p.info != key) p = p.next; if (p == null) // not in list... else // found it... boolean found = false; while (p != null && ! found) { if (p.info == key) found = true; else p = p.next; } while (p != null) { if (p.info == key) break; p = p.next; }

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Addition of Integers Is Not Associative Recall that addition of floats is not associative If a is the largest possible positive integer, b is 3, and c is -5, then (a + b) + c is undefined (or leads to an error) a + (b + c) evaluates to the largest integer minus 2 So, (a + b) + c != a + (b + c)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Side Effect A change to any variable or I/O. C expressions may have side effects: they are not referentially transparent. What is the value of: i = 2; b = 2; c = 5; a = b * i++ + c * i; 14 if i is incremented after its second reference 19 if i is incremented before its second reference In C, the above expression is undefined

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 7.3 Program State The state of a program is the collection of all active objects and their current values. It maps: 1.the pairing of active objects with specific memory locations (the environment function) 2.the pairing of active memory locations with their current values (the memory function) 3.So: state = memory x 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 the purposes of this chapter, use only a map from a variable to its value; like a debugger watch window, tied to a particular statement.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 }

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif undefundefundef 3undef undef

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif 3undef undef 3 1 undef

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif 31undef 311

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif 311

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering // compute the factorial of 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 } nif

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 7.4 Assignment Semantics Issues Multiple assignment Assignment statement vs. expression Copy vs. reference semantics

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Multiple Assignment Example: a = b = c = 0; Sets all 3 variables to zero Commonly used for initializations

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Assignment Command vs. Expression In most languages, assignment is a command; cannot appear in an expression. In C-like languages, assignment is an expression. –Example: if (a = 0)... // most likely an error –while (*p++ = *q++) ; // strcpy –while (ch = getc(fp))... // ??? –while (p = p->next)... // ???

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering public void add (Object word, Object number) { Vector set = (Vector) dict.get(word); if (set == null) { // not in Concordance set = new Vector( ); dict.put(word, set); } if (allowDupl || !set.contains(number)) set.addElement(number); } Under copy semantics, the last statement would not update the concordance properly

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 7.5 Control Flow Semantics To be complete, an imperative language needs: Statement sequencing Conditional statement Looping statement This is the Bohm-Jacopini theorem (1966): C. Bohm and G. Jacopini. “Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules.” Communications of the ACM, 9, 5 (May 1966),

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Conditional IfStatement  if ( Expression ) Statement [ else Statement ] Example: if (a > b) z = a; else z = b;

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 7.6 Input/Output Semantics Binding: open, close Access: sequential vs. random Stream vs. fixed length records Character vs. binary Format

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Standard Files Unix: stdin, stdout, stderr C: stdin, stdout, stderr C++: cin, cout, cerr Java: System.in, System.out, System.err

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Contents 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

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Purpose Simplify programming Make applications more robust. What does robust mean?

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering (* Pascal - what can go wrong *) reset(file, name); (* open *) sum := 0.0; count := 0; while (not eof(file)) do begin read(file, number); sum := sum + number; count := count + 1; end; ave := sum / count;

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Exception Handling Figure 7.9

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering #include int main () { char A[10]; cin >> n; try { for (int i=0; i<n; i++){ if (i>9) throw "array index error"; A[i]=getchar(); } catch (char* s) { cout << "Exception: " << s << endl; } return 0; }

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Java Exception Type Hierarchy Figure 7.10

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Creating a New Exception Class

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Missing Argument Exception

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Invalid Input Exception Figure 7.12

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering StackUnderflowException Class Figure 7.13

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Throwing an Exception Figure 7.13

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering AssertException Class

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Assert Class Figure 7.15

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Asserts