Top Banner
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern University, Houston January, 2008
28

Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages

Jan 05, 2016

Download

Documents

Brenna Vinson

Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages. Instructor: Li Ma Department of Computer Science Texas Southern University, Houston. January, 2008. Review and Preview. Last lecture Syntax specifications of programming languages - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

Programming Languages and Design

Lecture 3 Semantic Specifications of Programming

Languages

Instructor: Li Ma

Department of Computer ScienceTexas Southern University, Houston

January, 2008

Page 2: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

2

Review and Preview

Last lecture Syntax specifications of programming languages

Regular expressions and rules Context-free grammar Parse tree Ambiguity

Today’s lecture Semantic specifications of programming languages

Reference: Chapter 5 of “Foundations of Programming Languages: Design and Implementation”, S. H. Roosta

Three formal semantics: axiomatic semantics, operational semantics, denotational semantics

Page 3: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

3

Language Description

A formal language is any set of character strings with characters chosen from a fixed, finite set of an alphabet of symbols The strings that belong to the language are called its

constructs, or phrases Any programming language description can be

classified according to its Syntax, which deals with the formation of phrases Semantics, which deals with the meaning of phrases Pragmatics, which deals with the practical use of

phrases

Page 4: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

4

Semantics

Semantics referred to the study of what words mean, the relation of words to sentences

of a language, their significance, and changes in their meaning

In computer science The investigation of it focuses on developing ways to

express the meaning of programming languages It also Involves a description of what happens during

execution of a program that refers to rules specifying its meaning

Page 5: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

5

Formal Semantics Methods

Different notational systems could be used to describe programming languages

Formal definition method is precise, but complex and abstract Question the behavior of programming languages by

mathematical method

Formal methods are important because they provide an unambiguous language definition standards to unify the language a basis for correctness proof by both compilers and programs

Page 6: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

6

Formal Semantic Methods (cont’)

Three formal methods differ in their formation Axiomatic semantics – proof system Operational semantics – proof system Denotational semantics – model theory

Page 7: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

7

Axiomatic Semantics

Associate with each syntactically correct program construct properties of variables that hold before execution starts and after the program halts Apply mathematical logic to language constructs The state of the computation is described by a logical

statement – a predicate or an assertion

Page 8: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

8

Principles of Axiomatic Semantics

Preconditions and postconditions are used to specify the behavior of programs at each step of execution

{P} S {Q} A logical expression P and Q are logical assertions for precondition and

postcondition, respectively S is a construct of the subject language If P is true before execution of S and if the execution

of S terminates, then Q is true

Page 9: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

9

Proof Rules

The goal of axiomatic semantics is to provide logical statements that capture the intended meaning of each construct in a programming language Deduce a specification for a construct using logical

statements

Also called inference rules, or deduction rules Enable the truth of certain assertions to be

deduced from the truth of certain other assertions

Page 10: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

10

Proof Rules (cont’)

R

RRR n,...,, 21

R

RRRR nn 121 ,...,,

given that R1, R2, …, Rn are true, then R is true can be deduced

if the truth of Rn+1 is deduced by assuming the truth of R1, R2, …, Rn, then the truth of R can be deduced

R1, R2, …, Rn, Rn+1, and R are all assertions

Page 11: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

11

Program Correctness

A purely static analysis for the text of a program is used to prove the correctness of the program Proof theory – a set of axiomatic specifications A correct program is one that meets its specifications

Two predicates for correctness requirements of the program: A precondition on input variables A postcondition on input and output variables

The aim of verification is to show that if input holds before the program runs, execution terminates in a state where output holds

Page 12: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

12

Example

Given a program fragment, prove it is correct with respect to some assertions

Page 13: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

13

Operational Semantics

Specify a programming language by the operations through which each program construct is carried out

Hypothetical machine is an abstract machine A collection of actions described in mathematical

notation Apply the operations to values to show the run-time

behavior of a program

Individual steps should be checked to see how programs are implemented

Page 14: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

14

Principles of Operational Semantics

Shortcomings if using actual machine Difficult to understand actions Computer-dependent Correct interpreters are difficult to write

So use an abstract machine Independent of the subject language Define an abstract machine with primitive instructions

Simple, so no misunderstanding

States and transition between states must be explicitly defined

Page 15: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

15

Principles of Operational Semantics (cont’)

The virtual machine interprets a program by passing through a sequence of discrete states State transitions are defined by a set of instruction

definitions written in specific notation Three components of the abstract machine

Program Control system Memory system

The semantic description of the programming language specifies a translation to a metelanguage

Page 16: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

16

VDL MetaLanguage

One of the best known metalanguage A formal system: model the interpretation of a

program Two specification parts:

A translator: specify the translation of each program into an abstract syntax tree

• Each instruction is converted into the chosen low-level language

An interpreter: specify how the program could be carried out

Interpret a program by passing through a sequence of transition states to model the operations

Page 17: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

17

VDL MetaLanguage (cont’)

A typical VDL structure is represented as a collections of subtrees All nonprimitive objects are modeled as syntax tree

Program, memory, input, output, environment, control mechanism, …

An initial configuration contains all the components Control system subtree represents a single instruction

The transition functions perform the steps of the computation

Page 18: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

18

VDL MetaLanguage (cont’)

A computation consists of Select a terminal node of the control tree Evaluate it according to the specific definition Produce a new state with a modified control tree

Interpretation of the program terminates when all the nodes of the control tree have been evaluated Evaluation of the program is concluded

Page 19: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

19

Notations for Abstract Syntax Trees

Elementary objects No internal structure A terminal node of a tree A sequence of one or more uppercase letters

For example, A, +, Ω Or a sequence of one or more characters enclosed in single

quotes Composite objects

A set of one or more pairs of the form <s : o> “s” for a selector, the name begins with “s-”

The null object and the associated selector have no corresponding branch in the tree diagram

A selector may be defined as a function

Page 20: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

20

Notations for Abstract Syntax Trees (cont’)

Lists An object may be regarded as a list of other objects

The function head yields the first element The function tail yields a list consisting of all but first element The function length provides the number of elements in list

Predicates A function that takes an object as an argument and yields an

elementary object as True or False The name begin with “is-”

Equations An equation defines a predicate for a class of objects, the

selectors involved, and the kinds of objects they select

Page 21: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

21

Example

Given a program fragment, provide the VDL syntax tree representation with respect to the syntax rules for a language

Page 22: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

22

Denotational Semantics

Define a programming language behavior by applying mathematical functions to programs to represent their meaning

Mathematical semantics, Programs can be translated to functions Their properties can be proved by using the standard

mathematical theory of functions Function calculus

Page 23: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

23

Principles of Denotational Semantics

Denotational semantics not only assigns a meaning to a complete program, but also to every phrase

Each phrase is defined in terms of the meaning of its subphrases The meaning of each phrase – denotation

Denotational definitions are enclosed in [[ ]], to separate from syntactic definitions If p is a syntactic phrase, meaning[[p]] is a denotational

specification For example, 2*4 and 7+1 are syntactic phrase,

meaning[[2*4]]=meaning[[7+1]]=meaning[[8]]

Page 24: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

24

Principles of Denotational Semantics (cont’)

Denotational semantics focus on the net effect of running a program, ignoring the steps by which that effect is achieved

Advantages We can predict the behavior of a program without

running it on computer We can reason about programs

For example, to prove whether two programs are equivalent

We discuss four different aspects of denotational semantics

Page 25: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

25

Semantic Domains

Sets of mathematical objects of a particular form A denotational definition lists its set of elements

and operations, but no properties of the operations

Domain characteristics A primitive domain: primitive values as elements A function domain: functions or mappings A Cartesian production domain: A X B A disjoint union domain: A + B A sequence domain: homogeneous sequence

Page 26: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

26

Semantic Functions

Semantic functions synthesize the meaning of constructs Map syntactic objects onto objects in semantic

domains These functions are specified by giving their domains

– signatures A program can be viewed as an entity that

receives input and produces outputProgram : Program => (Input => Output)

Each semantic domain has one semantic function

Page 27: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

27

Semantic Equations

Specify how the semantic functions act on each construct by using the structure of the grammar rules For example, given

grammar rule: Digit ::= ‘0’ | ‘1’ | … | ‘9’

semantic function: Dig : Digit => Integer They can be combined to form the semantic equations

Dig( Digit=>’0’) = 0

Dig( Digit=>’1’) = 1

Dig( Digit=>’9’) = 9

or

Dig[[‘0’]] = 0, Dig[[‘1’]] = 1, …, Dig[[‘9’]] = 9

Page 28: Programming Languages and Design Lecture 3  Semantic Specifications of Programming Languages

28

Auxiliary Functions

Predefined mathematical operations plus, times, minus, and divide in semantic functions For example,

plus: Number + Number => Numbertimes: Number x Number => Number

Number x Number is an element of Cartesian product domain Number is an element of primitive domain

So far, we can have a complete denotational specification of a language for some construct of that language, which should have Production rules, semantic domain, semantic functions, auxiliary

functions, and semantic equations