1
Prof. Aiken
CS 143
Lecture 13
1
Operational Semantics of Cool
Lecture 13
Prof. Aiken
CS 143
Lecture 13
2
Lecture Outline
• COOL operational semantics
• Motivation
• Notation
• The rules
Prof. Aiken
CS 143
Lecture 13
3
Motivation
• We must specify for every Cool expression
what happens when it is evaluated
– This is the “meaning” of an expression
• The definition of a programming language:
– The tokens
lexical analysis
– The grammar
syntactic analysis
– The typing rules
semantic analysis
– The evaluation rules
code generation and optimization
Prof. Aiken
CS 143
Lecture 13
4
Evaluation Rules So Far
• We have specified evaluation rules indirectly
– The compilation of Cool to a stack machine
– The evaluation rules of the stack machine
• This is a complete description
– Why isn’t it good enough?
Prof. Aiken
CS 143
Lecture 13
5
Assembly Language Description of Semantics
• Assemblylanguage descriptions of language
implementation have irrelevant detail
– Whether to use a stack machine or not
– Which way the stack grows
– How integers are represented
– The particular instruction set of the architecture
• We need a complete description
– But not an overly restrictive specification
Prof. Aiken
CS 143
Lecture 13
6
Programming Language Semantics
• A multitude of ways to specify semantics
– All equally powerful
– Some more suitable to various tasks than others
• Operational semantics
– Describes program evaluation via execution rules
• on an abstract machine
– Most useful for specifying implementations
– This is what we use for Cool
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Prof. Aiken
CS 143
Lecture 13
7
Other Kinds of Semantics
• Denotational semantics
– Program’s meaning is a mathematical function
– Elegant, but introduces complications
• Need to define a suitable space of functions
• Axiomatic semantics
– Program behavior described via logical formulae
• If execution begins in state satisfying X, then it ends in
state satisfying Y
• X, Y formulas
– Foundation of many program verification systems
Prof. Aiken
CS 143
Lecture 13
8
Introduction to Operational Semantics
• Once again we introduce a formal notation
• Logical rules of inference, as in type checking
Prof. Aiken
CS 143
Lecture 13
9
Inference Rules
• Recall the typing judgment
Context
e : C
(in the given
context
, expression
e
has type
C
)
• We try something similar for evaluation
Context
e : v
(in the given
context
, expression
e
evaluates to
value
v
)
Prof. Aiken
CS 143
Lecture 13
10
Example Operational Semantics Rule
• Example:
• The result of evaluating an expression can
depend on the result of evaluating its
subexpressions
• The rules specify everything that is needed to
evaluate an expression
Context
e
1
: 5
Context
e
2
: 7
Context
e
1
+ e
2
: 12
Prof. Aiken
CS 143
Lecture 13
11
Contexts are Needed for Variables
• Consider the evaluation of
y
x + 1
– We need to keep track of values of variables
This is the end of the preview.
Sign up
to
access the rest of the document.
 '09
 Logic, denotational semantics, Operational Semantics, Formal semantics of programming languages, Axiomatic semantics, Prof. Aiken CS

Click to edit the document details