Download presentation
Presentation is loading. Please wait.
1
Concepts of Programming Languages
Lecturer: Dr Emad Nabil Lecture #9 Dynamic Semantic Ch3 Copyright © 2012 Pearson Education. All rights reserved.
2
Denotational semantic
Static Semantic Dynamic Semantic Operational semantic Denotational semantic Axiomatic semantic Copyright © 2012 Pearson Education. All rights reserved.
3
Denotational Semantics
Denotational semantics is the most rigorous (strict) and most widely known formal method for describing the meaning of programs. Based on recursive function theory The most abstract semantics description method Originally developed by Scott and Strachey (1970) Dana Scott Christopher S. Strachey
4
Denotational Semantics
The meaning of language constructs are defined by only the values of the program's variables
5
Denotational Semantics
* In denotational semantics, mathematical objects are used to represent the meanings of language constructs. * Language entities are converted to these mathematical objects with recursive functions are converted by Recursion Language entities mathematical objects
6
Denotational Semantics
The process of building a denotational specification for a language: - Define a mathematical object for each language entity Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects Copyright © 2012 Addison-Wesley. All rights reserved.
7
Denotational Semantics: program state
The state of a program is the values of all its current variables s = {<i1, v1>, <i2, v2>, …, <in, vn>} Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(ij, s) = vj Math. Map function Var. name (lang. entity) value state Copyright © 2012 Addison-Wesley. All rights reserved.
8
Example: Binary Numbers
syntactic objects Semantic function Mbin objects in N, nonnegative decimal numbers ex 110 The semantic function, named Mbin, maps the syntactic objects, as described in the previous grammar rules, to the objects in N, N is the set of nonnegative decimal numbers. Copyright © 2012 Pearson Education. All rights reserved.
9
syntax-directed semantics.
Syntax Rules The semantic function Mbin syntax-directed semantics.
10
Example2: Decimal Numbers
The syntax rules: <dec_num> '0' |'1' |'2' |'3' |'4' |'5' |'6' | '7' | '8' | '9' | <dec_num> '0' | <dec_num> '1' | ……………. <dec_num> ‘9’ Semantic rules Mdec ('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9 Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>) Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1 … Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9 Copyright © 2012 Addison-Wesley. All rights reserved.
12
Expressions Suppose that the only error we consider in expressions is a variable having an undefined value. Map expressions onto Z {error}: Z is the set in integers We assume expressions are decimal integer numbers, variables, or binary expressions having one arithmetic operator( * or + ) and two operands, each of which can be an expression
13
Syntax Rules for Expr. VARMAP(ij, s) = vj
Mdec(<dec_num>, s)= value ∆= to define mathematical functions => to connect an operand to its associated switch (case) A=123; A=B; A=B+C; Semantic Rules for Expr. The return value is:Z ∪ error. Z is the set of int numbers Copyright © 2012 Pearson Education. All rights reserved.
14
s = {<i1, v1>, <i2, v2>, …, <in, vn>}
* An assignment statement is an expression evaluation plus the setting of the target variable to the expression’s value * the meaning function maps a state to a state. Syntax rules <assign> X = E X A|B|C Semantic rules Returns the new state s’ The comparison here for names, not values s = {<i1, v1>, <i2, v2>, …, <in, vn>} Me(E,s)=value VARMAP(ij, s) = vj Copyright © 2012 Pearson Education. All rights reserved.
15
Logical Pretest Loops In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions - Recursion, when compared to iteration, is easier to describe with mathematical rigor Copyright © 2012 Addison-Wesley. All rights reserved.
16
Logical Pretest Loops <while> while B do L
Semantic rules Syntax rules <while> while B do L we assume that there are two other existing mapping functions: The meaning of the loop is simply the value of the program variables after the statements in the loop have been executed the prescribed number of times. Msl: map statement lists and states to states (or error) Mb: map Boolean expressions to Boolean values (or error) The return value is : s ∪ error.
17
Do-while in java public static void main(String args[]) { }
boolean b; int x=5; do x+=1; }while (b); } error: b is not initialized Copyright © 2012 Pearson Education. All rights reserved.
18
Do-Until Msl: map statement lists and states to states (or error)
Mb: map Boolean expressions to Boolean values (or error) Maps state sets to state sets U {error} Mr(Do L until B,S) ∆= if Mb(B, s) = error then error else if Msl(L, S) = error else s’=Msl(L, s) if Mb(B, s’) = True then S’ else Mr( Do L until B, S’) The return value is : s ∪ error.
19
Do-While in java Msl: map statement lists and states to states (or error) Mb: map Boolean expressions to Boolean values (or error) Maps state sets to state sets U {error} Mr(Do L until B,S) ∆= if Mb(B, s) = error then error else if Msl(L, S) = error else s’=Msl(L, s) if Mb(B, s’) = False then S’ else Mr(Do L until B, S’) The return value is : s ∪ error.
20
Java Boolean Expression
Example of B If(x==5||y==6) Mb(B, s)∆= if VARMAP(i, s)=undef for some i in B then error else B', where B' is the result of evaluating B after setting each variable i in B to VARMAP(i, s) Mb: map Boolean expressions to Boolean values (T or F) (or error) Copyright © 2012 Pearson Education. All rights reserved.
21
Mfor(for (<assign1> ; <boolean_expr>; <assign2>) {<stmts>}, S) Assume the semantics mapping functions Ma, Mb, Me, are Msl given. Mfor(for (<assign1> ; <boolean_expr>; <assign2>) L, S) ∆= If Ma (<assign1>, S) = error then error else S’ = Ma (<assign1>, S) if Mb(<Boolean_expr>, S’) = error then error else if Mb(<Boolean_expr>, S’) = false then S’ else if Msl( L , S’) = error then error else S’’ = Msl( L, S’) if Ma(<assign2>, S’’) = error then error else S’’’ = Ma(<assign2>, S’’) MforHelp(for(<boolean_expr>; <assign2>) L , S’’’) MforHelp(for (<boolean_expr>; <assign2>) L, S) ∆= if Mb(<Boolean_expr>, S) = false then S else S’ = Msl( L, S) S’’ = Ma(<assign2>, S’) MforHelp(for(<boolean_expr>; <assign2>) L , S’’) For loop in Java Copyright © 2012 Pearson Education. All rights reserved.
22
Evaluation of Denotational Semantics
provide an excellent way to describe a language concisely “briefly ”. Provides a rigorous “strict” way to think about programs Can be an aid to language design For example, statements for which the denotational semantic description is complex and difficult may indicate to the designer that such statements may also be difficult for language users to understand and that an alternative design may be in order. Has been used in compiler generation systems. (work started but didn’t completed (Jones,1980; Milos et al., 1984; Bodwin et al., 1982)). Because of its complexity, is little used by language users. Copyright © 2012 Addison-Wesley. All rights reserved.
23
operational semantics denotational semantics
In both the meaning of a program is the values of its variables (the state) the state is changed by algorithmic code (ie. 3 address code) the state is changed by mathematical objects Copyright © 2012 Pearson Education. All rights reserved.
24
حكمة اليوم لا ينمو الجسد إلا بالطعام و الرياضة و لا ينمو العقل إلا بالمطالعة و التفكير.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.