An introduction to operational semantics of the cool programming language, explaining how expressions are evaluated on an abstract machine. Topics include motivation, notation, rules for simple expressions, while loops, new expressions, and dispatch. The document also discusses the importance of operational semantics for programming language implementation.
Download Operational Semantics of Cool Programming Language: Rules and Examples - Prof. Westley Wei and more Study notes Programming Languages in PDF only on Docsity!
Operational Semantics Operational Semantics
One-Slide Summary
(^) Operational semantics are a precise way of specifying how to evaluate a program.
(^) A formal semantics tells you what each expression means.
(^) Meaning depends on context : a variableenvironment will map variables to memory locations and a store will map memory locations to values.
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 ⇒ interpretation
Evaluation Rules So Far
(^) So far, we specified the evaluation rules intuitively
(^) We described how dynamic dispatch behaved in words (e.g., “just like Java”)
(^) We talked about scoping, variables, arithmetic expressions (e.g., “they work as expected”)
(^) Why isn’t this description good enough?
Programming Language Semantics
(^) There are many ways to specify programming language semantics
(^) They are all equivalent but some are more suitable to various tasks than others
(^) Operational semantics
(^) Describes the evaluation of programs on an abstract machine
(^) Most useful for specifying implementations
(^) This is what we will use for Cool
Other Kinds of Semantics
(^) Denotational semantics
(^) The meaning of a program is expressed as a mathematical object
(^) Elegant but quite complicated
(^) Axiomatic semantics
(^) Useful for checking that programs satisfy certain correctness properties - (^) e.g., that the quick sort function sorts an array
(^) The foundation of many program verification systems
Example Operational Semantics
Inference Rule
(^) In general the result of evaluating an expression depends on the result of evaluating its subexpressions
(^) The logical rules specify everything that is needed to evaluate an expression Context ` e1+ e2
Context ` e 1
Context ` e 2
Aside
The operational semantics inference rules for Cool will become quite complicated - i.e., many hypotheses
This may initially look daunting
Until you realize that the opsem rules specify exactly how to build an interpreter
That is, every rule of inference in this lecture is pseudocode for something in PA - So by walking through the opsem is just like walking through the project.
What Contexts Are Needed?
(^) Contexts are needed to handle variables
(^) Consider the evaluation of y à x + 1
(^) We need to keep track of values of variables
(^) We need to allow variables to change their values during the evaluation
(^) We track variables and their values with:
(^) An environment : tells us at what address in memory is the value of a variable stored
(^) A store : tells us what is the contents of a memory location
What Contexts Are Needed?
(^) Contexts are needed to handle variables
(^) Consider the evaluation of y à x + 1
(^) We need to keep track of values of variables
(^) We need to allow variables to change their values during the evaluation
(^) We track variables and their values with:
(^) An environment : tells us at what address in memory is the value of a variable stored
(^) A store : tells us what is the contents of a memory location Remind me – why do we need a separatestoreandenvironment****? We're just building an interpreter. Aren't those compiler notions?
Lost?
Environments may seem hostile and unforgiving
But soon they'll feel just like home!
Names! Locations
Stores
(^) A store maps memory locations to values
(^) Example store: S = [l 1 ! 5, l 2
! 7]
To lookup the contents of a location^ l 1 in store S we write S(l 1
To perform an assignment of^12 to location^ l 1 we write S[12/l 1
]
(^) This denotes a new store S’ such that S’(l 1 ) = 12 and S’(l) = S(l) if l ≠ l 1
Cool Values
(^) All values in Cool are objects
(^) All objects are instances of some class (the dynamic type of the object)
(^) To denote a Cool object we use the notation X(a 1 = l 1 , …, a n = l n ) where
(^) X is the dynamic type of the object
a i are the attributes (including those inherited)
l i are the locations where the values of attributes are stored
Cool Values (Cont.)
(^) Special cases (classes without attributes) Int(5) the integer 5 Bool(true) the boolean true String(4, “Cool”) the string “Cool” of length 4
(^) There is a special value void that is a member of all types - (^) No operations can be performed on it - (^) Except for the test isvoid - (^) Concrete implementations might use NULL here