introduction to operational semantics

34
Introduction to Operational Semantics John L. Singleton University of Central Florida

Upload: jsinglet

Post on 24-Jan-2018

484 views

Category:

Software


0 download

TRANSCRIPT

Introduction to

Operational Semantics

John L. Singleton

University of Central Florida

Syntax vs Semantics

While b Do C

What Does this Mean?

“To execute this command, you execute the command C repeatedly so long as the expression

One Possible Interpretation:

Syntax of a Language L

• A description of the valid text (structure) that

programs may be composed of.

• e.g.: “A statement must end with a ‘;’”

• e.g.: “A ‘{‘ character must be followed by a ‘}’”

• Can be described by a formal grammar, e.g:

Backus–Naur form.

• Comes in two flavors: Abstract and Concrete

Concrete vs Abstract Syntax

3 + 3

+ 3 3

add(3, 3)

P1

P2

P3

Program Text Parse Trees

Concrete vs Abstract Syntax

Program Text

(Concrete Syntax)Parse Trees

Abstract Syntax“Meaning” of Program

Defining Abstract Syntax

• Unlike concrete syntax, abstract syntax is not

designed to be used by a parser.

• Must first be normalized for use in a parser (e.g.:

removal of left recursion, precedence, etc.)

• Abstract syntax describes valid unique parse trees

for a language, but does not describe how to derive

them from text.

Defining Abstract Syntax

Contrasting Abstract and

Concrete Syntax

Equal

1 2

Abstract Syntax Tree

Exp

1 2

‘==‘ExpExp

Parse Tree

VS

nodes are constructors

leaves are atoms

nodes are syntactic categories

leaves are terminals

Creating Abstract Syntax

from Concrete Syntax

Step 1: Labeling

Assign a label to the syntactic category

Exp ::= Exp “+” Exp

plus: Exp ::= Exp “+” Exp

Creating Abstract Syntax

from Concrete Syntax

Step 2: Remove Terminals

plus: Exp ::= Exp “+” Exp

plus: Exp ::= Exp Exp

Creating Abstract Syntax

from Concrete Syntax

Step 3: Convert to Constructor

plus: Exp ::= Exp Exp

plus :: (Exp, Exp) -> Exp

Rewrite our rule so that the syntactic category is the “output” of our constructor

Operational Semantics and

Syntax

• Abstract syntax, as opposed to concrete syntax,

provides us with a streamlined set of syntactic

constructs to work with.

• In concept, one could develop a semantics with

either type of syntax, in practice, however, it’s

usually easier to work with abstract syntax.

Rule Induction

• The main mathematical tool used in semantics is

induction

• Recall, an inductive proof has three parts:

• Base Case (show for n=0, it’s true)

• Inductive Hypothesis (suppose it’s true for some k)

• Inductive Step (assume IH is true, prove k+1 is

true).

Rule InductionSuppose

Premise

Conclusion

Structural Induction

• Since we are working in programming languages, it

is often useful to use a form of induction known as

structural induction.

• Useful when the definition of our objects (unlike

mathematical objects) are purely syntactic in nature.

Structural Induction Over

Lists

Structural Induction Over

Expressions

How can we use this to show:

An Operational Semantics for

a Simple Imperative Language

Evaluation Relations

• The first step in defining an operational semantics

for a language (after defining the domains and

formation rules) is to define an transition/evaluation

relation

“expression a in state σ, evaluates to n”

“location X evaluates to its contents in a

state”

“n in state σ, evaluates to n”

Evaluation of Arithmetic

Expressions

Derivations

• Application of a given rule leads to a rule instance.

• A rule instance is simply a given rule with the

appropriate values substituted.

• For Example:

Sample Derivation

Derive the expression (Init + 5) + (7 + 9), where Init is a location with a value initialized to zero.

Execution of Commands

• Whereas expressions evaluate to a certain value in

a state, commands execute and modify the state.

Command c executed in state σ terminates in state σ’

After the execution of assignment to location X, the

value of 5 will replace the contents of X.

Semantics of Commands

Semantics of Commands

Other Types of Operational

Semantics• The semantics we have focused on here are the so-

called “big-step” (natural) semantics.

• Probably (currently) the most popular form of

semantics for describing programming languages.

• Why?

• Other types of operational semantics: “Small-Step”

semantics, Reduction Semantics, Evaluation

Semantics…

Contrasting Small Step and

Big Step Semantics

• In big step semantics, evaluations of entire

structures happen in a single step.

• In contrast, small step semantics take a single step

with each rule.

• Roughly, one can think of this as an abstract

implementation of a virtual machine.

Small Step

Big Step

Big Step

Small Step

Big Step

Small Step

Conclusions

• We introduced the idea of abstract syntax and

provided a description in contrast with the familiar

concrete syntax.

• Introduced rule induction and structural induction.

• Provided a simple operational semantics for a small

imperative programming language

• Introduced other flavors of operational semantics.

References

• [1] M. Hennessy, The Semantics of Programming

Languages: An Elementary Introduction Using

Structural Operational Semantics. Chichester

England ; New York: John Wiley & Sons Ltd, 1990.

• [2] G. Winskel, Formal Semantics of Programming

Languages. Cambridge, Mass. u.a.: The MIT Press,

1993.

Evaluation Relation for

PL/0

• What would a transition relation look like for PL/0?