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

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 11 Introduction to Programming in C
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Chapter 15 Debugging. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Debugging with High Level Languages.
Chapter 8 Improving the User Interface
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
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 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Ismael: “Surely all this is not.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CIS Computer Programming Logic
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Instructor - C. BoyleFall Semester
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
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.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Syntax (2).
Semantics (1).
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
By Mr. Muhammad Pervez Akhtar
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Fundamentals of Programming Most.
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.
Bill Tucker Austin Community College COSC 1315
Information and Computer Sciences University of Hawaii, Manoa
Chapter 3 Selection Statements
Chapter 4: Control Structures I (Selection)
Expressions and Assignment
A mechanism for deciding whether an action should be taken
Chapter 5: Loops and Files.
Chapter 4: Control Structures I (Selection)
Introduction to Programming
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Programming Languages 2nd edition Tucker and Noonan
Introduction to Object-Oriented Programming with Java--Wu
Programming Funamental slides
Programming Languages 2nd edition Tucker and Noonan
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
COMS 261 Computer Science I
Repetition Structures
The Fundamentals of C++
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without meaning. Ishmael, Moby Dick by Herman Melville

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: The meaning attached by compiling using compiler C and executing using machine M. Ex: Fortran on IBM 709. Axiomatize statements -- Chapter 12 Statements as state transforming functions This chapter uses an informal, operational model.

Copyright © 2006 The McGraw-Hill Companies, Inc. 7.2 Expression Semantics

Copyright © 2006 The McGraw-Hill Companies, Inc. (a + b) - (c * d) Polish Prefix: - + a b * c d Polish Postfix: a b + c d * - Cambridge Polish: (- (+ a b) (* c d)) Infix uses associativity and precedence to disambiguate.

Copyright © 2006 The McGraw-Hill Companies, Inc. Associativity of Operators Language+ - * /Unary -**== != <... C-likeLRL AdaLnonnonnon FortranLRRL Meaning of: a < b < c

Copyright © 2006 The McGraw-Hill Companies, Inc. Precedence of Operators OperatorsC-likeAdaFortran Unary -733 **55 * / == !=422 < <= not722

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. Example Node p = head; while (p != null && p.info != key) p = p.next; if (p == null) // not in list... else // found it...

Copyright © 2006 The McGraw-Hill Companies, Inc. Versus boolean found = false; while (p != null && ! found) { if (p.info == key) found = true; else p = p.next; }

Copyright © 2006 The McGraw-Hill Companies, Inc. Side Effect A change to any non-local variable or I/O. What is the value of: i = 2; b = 2; c = 5; a = b * i++ + c * i;

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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 }

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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 } nif311311nif311311

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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 } nif311321nif311321

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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 } nif322321nif322321

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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 nif

Copyright © 2006 The McGraw-Hill Companies, Inc. // 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 } nif336336nif336336

Copyright © 2006 The McGraw-Hill Companies, Inc. 7.4 Assignment Semantics Issues Multiple assignment Assignment statement vs. expression Copy vs. reference semantics

Copyright © 2006 The McGraw-Hill Companies, Inc. Multiple Assignment Example: a = b = c = 0; Sets all 3 variables to zero. Problems???

Copyright © 2006 The McGraw-Hill Companies, Inc. 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->next)... // ???

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 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); }

Copyright © 2006 The McGraw-Hill Companies, Inc. 7.5 Control Flow Semantics To be complete, an imperative language needs: Statement sequencing Conditional statement Looping statement

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. Conditional IfStatement if ( Expresion ) Statement [ else Statement ] Example: if (a > b) z = a; else z = b;

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 7.6 Input/Output Semantics Binding: open, close Access: sequential vs. random Stream vs. fixed length records Character vs. binary Format

Copyright © 2006 The McGraw-Hill Companies, Inc. Standard Files Unix: stdin, stdout, stderr C: stdin, stdout, stderr C++: cin, cout, cerr Java: System.in, System.out, System.err

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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