Chapter 7 - Semantics. The meaning of a program is defined by its semantics. That is, when a program is run, the effect of each statement on the values of the variables in the program is given by the semantics of the language. Thus, when we write a program, we must understand such basic ideas as the exact effect that an assignment has on the program's variables. We study semantics in Chapter 7. | Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without meaning. Ishmael, Moby Dick by Herman Melville Contents Motivation Expression Semantics Program State Assignment Semantics Control Flow Semantics Input/Output Semantics Exception Handling Semantics 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. 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. Expression Semantics (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. Associativity of Operators Language + - * / Unary - ** == != < . C-like L R L Ada L non non non Fortran L R R L Meaning of: a < b < c Precedence of Operators Operators C-like Ada Fortran Unary - 7 3 3 ** 5 5 * / 6 4 4 + - 5 3 3 == != 4 2 2 < <= . 3 2 2 not 7 2 2 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 Example Node p = head; while (p != null && != key) p = ; if (p == null) // not in list . else // found it . Versus boolean found = false; while (p != null && ! found) { if ( == key) found = true; else p = ; } 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; Program State The state of a program is the collection of all active objects and their current values. Maps: The pairing of . | Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without meaning. Ishmael, Moby Dick by Herman Melville Contents Motivation Expression Semantics Program State Assignment Semantics Control Flow Semantics Input/Output Semantics Exception Handling Semantics 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. 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. Expression Semantics (a + b) - (c * d) Polish Prefix: - + a b