Download presentation
Presentation is loading. Please wait.
1
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories (terminals, non-terminals) –Specify value domains for semantic (meaning) functions –Define meaning functions to map syntactic constructs into their intended meaning Syntax-directed approach provides compositional rules for meanings.
2
Example: decimal numerals Syntactic categories: –D Є Digits the decimal digits: 0,1 … –N Є Num decimal numerals (strings) Productions –D ::= ‘0’ | ‘1’ | ‘2’…. –N ::= D | N D Value Domain: –Nat = {0,1,…} the natural numbers –Semantics defines mapping of strings into numbers
3
The meaning of decimal numerals Semantic functions –D : Digits → Nat –M : Num → Nat Semantic equations: –D [‘0’] = 0, D [‘1’] = 1, … –M [D] = D [D] production for M –M [N D] = 10 * M [N] + M [D] production for M
4
Propositional logic Boolean expressions with variables and logical connectives. Categories –P Є Prop : propositions (variable names) –F Є Form : Formulae Productions –F ::= P | ¬ F | F or F | F and F | F => F (implication) Value Domains –Bool = {True, False} boolean values – Є Assign = P → Bool assignments (values of names)
5
The meaning of propositional formulae The value of a formula depends on the current assignments. Curried form is: –M : Form → Assign → Bool Semantic equations: –M [P] = (P) -- meaning of variable is its value –M [ ¬ F] = ¬ M [F] –M [F 1 or F 2 ] = M [F 1 ] or M [F 2 ] –M [F 1 and F 2 ] = M [F 1 ] and M [F 2 ] –M [F 1 => F 2 ] = ¬ M [F 1 ] or M [F 2 ] Must distinguish “or” in syntax from or in semantics
6
A language of expressions Syntactic categories –Id Є Ident Identifiers –Num Є N numeric literals –E Є Exp Expressions Productions –E ::= num | Id | (E 1 + E 2 ) | (E 1 – E 2 ) –E ::= let Id = E 1 in E 2 end local binding Value domains –Semantics describes integer values of expressions, in terms of current bindings of identifiers: –Int : {…-2, -1, 0, 1, 2…} integers Є Env : Ident → Int environments
7
The meaning of simple expressions Semantics describes rule of computation. Value is a function of environment: E : Exp → Env → Int Semantic functions: E [Id] = (Id) E [ num] = M [num] given previously E [ E 1 + E 2 ] = E [E 1 ] + E [E 2 ] E [ E 1 - E 2 ] = E [E 1 ] - E [E 2 ]
8
Bindings and environments A binding is modeled by an update function, best described in a programming language: fun Update (Env, Id, Val) (x) = if Id = x then Val else Env (x) E [let Id = E 1 in E 2 end] = E [E 2 ] Update ( , Id, E [E 1 ])
9
A domain with an error value To model unbound names or type errors in a dynamically typed language, extend domain with undefined value: –Int = {… -2, -2, 0, 1, 2, …} integers – Є Env = Ident → (Int + { } ) environments Type checking is explicit: –E [Id] = let n = (Id) in if Is_Int (n) then n else end; –E [E 1 + E 2 ] = let n 1 = E [E 1 ] n 2 = E [E 2 ] in if Is_int (n 1 ) andalso Is_Int (n 2 ) then n 1 + n 2 else end;
10
Denotational Semantics of Imperative languages Need domains to describe the state of the computation, including sequences of instructions (the program counter) and memory (the mapping of names to locations). C Є Com : commands, describes control structures –C ::= C 1 ; C 2 –C ::= if E then C 1 else C 2 –C ::= while E do C Value domain Є States configuration of computation
11
The meaning of commands The value of a command is a state: C [if E then C1 else C2] = if IsTrue ( E [E] ) then C [C1] else C [C2] C [C1 ; C2] = let ’ = C [C1] in C [C2] ’ end C [while E do C] = let fun p ( ’) = if IsTrue ( E [E] ) then p ( C [C] ’ ) else ’ in p ( ) end
12
Describing state New syntactic category: –L Є left-hand side : for bindings and references Value domains: – Є Loc locations (addresses) – Є States = Loc → (Int + ) memory – Є Env = Ident → (Int + Loc + ) environments Semantic functions: –C : Com → Env → States → States –E : Exp → Env → States → Int –L : Left → Env → States → Loc
13
Manipulating state C ::= C1; C2 | if E then C1 else C2 | L := E assignment | local V:= E in C end local variable L ::= Id (lhs is simple name) C [L := E] = let = L [L] ; meaning of l.h.s is a location n = E [E] meaning of rhs is a value in update ( , , n); meaning of assignment is new state end; side effects of E are ignored
14
Local variables Assume pool of available locations C [local V := E in C end] = let Є Loc such that = unused; n := E [ E] in C [C] update ( , V, ) update ( , , n) end Enlarge environment with new variable, enlarge state with its initial value, evaluate command in new environment / state
15
Dereferencing Obtaining the value of a variable is a two-step affair: name -> loc, loc -> value E ::= !L | if E1 then E2 else E3 | … E [ !L] = let = L [L] in end
16
Programs New syntactic category: P P ::= program (I) C end program is a command with a read / write parameter Semantic function: P : Prog -> int -> int P [ program (I); C end] n = let fun (J) = undefined for all J fun = unused for all Є Loc f = C [C ] update ( , I, ) update ( , , n) in f ( ) end
17
Function calls Semantic domains: A function affects the state: f Є Fun = States -> int > int * States The name of the function denotes a function, not a location: Denote = int + Loc + Fun Є Env = Ident -> (Denote + ) A function does not depend on the environment: static binding of identifiers.
18
Syntax and semantics of functions M [function f (a) = E 1 in E 2 end] = E 1 is body of function, E 2 contains calls let fun g ’ n = M [E 1 ] update ( , a, n) ’ in M [E 2 ] update ( , f, g) end M [call f (E)] = let (n, ’) = M [E] ; func = (f) in func ’ n end
19
Transfer of control In the presence of goto’s and exceptions, state must include notion of program counter. C ::= C1 ; C2 | if E then C1 else C2 | L := E | local I := E in C end | skip | goto L | L : C M ::= program (I) ; C end New semantic domains and functions: Є CC : States -> int (next instruction to execute) C : Com -> Env -> CC -> States -> Int
20
Commands have continuations C [C 1 ; C 2 ] = let fun ’ ( ’) = C [C 2 ] ’ in C [C 1 ] ’ A label captures a continuation: C [L : C] = let fun ’ ( ’) = C [C] update ( , L, ’) ’ in ’ ( ) end
21
The goto resets the continuation C [goto L] = ( L) The meaning of the goto is the result of applying the continuation of the label to the state. C [Here : goto Here] = ’ ( ) And ’ ( ) = ’ ( ) for any => infinite loop
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.