Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.

Similar presentations


Presentation on theme: "Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 6 Formal Semantic Methods (cont’) Three formal methods differ in their formation  Axiomatic semantics – proof system  Operational semantics – proof system  Denotational semantics – model theory

7 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

8 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

9 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

10 10 Proof Rules (cont’) given that R 1, R 2, …, R n are true, then R is true can be deduced if the truth of R n+1 is deduced by assuming the truth of R 1, R 2, …, R n, then the truth of R can be deduced R 1, R 2, …, R n, R n+1, and R are all assertions

11 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

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

13 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

14 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

15 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

16 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

17 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

18 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

19 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” 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

20 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

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

22 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

23 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]]

24 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

25 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

26 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 output Program : Program => (Input => Output) Each semantic domain has one semantic function

27 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

28 28 Auxiliary Functions Predefined mathematical operations plus, times, minus, and divide in semantic functions  For example, plus: Number + Number => Number times: 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


Download ppt "Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern."

Similar presentations


Ads by Google