copyright 2006 addison-wesley. all rights reserved.1-1 ics 410: programming languages chapter 3 :...

15
Copyright © 2006 Addison-Wesley. All rights reserved. 1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Upload: dennis-james

Post on 18-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

Copyright © 2006 Addison-Wesley. All rights reserved.1-3 Denotational Semantics (continued) The process of building a denotational specification for a language define for each language entity both –a mathematical object and –a function that maps instances of that entity onto instances of the mathematical object. The difficulty with this method lies in creating the objects and the mapping functions. The method is named denotational because the mathematical object denote the meaning of their corresponding language entity.

TRANSCRIPT

Page 1: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-1

ICS 410: Programming Languages

Chapter 3 :Describing Syntax and Semantics

Denotational Semantics

Page 2: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-2

Denotational Semantics

• The most widely known method for describing the meaning of programs.

• Based on recursive function theory.

• Originally developed by Scott and Strachey (1970)

Page 3: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-3

Denotational Semantics (continued)

• The process of building a denotational specification for a language define for each language entity both – a mathematical object and – a function that maps instances of that entity onto

instances of the mathematical object.

• The difficulty with this method lies in creating the objects and the mapping functions.

• The method is named denotational because the mathematical object denote the meaning of their corresponding language entity .

Page 4: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-4

Denotation vs Operational

• In operational semantics (OS), the state changes are defined by coded algorithms

• In denotational semantics (DS), the state changes are defined by rigorous mathematical functions

Page 5: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-5

Example – Binary Number

• The syntax of a binary number is:

• <bin_num> → 0

| 1

| <bin_num> 0

| <bin_num> 1

• To describe the meaning of a binary number using denotational semantics we associate the actual meaning with each rule that has a single terminal symbol in its RHS.

• The syntactic entities in this case are ‘0’ and ‘1’.

• The objects are the decimal equivalent.

Page 6: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-6

Example – Binary Number

• Let the domain of semantic values of the objects be N, the set of nonnegative decimal integer values.

• The function Mbin maps the syntactic entities of the previous grammar to the objects in N.

• The function Mbin ,for the above grammar, is defined as follows:Mbin(‘0’) = 0

Mbin(‘1’) = 1

Mbin(<bin_num> ‘0’) = 2 * Mbin(<bin_num>)

Mbin(<bin_num> ‘1’) = 2 * Mbin(<bin_num>) + 1

Page 7: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-7

Example - Decimal Numbers

<dec_num> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | <dec_num> ( 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)

• The denotational semantics for these syntax rules are:

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

Page 8: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-8

Denotational Semantics: Program Constructs

• OS are defined in terms of state changes.

• DS are defined in nearly the same way.

• For simplicity, DS are defined in terms of only the values of all of the program’s variables.

• The difference between OS and DS is that: – state changes in OS are defined by coded

algorithms, written in some programming language

– state changes in DS are defined by mathematical functions.

Page 9: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-9

Denotational Semantics: Program Constructs

• Let the state of a program be represented as a set of ordered pairs as follows:

s = {<i1, v1>, <i2, v2>, …, <in, vn>}• Each i is a variable and the associated v is its current value.

• Any of the v’s can have the special value undef.

• 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

• The state changes are used to define the meanings of programs and program constructs.

• Some constructs, such as expressions, are mapped to values, not states.

Page 10: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-10

Denotational Semantics: Expressions

• We assume here that we deal with only simple expressions:– Only + and * operators.– An expression can have at most one operator.– The only operands are scalar variables and integer

literals.– No parenthesis.– The value of an expression is integer.

Page 11: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-11

Denotational Semantics: Expressions

• The BNF description of these expressions:<expr> <dec_num> | <var> | <binary_exp><inary_exp> <left_exp> <operator> <right_exp><left_exp> <dec_num> | <var><right_exp> <dec_num> | <var><operator> + | *

• The only error we consider in expressions is that a variable has an undefined value.

• Let Z be the set of integers, and let error be the error value.

• Then Z U {error} is the set of values to which an expression can evaluate.

Page 12: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-12

Denotational Semantics: Expressions

• The DS of expressions are (dot notation refer to child nodes of a node)

Me(<expr>, s) = case <expr> of <dec_num> = Mdec(<dec_num>, s)

<var> = if VARMAP(<var>, s) == undef then error else VARMAP(<var>, s)

<binary_expr> = if (Me(<binary_expr>.<left_expr>, s) == undef OR Me(<binary_expr>.<right_expr>, s) = undef) then error

else if (<binary_expr>.<operator> == ‘+’ then Me(<binary_expr>.<left_expr>, s) + Me(<binary_expr>.<right_expr>, s) else Me(<binary_expr>.<left_expr>, s) * Me(<binary_expr>.<right_expr>, s)

Page 13: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-13

Assignment Statements

• An assignment statement is an expression evaluation plus the setting of the left-side variable to the expression’s value. Maps state sets to state sets

Ma(x := E, s) = if Me(E, s) == error

then error

else s’ = {<i1’,v1’>, <i2’,v2’>,..., <in’,vn’>},

where for j = 1, 2, ..., n,

vj’ = VARMAP(ij, s) if ij <> x

= Me(E, s) if ij == x

Page 14: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-14

Logical Pretest Loops

• Assume we have two mapping functions, Msl and Mb

– Msl Maps statement list to states.– Mb Maps boolean expression to boolean value.

• The DS of a simple loop are:

Ml(while B do L, s) = if Mb(B, s) == undef

then error

else if Mb(B, s) == false

then s

else if Msl(L, s) == error

then error

else Ml(while B do L, Msl(L, s))

Page 15: Copyright  2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-15

Loop Meaning

• The meaning of the loop is the value of the program variables after the statements in the loop have been executed the prescribed number of times, assuming there have been no errors

• In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions