Download presentation
Presentation is loading. Please wait.
Published byShonda Porter Modified over 8 years ago
1
Semantics(1)
2
2 Symantec(1) 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
3
3 Symantec(2) Semantics is a precise definition of the meaning of a syntactically and type-wise correct program
4
4 Approaches to define Semantics (1) Operational Semantics Whatever happened when the program compiled is compiled by a compiler C and run on a machine M Operational definition Abstract Syntax Interpreter
5
5 Approaches to define Semantics (2) Axiomatic Semantics Ascertain about the relationships that remain the same each time the program executes The axiomatic formula: {P} c {Q} c : command or control structure P : pre-conditions Q : post-conditions
6
6 Denotation Semantics Denotation definition Abstract syntax Semantic algebra defining a computational model Valuation functions The valuation functions map the syntactic constructs to semantic algebra The meaning can be expressed as a collection of functions operating on a program state Approaches to define Semantics (3)
7
7 Expression Semantics Expression Semantics includes Operators Operators associativity and precedence Role of different evaluation order Importance of precision
8
8 Infix Notation Binary operator is written between its operands Ambiguous expression Associativity and Precedence is one way to eliminate the ambiguity
9
9 Eliminating infix Expression Ambiguity(1) Polish prefix notations Binary operator is written in front of the two operands Semantics is inherently unambiguous Prefix can be generated by using a prefix walk (preorder traversal) Limitation: some symbol cannot be used for an operation with deferent number of operands - is cannot be used for both unary and binary minus One solution using different symbols Example: the infix: a+b-c*d the prefix: -+ab*cd
10
10 Eliminating infix Expression Ambiguity(2) Polish postfix notations Binary operator follows the two operands Semantics is inherently unambiguous postfix can be generated by using a postfix walk (postorder traversal) Example: the infix: a+b-c*d the postfix: ab+cd*-
11
11 Expression Sort-Circuit Evaluation (1) Evaluating a Boolean expression from left to right and stop as soon as the truth of the expression can be determined. The short-circuit definition of A and B if A then B else false The short-circuit definition of A or B if A then true else B
12
12 Example 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.inf == key) break; P=p.next; }
13
13 The Meaning of an Expression Meaning of expression should depend only on the values of its sub-expressions and the meaning of its operator Fixed size representation of numbers in computer makes numbers have smallest and largest values Mathematics has unlimited values
14
14 Example: (a+b)+c ≠ a+(b+c) for some values of a, b, c Assume: a = largest possible integer b = 3 c =-5 Left-hand side= error (integer overflow) Right-hand side = largest integer -2
15
15 Example a is semantically undefined a = 14 Increment i after the 2 nd referencing a=19 Increment i before the 2 nd referencing In many languages sub-expression in separate sub-trees may be evaluated in any order i = 2; b = 2; c=5; a = b * i++ + c * i
16
16 Program State(1) The state of a program is the collection of all active objects and their current values. Maps: The pairing of active objects with specific memory locations, and the pairing of active memory locations with their current values
17
17 Program State(2) 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
18
18 Example Suppose variables i and j have the values 13 and -1 at some time during the execution i and j are associated with memory locations 154 and 155 at that time The current state: Environment {i,154, j,155} Memory {0,undef, … 154,13, 155,-1, …}
19
19 Program State(3) The state of a program is a product of its active objects, Their memory locations, and Associative values Function composed of two maps environment and memory State = environment x memory State is like watch widow
20
20 Assignment Semantics Assignment = Variable target; Expression source The semantic is as follows Source expression is evaluated in the current state, resulting in a value The resulting value replaces the value of the target variable, resulting in a new state
21
21 Assignment Semantics(1) 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))...
22
22 Assignment Semantics(2) 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.