Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Operational Semantics ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Operational Semantics
Dr. Muhammed Al-Mulhem 2ICS Semantic Domains and State Transformation Semantic domain Semantic domain A semantic domain is any set whose properties and operations are independently well-understood and upon which the functions that define the semantics of a language are ultimately based, Examples: natural numbers, integers, booleans Examples: natural numbers, integers, booleans Useful semantic domains for programming languages are Useful semantic domains for programming languages are 1. Environment 2. Memory 3. Locations
Dr. Muhammed Al-Mulhem 3ICS Semantic Domains and State Transformation Suppose we have variables i and j with values 13 and -1 at memory locations 154 and 155. Suppose we have variables i and j with values 13 and -1 at memory locations 154 and 155. Environment Environment A set of pairs that unite specific variables with memory locations = {, } Memory [static and dynamic] Memory [static and dynamic] A set of pairs that unite specific locations with values = {, ….,, …} Locations Locations Locations are the natural numbers.
Dr. Muhammed Al-Mulhem 4ICS Semantic Domains and State Transformation State State The state of a program is the product of its environment and its memory Simplified form The state of a program is a set of pairs that represents all the active variables and their currently assigned values at some stage during the execution of the program. = {,, … } = {,, … } NOTE: Value of each variable is undefined before the program begins execution.
Dr. Muhammed Al-Mulhem 5ICS Semantic Domains and State Transformation Example of a state Example of a state = {,, } = {,, } Value retrieval function (var) Value retrieval function (var) (y) gives the value 12 (y) gives the value 12 State transformations represent the changes in state of a program due to the execution of program statements. State transformations represent the changes in state of a program due to the execution of program statements. State transformation can be represented mathematically by a special function called Overriding Union ( Ū ). State transformation can be represented mathematically by a special function called Overriding Union ( Ū ).
Dr. Muhammed Al-Mulhem 6ICS Semantic Domains and State Transformation Overriding union Ū between two sets of pairs Overriding union Ū between two sets of pairs X Ū Y = replace in X all pairs whose first member matches a pair from Y by and then add to X any remaining pairs in Y Ū is a formal and generalized model of assignment statement Example Example 1 = {,, } y = 2*w + 3; z = 14; 2 = {, } = 1 Ū 2 = {,,, }
Dr. Muhammed Al-Mulhem 7ICS Operational Semantics Operational semantics of a program provides a definition of program meaning by simulating the program’s behavior on a machine model Operational semantics of a program provides a definition of program meaning by simulating the program’s behavior on a machine model Structured operational semantics model use Structured operational semantics model use Rule-based approach Rule-based approach We developed this kind of operational semantics for Jay language. We developed this kind of operational semantics for Jay language.
Dr. Muhammed Al-Mulhem 8ICS Operational Semantics Operational semantics model uses the following notations: Operational semantics model uses the following notations: 1. Expression computation (e) v The computation of a value v from the expression e in state 2. Execution rule: It has the form If the premise is true then the conclusion is true. premise Conclusion
Dr. Muhammed Al-Mulhem 9ICS Operational Semantics Execution rule for addition Execution rule for addition (e 1 ) v 1 (e 2 ) v 2 : (e 1 + e 2 ) v 1 + v 2 Execution rule for an assignment statement Execution rule for an assignment statement s.target = s.source (s.source) v : (s.target = s.source;) Ū { } Execution rule for statement sequences s 1, s 2 Execution rule for statement sequences s 1, s 2 (s 1 ) 1 1 (s 2 ) 2 : (s 1, s 2 ) 2
Dr. Muhammed Al-Mulhem 10ICS Operational Semantics Execution rule for conditional statement Execution rule for conditional statement s = if (s.test) s.thenpart else s.elsepart (s.test) true (s.thenpart) 1 : (if(s.test) s.thenpart else s.elsepart) 1 (s.test) false (s.elsepart) 1 : (if(s.test) s.thenpart else s.elsepart) 1
Dr. Muhammed Al-Mulhem 11ICS Operational Semantics Execution rule for loops Execution rule for loops s = while (s.test) s.body; (s.test) true (s.body) 1 (while(s.test) s.body) 2 : (while(s.test) s.body) 2 (s.test) false : (while(s.test) s.body)
Dr. Muhammed Al-Mulhem 12ICS Examples - addition Suppose we have x = x + 1; and a current state in which the value of x is 5. That is Suppose we have x = x + 1; and a current state in which the value of x is 5. That is = { …,, …} = { …,, …} The operational semantics computes a new state as follows: The operational semantics computes a new state as follows: (x) 5 (1) 1 (x + 1) 6 (x = x + 1;) { …,, …} Ū {…, …} This transformed the state to = { …,, …}
Dr. Muhammed Al-Mulhem 13ICS Example - conditional If we have the state = { …,, …} If we have the state = { …,, …} And the following Jay statement And the following Jay statement If ( x<0 ) x = x -1; else x = x + 1; We apply the false rule We apply the false rule The rule concludes that the result of executing the conditional is the same as the result of executing x = x + 1; in state = { …,, …} leaving as final result the state 1 = { …,, …} The rule concludes that the result of executing the conditional is the same as the result of executing x = x + 1; in state = { …,, …} leaving as final result the state 1 = { …,, …}