100 likes | 393 Views
Operational Semantics. References: Kurtz (Ch. 8.4, 8.5, 8.6) Many ways to define o.s . of a language L: Define an interpreter for L Define a compiler for L, plus an interpreter for the assembly language used Specify how the state changes as various commands execute
E N D
Operational Semantics References: Kurtz (Ch. 8.4, 8.5, 8.6) Many ways to define o.s. of a language L: • Define an interpreter for L • Define a compiler for L, plus an interpreter for the assembly language used • Specify how the state changes as various commands execute We will consider the third approach CSE 755, part4
Two approaches to O.S.: Big step, small step. Start with big-step • For each command C, define the final state you reach if you execute C starting in a given initial state. Do this inductively on the structure of the language. • Notation: <C, s> s' • <C1, s> s', <C2, s'> s'' <C1; C2, s> s''That looks like axiomatic semantics but it is not: s, s', etc. are single states • Also need to define sem. of boolean and arithmetic expressions<a1, s> n1, <a2, s> n2 <a1+a2, s> n1 n2 • <X, s> s(X)
<b, s> true, <C, s> s' (ite1) <if b then C else C', s> s' • <b, s> false, <C', s> s' (ite2)<if b then C else C', s> s' • <b, s> false (w1) <while b do C, s> s • <b, s> true, <C, s> s', <while b do C, s'> s'' (w2)<while b do C, s> s'' • That is big-step operational sem. It ignores intermediate states • Small-step: <C, s> <C', s'> or <C, s> s' • <C1, s> <C1', s'> (sc1) <C1; C2, s> <C1'; C2, s'> • <C1, s> s' (sc2) <C1; C2, s> <C2, s'> • But: often it is not clear what a "small" step is.
Denotational Semantics References: Kurtz (Ch. 9, 10(?)); Pagan (Ch. 4.2) Idea: The d.s. of each construct is the function computed by it Important: Compositionality: Semantics of a construct should be obtained by appropriately composing the semantics of its components (and should not depend on the details of the components) CSE 755, part4
Den. sem. is similar to big-step o.s. but uses a more mathematical notation • D[C](s) = s' means: the den. semantics of C is a function that maps s to s'E[e](s) = n means: ... B[b](s) = t means ... • D[skip](s) = s • D[x:=e](s) = s[X/n] if E[e](s) = n • D[C1; C2](s) = D[C2](D[C1](s)) • D[read X](s) = s[X/head(s(IN)), IN/tail(s(IN))] if s(IN) <> "error" if s(IN) = <> • D[while b do C](s) = s if B[b](s) = false D[while b do C](D[C](s)) if B[b](s) = true • Consider "while true do skip" ... the above reduces to:D[while true do skip](s) = D[while true do skip](s) !! • Solution: need to introduce the notion of a "bottom" state ( ), an orderingon states, an ordering on functions, and use the "least fixed point" of (7) as the semantics: See Kurtz-Slonnegar book for some details.
Possible to handle more complex languages such as with nested blocks, procedures, etc. • Consider: begin int x; x:=0; begin int x; x:=1; end; write x; end; That should output 0, not 1 But when we reach the end of the block, we can't just go back to the state we had at the start of the block begin int x; int y; x:=0; y:=0; begin int x; x:=1; y:=1; end; write x; write y; end; That should output 0, 1; not 0, 0
So: split the state into: • an environment en that maps variable names to addresses; • and a storest that maps addresses to values (integers, ...) • The semantics of a command gives a new store but not a new environment; the semantics of a declaration (or definition) gives a new environment: D[d](en) = en' C[c](en, st) = st' • C[skip](en, st) = st • C[x:=5](en, st) = st' wherest' = st[en(x)/5] • D[int x](en) = en[x new(en)] • C[begin ds; cs; end](en, st) = C[cs](D[ds](en), st) • C[c1; c2](en, st) = C[c2](en, C[c1](en, st)) : key: restores, at start of C2, environment to what it was at start of C1 • You would not want to implement using this approach; why not? • Consider behavior of the second example from previous slide • What if semantics of declarations depended on the state?
That’s all folks! Final exam: See course homepage Topics: Everything we have discussed in class Review: I will post on the newsgroup and in Piazza Sample finals: maybe ... Thanks! CSE 755, part4